Skip to content

ref(serverless): Use GitHub action to zip lambda layer (second try) #5093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ env:
${{ github.workspace }}/packages/ember/*.d.ts
${{ github.workspace }}/packages/ember/instance-initializers
${{ github.workspace }}/packages/gatsby/*.d.ts
${{ github.workspace }}/packages/serverless/dist-awslambda-layer/*.zip
${{ github.workspace }}/packages/core/src/version.ts
${{ github.workspace }}/dist-serverless

BUILD_CACHE_KEY: ${{ github.event.inputs.commit || github.sha }}

Expand Down Expand Up @@ -99,6 +100,11 @@ jobs:
# this file) to a constant and skip rebuilding all of the packages each time CI runs.
if: steps.cache_built_packages.outputs.cache-hit == ''
run: yarn build
- name: Save SDK version for later
run: |
echo "Saving SDK_VERSION for later"
export SDK_VERSION=$(cat packages/core/src/version.ts | cut -f5 -d' ' | tr -d "'" | tr -d ";")
echo $SDK_VERSION > dist-serverless/version
outputs:
# this needs to be passed on, because the `needs` context only looks at direct ancestors (so steps which depend on
# `job_build` can't see `job_install_deps` and what it returned)
Expand Down Expand Up @@ -228,7 +234,6 @@ jobs:
${{ github.workspace }}/packages/integrations/build/bundles/**
${{ github.workspace }}/packages/tracing/build/bundles/**
${{ github.workspace }}/packages/**/*.tgz
${{ github.workspace }}/packages/serverless/dist-awslambda-layer/*.zip

job_unit_test:
name: Test (Node ${{ matrix.node }})
Expand Down Expand Up @@ -315,10 +320,10 @@ jobs:
uses: actions/checkout@v2
with:
ref: ${{ env.HEAD_COMMIT }}
# TODO: removing `fetch-depth` below seems to have no effect, and the commit which added it had no description,
# so it's not clear why it's necessary. That said, right now ember tests are xfail, so it's a little hard to
# tell if it's safe to remove. Once ember tests are fixed, let's try again with it turned off, and if all goes
# well, we can pull it out.
# TODO: removing `fetch-depth` below seems to have no effect, and the commit which added it had no description,
# so it's not clear why it's necessary. That said, right now ember tests are xfail, so it's a little hard to
# tell if it's safe to remove. Once ember tests are fixed, let's try again with it turned off, and if all goes
# well, we can pull it out.
fetch-depth: 0
- name: Set up Node
uses: actions/setup-node@v1
Expand Down Expand Up @@ -501,3 +506,41 @@ jobs:
run: |
cd packages/node-integration-tests
yarn test

job_build_aws_lambda_layer:
name: Build AWS Lambda Layer
needs: job_build
runs-on: ubuntu-latest
steps:
- name: Check out current commit (${{ env.HEAD_COMMIT }})
uses: actions/checkout@v2
with:
ref: ${{ env.HEAD_COMMIT }}
- name: Set up Node
uses: actions/setup-node@v1
with:
node-version: ${{ env.DEFAULT_NODE_VERSION }}
- name: Check dependency cache
uses: actions/cache@v2
with:
path: ${{ env.CACHED_DEPENDENCY_PATHS }}
key: ${{ needs.job_build.outputs.dependency_cache_key }}
- name: Check build cache
uses: actions/cache@v2
with:
path: ${{ env.CACHED_BUILD_PATHS }}
key: ${{ env.BUILD_CACHE_KEY }}
- name: Get SDK version
run: |
export SDK_VERSION=$(cat dist-serverless/version)
echo "SDK_VERSION=$SDK_VERSION"
echo "SDK_VERSION=$SDK_VERSION" >> $GITHUB_ENV
Comment on lines +536 to +537
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "SDK_VERSION=$SDK_VERSION"
echo "SDK_VERSION=$SDK_VERSION" >> $GITHUB_ENV
echo "SDK_VERSION=$SDK_VERSION" | tee -a $GITHUB_ENV

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tee -a does exactly what you did in 2 lines :)

- uses: actions/upload-artifact@v3
with:
name: ${{ env.HEAD_COMMIT }}
path: |
dist-serverless/*
- uses: getsentry/action-build-aws-lambda-extension@v1
with:
artifact_name: ${{ env.HEAD_COMMIT }}
zip_file_name: sentry-node-serverless-${{ env.SDK_VERSION }}.zip
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ scratch/
*.pyc
*.tsbuildinfo
scenarios/*/dist/
dist-serverless/
# transpiled transformers
jest/transformers/*.js
# node tarballs
Expand Down
1 change: 0 additions & 1 deletion packages/serverless/.gitignore

This file was deleted.

24 changes: 2 additions & 22 deletions packages/serverless/scripts/build-awslambda-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const path = require('path');
const process = require('process');
const fs = require('fs');
const childProcess = require('child_process');

const findUp = require('find-up');
const packList = require('npm-packlist');
Expand Down Expand Up @@ -75,6 +74,7 @@ async function collectPackages(cwd, packages = {}) {
}

async function main() {
const baseDir = path.resolve(__dirname, '../../../')
const serverlessDir = path.resolve(__dirname, '..'); // packages/serverless directory

const cjsBuildDir = path.resolve(serverlessDir, 'build', 'cjs');
Expand All @@ -86,7 +86,7 @@ async function main() {
const packages = await collectPackages(serverlessDir);

// the build directory of the Lambda layer
const layerBuildDir = path.resolve(serverlessDir, 'dist-awslambda-layer');
const layerBuildDir = path.resolve(baseDir, 'dist-serverless');

// the root directory in which the Lambda layer files + dependencies are copied to
// this structure resembles the structure where Lambda expects to find @sentry/serverless
Expand Down Expand Up @@ -170,9 +170,6 @@ async function main() {
}),
);

const version = serverlessPackageJson.version;
const zipFilename = `sentry-node-serverless-${version}.zip`;

// link from `./build/cjs` to `./dist`
// This needs to be done to satisfy the NODE_OPTIONS environment variable path that is set in
// AWS lambda functions when connecting them to Sentry. On initialization, the layer preloads a js
Expand All @@ -184,23 +181,6 @@ async function main() {
} catch (error) {
console.error(error);
}

// remove previously created layer zip
try {
fs.unlinkSync(path.resolve(layerBuildDir, zipFilename));
} catch (error) {
// If the ZIP file hasn't been previously created (e.g. running this script for the first time),
// `unlinkSync` will try to delete a non-existing file. This error is ignored.
}

// create new layer zip
try {
childProcess.execSync(`zip -r ${zipFilename} ${destRootRelative}`, { cwd: layerBuildDir });
} catch (error) {
// The child process timed out or had non-zero exit code.
// The error contains the entire result from `childProcess.spawnSync`.
console.log(error);
}
}

main().then(
Expand Down