Skip to content

Build Layers

Build Layers #16

Workflow file for this run

name: Build Layers
on:
push:
branches: [master]
pull_request:
branches: [master]
schedule:
- cron: '0 16 * * *'
workflow_dispatch:
inputs:
sharp_version:
description: 'Sharp version to build'
required: false
default: 'latest'
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
strategy:
matrix:
arch: [arm64, x64, all]
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22
- name: Install esbuild
run: npm i -g esbuild@latest
- name: Install sharp (${{ matrix.arch }})
run: |
VERSION="${{ github.event.inputs.sharp_version }}"
if [ "$VERSION" = "latest" ]; then
if [ "${{ matrix.arch }}" = "all" ]; then
npm i --os=linux --cpu=x64 --libc=glibc sharp
npm i --os=linux --cpu=arm64 --libc=glibc sharp
else
npm i --save-exact --os=linux --cpu=${{ matrix.arch }} --libc=glibc sharp
fi
else
if [ "${{ matrix.arch }}" = "all" ]; then
npm i --os=linux --cpu=x64 --libc=glibc sharp@$VERSION
npm i --os=linux --cpu=arm64 --libc=glibc sharp@$VERSION
else
npm i --save-exact --os=linux --cpu=${{ matrix.arch }} --libc=glibc sharp@$VERSION
fi
fi
- name: Extract Sharp version
id: version
uses: notiz-dev/github-action-json-property@release
with:
path: 'package.json'
prop_path: 'dependencies.sharp'
- name: esbuild bundle
run: |
esbuild --bundle ./node_modules/sharp/ \
--outfile=index.js \
--minify --format=cjs --platform=node
- name: Package Lambda layer (${{ matrix.arch }})
run: |
mkdir -p nodejs/node_modules/sharp/lib
mv node_modules/sharp/package.json nodejs/node_modules/sharp/
mv index.js nodejs/node_modules/sharp/lib/
mv node_modules/sharp/lib/index.d.ts nodejs/node_modules/sharp/lib/
mv node_modules/sharp/LICENSE nodejs/node_modules/sharp/
mv node_modules/@img nodejs/node_modules/ || true
zip -r release-${{ matrix.arch }} nodejs
- name: Clean up
run: rm -rf nodejs node_modules
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: sharp-${{ matrix.arch }}
path: release-${{ matrix.arch }}.zip
test:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Download x64 artifact
uses: actions/download-artifact@v4
with:
name: sharp-x64
- name: Run test for x64
run: |
unzip release-x64.zip
cp test.mjs nodejs/test.mjs
node nodejs/test.mjs
if [ ! -f test.png ]; then exit 1 ; fi
publish:
needs: [build, test]
if: ${{ github.event_name != 'pull_request' }}
runs-on: ubuntu-latest
# Add permissions if needed for writing to version.txt and creating releases
permissions:
contents: write # Needed for committing version.txt and creating releases
steps:
- name: Checkout
uses: actions/checkout@v3
with:
# Fetch depth 0 to get all history for version comparison if needed,
# though reading version.txt is simpler here.
fetch-depth: 0
- name: Download artifacts
uses: actions/download-artifact@v4
with:
path: artifacts # Download all artifacts to ./artifacts/<artifact-name>
- name: Unzip sharp-x64 to access package.json
run: |
unzip artifacts/sharp-x64/release-x64.zip -d artifacts/sharp-x64
- name: Read previous version from version.txt
id: previous
run: |
# Handle case where version.txt might not exist yet
if [[ -f version.txt ]]; then
echo "sharpver=$(cat version.txt)" >> $GITHUB_ENV
else
echo "sharpver=0.0.0" >> $GITHUB_ENV # Default if file doesn't exist
fi
# Continue even if version.txt doesn't exist on first run
continue-on-error: true
- name: Get new sharp version from downloaded artifact
id: version
uses: notiz-dev/github-action-json-property@release
with:
# Path to the package.json within the unzipped artifact
path: 'artifacts/sharp-x64/nodejs/node_modules/sharp/package.json'
prop_path: 'version'
- name: Check if new version is a pre-release
id: prerelease # Give the step an ID if you wanted to use outputs instead of env
run: |
SHARP_VERSION="${{ steps.version.outputs.prop }}"
if [[ "$SHARP_VERSION" == *-* ]]; then
echo "Detected pre-release version: $SHARP_VERSION"
echo "IS_PRERELEASE=true" >> $GITHUB_ENV
else
echo "Detected stable version: $SHARP_VERSION"
echo "IS_PRERELEASE=false" >> $GITHUB_ENV
fi
# --- Optional: Skip logic based on version comparison ---
# This logic prevents re-releasing the *same* version.
# It also only updates version.txt for *stable* releases.
- name: Check if release should be skipped
id: skip_check
run: |
echo "Previous version: ${{ env.sharpver }}"
echo "New version: ${{ steps.version.outputs.prop }}"
echo "Is pre-release: ${{ env.IS_PRERELEASE }}"
if [[ "${{ env.sharpver }}" == "${{ steps.version.outputs.prop }}" ]]; then
echo "Version hasn't changed (${{ steps.version.outputs.prop }}). Skipping release."
echo "SKIP_RELEASE=true" >> $GITHUB_ENV
else
echo "Version changed or first run. Proceeding with release checks."
echo "SKIP_RELEASE=false" >> $GITHUB_ENV
fi
# --- Update version.txt only for new STABLE releases ---
- name: Update version.txt for new stable release
# Run only if it's NOT a pre-release AND the version has changed
if: steps.skip_check.outputs.SKIP_RELEASE == 'false' && env.IS_PRERELEASE == 'false'
run: |
echo "Updating version.txt to ${{ steps.version.outputs.prop }}"
echo "${{ steps.version.outputs.prop }}" > version.txt
- name: Commit version.txt update
# Run only if version.txt was updated (new stable release)
if: steps.skip_check.outputs.SKIP_RELEASE == 'false' && env.IS_PRERELEASE == 'false'
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "Update sharp to ${{ steps.version.outputs.prop }}"
file_pattern: version.txt
# Add push options if your branch protection requires it
# push_options: '--force'
# --- Create GitHub Release ---
- name: Create GitHub Release
# Run only if the version has actually changed (don't re-release the same version)
if: steps.skip_check.outputs.SKIP_RELEASE == 'false'
uses: softprops/action-gh-release@v1
with:
files: artifacts/**/*.zip # Upload all zip files from the artifacts subdirectories
body: | # Use a multi-line body for better formatting
Sharp version ${{ steps.version.outputs.prop }} Lambda Layer.
Architectures included:
- arm64
- x64
- all (combined node_modules for arm64 & x64)
tag_name: v${{ steps.version.outputs.prop }} # Add 'v' prefix to tag, common practice
name: Sharp Layer v${{ steps.version.outputs.prop }} # Release title
# Use the environment variable set in the 'Check if new version is a pre-release' step
prerelease: ${{ env.IS_PRERELEASE }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Explicitly pass the token
# --- Notify Discord ---
- name: Notify Discord
# Notify only if a new release was created
if: steps.skip_check.outputs.SKIP_RELEASE == 'false'
run: |
# Construct message based on whether it's a pre-release or stable release
if [[ "${{ env.IS_PRERELEASE }}" == "true" ]]; then
RELEASE_TYPE="Pre-release"
else
RELEASE_TYPE="Stable release"
fi
MESSAGE_CONTENT="🧠 Sharp Lambda Layer updated!\\n${RELEASE_TYPE}: \`${{ steps.version.outputs.prop }}\`\\n<https://github.com/${{ github.repository }}/releases/tag/v${{ steps.version.outputs.prop }}>"
curl -X POST -H "Content-Type: application/json" \
-d "{\"content\":\"${MESSAGE_CONTENT}\"}" \
"${{ secrets.DISCORD_WEBHOOK_URL }}"