Skip to content

Commit 8a4039d

Browse files
antonpirkerLms24
andauthored
ref(serverless): Use GitHub action to zip lambda layer (second try) (#5093)
* Removed zipping from build script * Assemble Lambda Layer zip using GitHub action. Co-authored-by: Lukas Stracke <[email protected]>
1 parent 5c34152 commit 8a4039d

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

.github/workflows/build.yml

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ env:
3131
${{ github.workspace }}/packages/ember/*.d.ts
3232
${{ github.workspace }}/packages/ember/instance-initializers
3333
${{ github.workspace }}/packages/gatsby/*.d.ts
34-
${{ github.workspace }}/packages/serverless/dist-awslambda-layer/*.zip
34+
${{ github.workspace }}/packages/core/src/version.ts
35+
${{ github.workspace }}/dist-serverless
3536
3637
BUILD_CACHE_KEY: ${{ github.event.inputs.commit || github.sha }}
3738

@@ -99,6 +100,11 @@ jobs:
99100
# this file) to a constant and skip rebuilding all of the packages each time CI runs.
100101
if: steps.cache_built_packages.outputs.cache-hit == ''
101102
run: yarn build
103+
- name: Save SDK version for later
104+
run: |
105+
echo "Saving SDK_VERSION for later"
106+
export SDK_VERSION=$(cat packages/core/src/version.ts | cut -f5 -d' ' | tr -d "'" | tr -d ";")
107+
echo $SDK_VERSION > dist-serverless/version
102108
outputs:
103109
# this needs to be passed on, because the `needs` context only looks at direct ancestors (so steps which depend on
104110
# `job_build` can't see `job_install_deps` and what it returned)
@@ -228,7 +234,6 @@ jobs:
228234
${{ github.workspace }}/packages/integrations/build/bundles/**
229235
${{ github.workspace }}/packages/tracing/build/bundles/**
230236
${{ github.workspace }}/packages/**/*.tgz
231-
${{ github.workspace }}/packages/serverless/dist-awslambda-layer/*.zip
232237
233238
job_unit_test:
234239
name: Test (Node ${{ matrix.node }})
@@ -315,10 +320,10 @@ jobs:
315320
uses: actions/checkout@v2
316321
with:
317322
ref: ${{ env.HEAD_COMMIT }}
318-
# TODO: removing `fetch-depth` below seems to have no effect, and the commit which added it had no description,
319-
# so it's not clear why it's necessary. That said, right now ember tests are xfail, so it's a little hard to
320-
# tell if it's safe to remove. Once ember tests are fixed, let's try again with it turned off, and if all goes
321-
# well, we can pull it out.
323+
# TODO: removing `fetch-depth` below seems to have no effect, and the commit which added it had no description,
324+
# so it's not clear why it's necessary. That said, right now ember tests are xfail, so it's a little hard to
325+
# tell if it's safe to remove. Once ember tests are fixed, let's try again with it turned off, and if all goes
326+
# well, we can pull it out.
322327
fetch-depth: 0
323328
- name: Set up Node
324329
uses: actions/setup-node@v1
@@ -501,3 +506,41 @@ jobs:
501506
run: |
502507
cd packages/node-integration-tests
503508
yarn test
509+
510+
job_build_aws_lambda_layer:
511+
name: Build AWS Lambda Layer
512+
needs: job_build
513+
runs-on: ubuntu-latest
514+
steps:
515+
- name: Check out current commit (${{ env.HEAD_COMMIT }})
516+
uses: actions/checkout@v2
517+
with:
518+
ref: ${{ env.HEAD_COMMIT }}
519+
- name: Set up Node
520+
uses: actions/setup-node@v1
521+
with:
522+
node-version: ${{ env.DEFAULT_NODE_VERSION }}
523+
- name: Check dependency cache
524+
uses: actions/cache@v2
525+
with:
526+
path: ${{ env.CACHED_DEPENDENCY_PATHS }}
527+
key: ${{ needs.job_build.outputs.dependency_cache_key }}
528+
- name: Check build cache
529+
uses: actions/cache@v2
530+
with:
531+
path: ${{ env.CACHED_BUILD_PATHS }}
532+
key: ${{ env.BUILD_CACHE_KEY }}
533+
- name: Get SDK version
534+
run: |
535+
export SDK_VERSION=$(cat dist-serverless/version)
536+
echo "SDK_VERSION=$SDK_VERSION"
537+
echo "SDK_VERSION=$SDK_VERSION" >> $GITHUB_ENV
538+
- uses: actions/upload-artifact@v3
539+
with:
540+
name: ${{ env.HEAD_COMMIT }}
541+
path: |
542+
dist-serverless/*
543+
- uses: getsentry/action-build-aws-lambda-extension@v1
544+
with:
545+
artifact_name: ${{ env.HEAD_COMMIT }}
546+
zip_file_name: sentry-node-serverless-${{ env.SDK_VERSION }}.zip

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ scratch/
1212
*.pyc
1313
*.tsbuildinfo
1414
scenarios/*/dist/
15+
dist-serverless/
1516
# transpiled transformers
1617
jest/transformers/*.js
1718
# node tarballs

packages/serverless/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/serverless/scripts/build-awslambda-layer.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
const path = require('path');
33
const process = require('process');
44
const fs = require('fs');
5-
const childProcess = require('child_process');
65

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

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

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

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

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

173-
const version = serverlessPackageJson.version;
174-
const zipFilename = `sentry-node-serverless-${version}.zip`;
175-
176173
// link from `./build/cjs` to `./dist`
177174
// This needs to be done to satisfy the NODE_OPTIONS environment variable path that is set in
178175
// AWS lambda functions when connecting them to Sentry. On initialization, the layer preloads a js
@@ -184,23 +181,6 @@ async function main() {
184181
} catch (error) {
185182
console.error(error);
186183
}
187-
188-
// remove previously created layer zip
189-
try {
190-
fs.unlinkSync(path.resolve(layerBuildDir, zipFilename));
191-
} catch (error) {
192-
// If the ZIP file hasn't been previously created (e.g. running this script for the first time),
193-
// `unlinkSync` will try to delete a non-existing file. This error is ignored.
194-
}
195-
196-
// create new layer zip
197-
try {
198-
childProcess.execSync(`zip -r ${zipFilename} ${destRootRelative}`, { cwd: layerBuildDir });
199-
} catch (error) {
200-
// The child process timed out or had non-zero exit code.
201-
// The error contains the entire result from `childProcess.spawnSync`.
202-
console.log(error);
203-
}
204184
}
205185

206186
main().then(

0 commit comments

Comments
 (0)