This commit is contained in:
Kyle McNally 2023-08-18 17:13:45 -04:00
parent aeee592507
commit 6fc18b6935
5 changed files with 124 additions and 124 deletions

View File

@ -2,37 +2,37 @@ name: AWS Lambda CI
on: on:
push: push:
branches: [ master ] branches: [master]
pull_request: pull_request:
branches: [ master ] branches: [master]
jobs: jobs:
build: build:
name: Build Lambda Layer name: Build Lambda Layer
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Setup Node.js - name: Setup Node.js
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: 18.x node-version: 18.x
- name: Install Packages - name: Install Packages
run: npm ci run: npm ci
- name: Compile Typescript - name: Compile Typescript
run: npm run build run: npm run build
- name: Create Lambda Layer - name: Create Lambda Layer
run: make chromium.zip run: make chromium.zip
- name: Upload Layer Artifact - name: Upload Layer Artifact
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
name: chromium name: chromium
path: chromium.zip path: chromium.zip
execute: execute:
name: Lambda (Node ${{ matrix.version }}.x) name: Lambda (Node ${{ matrix.version }}.x)
@ -46,27 +46,27 @@ jobs:
- 16 - 16
- 18 - 18
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Setup Python - name: Setup Python
uses: actions/setup-python@v4 uses: actions/setup-python@v4
with: with:
python-version: '3.x' python-version: "3.x"
- name: Setup AWS SAM CLI - name: Setup AWS SAM CLI
uses: aws-actions/setup-sam@v2 uses: aws-actions/setup-sam@v2
- name: Download Layer Artifact - name: Download Layer Artifact
uses: actions/download-artifact@v3 uses: actions/download-artifact@v3
with: with:
name: chromium name: chromium
- name: Provision Layer - name: Provision Layer
run: unzip chromium.zip -d _/amazon/code run: unzip chromium.zip -d _/amazon/code
- name: Install test dependencies - name: Install test dependencies
run: npm install --prefix _/amazon/handlers puppeteer-core --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false run: npm install --prefix _/amazon/handlers puppeteer-core --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false
- name: Invoke Lambda on SAM - name: Invoke Lambda on SAM
run: sam local invoke --template _/amazon/template.yml --event _/amazon/events/${{ matrix.event }}.json node${{ matrix.version }} 2>&1 | (grep 'Error' && exit 1 || exit 0) run: sam local invoke --template _/amazon/template.yml --event _/amazon/events/${{ matrix.event }}.json node${{ matrix.version }} 2>&1 | (grep 'Error' && exit 1 || exit 0)

View File

@ -10,71 +10,70 @@ jobs:
name: Build and release name: Build and release
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# Install jq so I can edit package.json from the command line # Install jq so I can edit package.json from the command line
- run: sudo apt-get install jq -y - run: sudo apt-get install jq -y
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Setup Node.js - name: Setup Node.js
uses: actions/setup-node@v3 uses: actions/setup-node@v3
with: with:
node-version: 18.x node-version: 18.x
registry-url: https://registry.npmjs.org/ registry-url: https://registry.npmjs.org/
- run: npm ci - run: npm ci
- run: npm run build - run: npm run build
- name: Release chromium on npmjs - name: Release chromium on npmjs
run: npm publish run: npm publish
env: env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- name: Create Lambda Layer - name: Create Lambda Layer
run: make chromium-${{ github.ref_name }}-layer.zip run: make chromium-${{ github.ref_name }}-layer.zip
# Change the package name to chromium-min, # Change the package name to chromium-min,
# delete the bin folder from the files array # delete the bin folder from the files array
# so that it doesn't publish the binaries to -min # so that it doesn't publish the binaries to -min
- name: Cleanup and prepare for chromium-min - name: Cleanup and prepare for chromium-min
run: | run: |
jq '.name="@sparticuz/chromium-min"' package.json > .package.json jq '.name="@sparticuz/chromium-min"' package.json > .package.json
jq 'del(.files[] | select(. == "bin"))' .package.json > package.json jq 'del(.files[] | select(. == "bin"))' .package.json > package.json
jq '.homepage="https://github.com/Sparticuz/chromium#-min-package"' package.json > .package.json jq '.homepage="https://github.com/Sparticuz/chromium#-min-package"' package.json > .package.json
mv .package.json package.json mv .package.json package.json
rm package-lock.json rm package-lock.json
npm install npm install
- name: Release chromium-min on npmjs - name: Release chromium-min on npmjs
run: npm publish run: npm publish
env: env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- name: Create Chromium Pack - name: Create Chromium Pack
run: | run: |
cd bin cd bin
tar -cvf chromium-${{ github.ref_name }}-pack.tar * tar -cvf chromium-${{ github.ref_name }}-pack.tar *
mv chromium-${{ github.ref_name }}-pack.tar .. mv chromium-${{ github.ref_name }}-pack.tar ..
cd .. cd ..
- name: Upload items to Github Release - name: Upload items to Github Release
uses: ncipollo/release-action@v1.12.0 uses: ncipollo/release-action@v1.12.0
with: with:
tag: ${{ github.ref_name }} tag: ${{ github.ref_name }}
body: | body: |
# [@sparticuz/chromium ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium), [@sparticuz/chromium-min ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium-min) # [@sparticuz/chromium ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium), [@sparticuz/chromium-min ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium-min)
The `chromium-${{ github.ref_name }}-layer.zip` file may be uploaded directly as a layer in AWS Lambda using the following code The `chromium-${{ github.ref_name }}-layer.zip` file may be uploaded directly as a layer in AWS Lambda using the following code
``` ```
bucketName="chromiumUploadBucket" && \ bucketName="chromiumUploadBucket" && \
aws s3 cp chromium-${{ github.ref_name }}-layer.zip "s3://${bucketName}/chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" && \ aws s3 cp chromium-${{ github.ref_name }}-layer.zip "s3://${bucketName}/chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" && \
aws lambda publish-layer-version --layer-name chromium --description "Chromium ${{ github.ref_name }}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" --compatible-runtimes nodejs --compatible-architectures x86_64 aws lambda publish-layer-version --layer-name chromium --description "Chromium ${{ github.ref_name }}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" --compatible-runtimes nodejs --compatible-architectures x86_64
``` ```
The `chromium-${{ github.ref_name }}-pack.tar` file may be uploaded to any https endpoint and the remote location may be used as the `input` variable in the `chromium.executablePath(input)` function. The `chromium-${{ github.ref_name }}-pack.tar` file may be uploaded to any https endpoint and the remote location may be used as the `input` variable in the `chromium.executablePath(input)` function.
artifacts: "chromium-${{ github.ref_name }}-layer.zip,chromium-${{ github.ref_name }}-pack.tar" artifacts: "chromium-${{ github.ref_name }}-layer.zip,chromium-${{ github.ref_name }}-pack.tar"
prerelease: false prerelease: false
draft: true draft: true
generateReleaseNotes: true generateReleaseNotes: true
token: ${{ github.token }} token: ${{ github.token }}
owner: Sparticuz owner: Sparticuz
repo: chromium repo: chromium

View File

@ -1,7 +1,7 @@
const { ok } = require('assert'); const { ok } = require("assert");
const { createHash } = require('crypto'); const { createHash } = require("crypto");
const puppeteer = require("puppeteer-core"); const puppeteer = require("puppeteer-core");
const chromium = require('@sparticuz/chromium'); const chromium = require("@sparticuz/chromium");
exports.handler = async (event, context) => { exports.handler = async (event, context) => {
let browser = null; let browser = null;
@ -15,9 +15,9 @@ exports.handler = async (event, context) => {
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });
const contexts = [ console.log("Chromium verion", await browser.version());
browser.defaultBrowserContext(),
]; const contexts = [browser.defaultBrowserContext()];
while (contexts.length < event.length) { while (contexts.length < event.length) {
contexts.push(await browser.createIncognitoBrowserContext()); contexts.push(await browser.createIncognitoBrowserContext());
@ -27,23 +27,31 @@ exports.handler = async (event, context) => {
const job = event.shift(); const job = event.shift();
const page = await context.newPage(); const page = await context.newPage();
if (job.hasOwnProperty('url') === true) { if (job.hasOwnProperty("url") === true) {
await page.goto(job.url, { waitUntil: ['domcontentloaded', 'load'] }); await page.goto(job.url, { waitUntil: ["domcontentloaded", "load"] });
if (job.hasOwnProperty('expected') === true) { if (job.hasOwnProperty("expected") === true) {
if (job.expected.hasOwnProperty('title') === true) { if (job.expected.hasOwnProperty("title") === true) {
ok(await page.title() === job.expected.title, `Title assertion failed.`); ok(
(await page.title()) === job.expected.title,
`Title assertion failed.`
);
} }
if (job.expected.hasOwnProperty('screenshot') === true) { if (job.expected.hasOwnProperty("screenshot") === true) {
if (job.expected.hasOwnProperty('remove') === true) { if (job.expected.hasOwnProperty("remove") === true) {
await page.evaluate((selector) => { await page.evaluate((selector) => {
document.getElementById(selector).remove(); document.getElementById(selector).remove();
}, job.expected.remove); }, job.expected.remove);
} }
const screenshot = await page.screenshot(); const screenshot = await page.screenshot();
// console.log(screenshot.toString('base64'), createHash('sha1').update(screenshot.toString('base64')).digest('hex')); // console.log(screenshot.toString('base64'), createHash('sha1').update(screenshot.toString('base64')).digest('hex'));
ok(createHash('sha1').update(screenshot.toString('base64')).digest('hex') === job.expected.screenshot, `Screenshot assertion failed.`); ok(
createHash("sha1")
.update(screenshot.toString("base64"))
.digest("hex") === job.expected.screenshot,
`Screenshot assertion failed.`
);
} }
} }
} }

View File

@ -9,11 +9,11 @@ Resources:
layer: layer:
Type: AWS::Serverless::LayerVersion Type: AWS::Serverless::LayerVersion
Properties: Properties:
LayerName: sparticuz-chromium LayerName: sparticuz-chromium
ContentUri: code/ ContentUri: code/
CompatibleRuntimes: CompatibleRuntimes:
- nodejs16.x - nodejs16.x
- nodejs18.x - nodejs18.x
node16: node16:
Type: AWS::Serverless::Function Type: AWS::Serverless::Function

View File

@ -1,17 +1,10 @@
{ {
"extends": [ "extends": ["@tsconfig/node16/tsconfig", "@tsconfig/strictest"],
"@tsconfig/node16/tsconfig",
"@tsconfig/strictest"
],
"compilerOptions": { "compilerOptions": {
"declaration": true, "declaration": true,
"lib": [ "lib": ["dom", "ES2021"],
"dom"
],
"module": "commonjs", "module": "commonjs",
"outDir": "build", "outDir": "build"
}, },
"include": [ "include": ["source"]
"source"
]
} }