Skip to content

Commit

Permalink
move lambda ci here
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Feb 26, 2024
1 parent bd86aba commit 7d2c443
Show file tree
Hide file tree
Showing 12 changed files with 645 additions and 7 deletions.
99 changes: 99 additions & 0 deletions .github/workflows/lambda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Build & Publish Layer
on:
workflow_dispatch:
inputs:
type:
type: choice
description: Type of package to use
options:
- production
- testing
required: true
version:
type: string
description: Version of the package to use
required: true
workflow_call:
inputs:
type:
type: string
description: Type of package to use
required: true
version:
type: string
description: Version of the package to use
required: true
secrets:
GITHUB_TOKEN:
required: true
LAMBDA_PUBLISHER_ARN:
required: true

jobs:
build:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGE_NAME: ${{ inputs.type == 'production' && 'solarwinds-apm' || '@solarwinds/solarwinds-apm' }}
PACKAGE_VERSION: ${{ inputs.version }}
YARN_ENABLE_IMMUTABLE_INSTALLS: false

steps:
- uses: actions/checkout@v4
with:
lfs: true
- uses: actions/setup-node@v4
with:
node-version: 20
- run: corepack enable

- run: yarn install
- run: yarn lambda $PACKAGE_NAME $PACKAGE_VERSION

- uses: actions/upload-artifact@v4
with:
name: layer.zip
path: lambda/layer.zip

publish:
needs: build
permissions:
id-token: write
strategy:
matrix:
region:
- us-east-1
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: layer.zip
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.LAMBDA_PUBLISHER_ARN }}
aws-region: ${{ matrix.region }}

- name: Calculate layer name
run: |
LAYER_NAME="solarwinds-apm-js"
if [[ "${{ inputs.type }}" != "production" ]]; then
LAYER_NAME="${LAYER_NAME}-${{ inputs.type }}"
else
LAYER_NAME="${LAYER_NAME}-$(echo '${{ inputs.version }}' | sed -r 's/\./_/g')"
fi
echo "LAYER_NAME=$LAYER_NAME" >> $GITHUB_ENV
- name: Publish
run: |
LAYER_ARN=$(
aws lambda publish-layer-version \
--layer-name $LAYER_NAME \
--license-info "Apache 2.0" \
--compatible-architectures x86_64 arm64 \
--compatible-runtimes nodejs20.x nodejs16.x nodejs18.x \
--zip-file fileb://layer.zip \
--query 'LayerVersionArn' \
--output text
)
echo "::notice::$LAYER_ARN"
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
jobs:
publish:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -17,3 +19,17 @@ jobs:
- run: yarn publish
env:
YARN_NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Export version
id: version
run: |
VERSION=$(cat packages/solarwinds-apm/package.json | jq -r '.version')
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
lambda:
needs: publish
uses: ./.github/workflows/lambda.yml
with:
type: testing
version: ${{ needs.publish.outputs.version }}
secrets: inherit
16 changes: 16 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ jobs:
runs-on: ubuntu-latest
permissions:
packages: write
outputs:
version: ${{ steps.version.outputs.version }}
env:
YARN_ENABLE_IMMUTABLE_INSTALLS: "false"
YARN_NPM_AUTH_TOKEN: ${{ github.token }}
Expand Down Expand Up @@ -47,3 +49,17 @@ jobs:
yarn lint:fix
- run: yarn publish

- name: Export version
id: version
run: |
VERSION=$(cat packages/solarwinds-apm/package.json | jq -r '.version')
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
lambda:
needs: publish
uses: ./.github/workflows/lambda.yml
with:
type: testing
version: ${{ needs.publish.outputs.version }}
secrets: inherit
1 change: 1 addition & 0 deletions lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layer.zip
14 changes: 14 additions & 0 deletions lambda/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
nodeLinker: node-modules
supportedArchitectures:
os:
- linux
cpu:
- arm64
- x64
libc:
- glibc
npmScopes:
solarwinds:
npmRegistryServer: https://npm.pkg.github.com
npmAuthToken: ${GITHUB_TOKEN:-GITHUB_TOKEN}
npmAlwaysAuth: true
11 changes: 11 additions & 0 deletions lambda/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@solarwinds-apm/lambda-shim",
"private": true,
"packageManager": "yarn@4.0.2",
"dependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.46.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.46.0",
"{{name}}": "{{version}}"
}
}
1 change: 1 addition & 0 deletions lambda/shim.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("{{name}}");
6 changes: 6 additions & 0 deletions lambda/wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

export NODE_OPTIONS="${NODE_OPTIONS} --require /opt/solarwinds-apm/shim.cjs"
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc"

exec "$@"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build": "turbo run build --continue",
"docker": "node scripts/docker.js",
"example": "node scripts/example.js",
"lambda": "node scripts/lambda.js",
"lint": "prettier --check *.json *.md .github && turbo run lint --continue",
"lint:fix": "prettier --write *.json *.md .github && turbo run lint:fix --continue",
"publish": "turbo run build && turbo run lint && turbo run publish",
Expand Down
103 changes: 103 additions & 0 deletions scripts/lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023-2024 SolarWinds Worldwide, LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

const { execSync } = require("node:child_process")
const {
cpSync,
createWriteStream,
readdirSync,
readFileSync,
rmSync,
writeFileSync,
} = require("node:fs")
const { argv } = require("node:process")
const archiver = require("archiver")
const ora = require("ora")

const [name, version] = argv.slice(2)

const rm = (...args) => {
try {
rmSync(...args)
} catch {
// ignore
}
}

const replace = (file) => {
let contents = readFileSync(file, { encoding: "utf-8" })
contents = contents.replace("{{name}}", name).replace("{{version}}", version)
writeFileSync(file, contents)
}

rm("lambda/layer.zip")
rm("node_modules/.lambda", { recursive: true })
cpSync("lambda", "node_modules/.lambda", { recursive: true })
replace("node_modules/.lambda/package.json")
replace("node_modules/.lambda/shim.cjs")

execSync("touch yarn.lock && yarn install", {
cwd: "node_modules/.lambda",
env: { ...process.env, NODE_ENV: "production" },
stdio: "inherit",
})

const scope = `node_modules/.lambda/node_modules/${
name === "solarwinds-apm" ? "@solarwinds-apm" : "@solarwinds"
}`
for (const entry of readdirSync(scope)) {
if (entry.includes("bindings-") && !entry.includes("serverless")) {
rmSync(`${scope}/${entry}`, { recursive: true })
}
}

const spinner = ora("building layer")

const archive = archiver("zip", { zlib: { level: 9 } })
archive
.on("error", (err) => {
spinner.fail(err.message)
throw err
})
.on("warning", (warn) => {
spinner.fail(warn.message)
throw warn
})
.on("entry", (e) => {
spinner.text = e.name
spinner.render()
})

const out = createWriteStream("lambda/layer.zip")
out.on("error", (err) => {
spinner.fail(err.message)
throw err
})
archive.pipe(out)

archive.directory(
"node_modules/.lambda/node_modules/",
"solarwinds-apm/node_modules/",
)
archive.file("node_modules/.lambda/shim.cjs", {
name: "solarwinds-apm/shim.cjs",
})
archive.file("node_modules/.lambda/wrapper", {
name: "solarwinds-apm/wrapper",
mode: 0o755,
})

archive.finalize().then(() => spinner.succeed("layer built"))
2 changes: 2 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
},
"devDependencies": {
"@solarwinds-apm/eslint-config": "workspace:^",
"archiver": "^6.0.1",
"eslint": "^8.50.0",
"ora": "^5.4.1",
"prettier": "^3.0.3"
}
}
Loading

0 comments on commit 7d2c443

Please sign in to comment.