Build Layers #78
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
permissions: | |
contents: write # To commit version.txt | |
pull-requests: read # Needed by gh release view potentially? Better safe than sorry. | |
# No specific permission needed for reading public repo releases via gh | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Download artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
path: artifacts | |
- 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: | | |
if [[ -f version.txt ]]; then | |
echo "sharpver=$(cat version.txt)" >> $GITHUB_ENV | |
else | |
echo "sharpver=0.0.0" >> $GITHUB_ENV | |
fi | |
continue-on-error: true | |
- name: Get new sharp version from downloaded artifact | |
id: version | |
uses: notiz-dev/github-action-json-property@release | |
with: | |
path: 'artifacts/sharp-x64/nodejs/node_modules/sharp/package.json' | |
prop_path: 'version' | |
# --- NEW: Check GitHub Release status using gh CLI --- | |
- name: Check GitHub Release status for v${{ steps.version.outputs.prop }} | |
id: gh_release_check | |
env: | |
# Provide the GITHUB_TOKEN to the gh CLI | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# Extract the version number for easier use | |
SHARP_VERSION: ${{ steps.version.outputs.prop }} | |
run: | | |
# Construct the tag name (usually 'v' + version) | |
TAG_NAME="v$SHARP_VERSION" | |
echo "Checking GitHub release status for tag: $TAG_NAME" | |
# Use gh release view to get the isPrerelease field. | |
# -q filters the JSON output. | |
# We redirect stderr to /dev/null to suppress errors if the release/tag doesn't exist. | |
# We add || echo "error" to handle the case where the gh command fails (e.g., tag not found) | |
IS_PRERELEASE_STATUS=$(gh release view "$TAG_NAME" --repo lovell/sharp --json isPrerelease -q .isPrerelease 2>/dev/null || echo "error") | |
echo "GitHub API check result: $IS_PRERELEASE_STATUS" | |
if [[ "$IS_PRERELEASE_STATUS" == "true" ]]; then | |
echo "GitHub marks release $TAG_NAME as pre-release." | |
echo "IS_PRERELEASE=true" >> $GITHUB_ENV | |
elif [[ "$IS_PRERELEASE_STATUS" == "false" ]]; then | |
echo "GitHub marks release $TAG_NAME as stable (not pre-release)." | |
echo "IS_PRERELEASE=false" >> $GITHUB_ENV | |
else | |
# Handle error or tag not found case - assume stable? Or fail? | |
# Assuming stable if tag not found on GitHub releases is safer | |
# as the package *was* successfully installed from npm. | |
echo "WARN: Could not determine release status for tag $TAG_NAME from GitHub API (may not exist as a release yet or an error occurred). Assuming stable." | |
echo "IS_PRERELEASE=false" >> $GITHUB_ENV | |
# Alternatively, you could fail the job here if needed: | |
# echo "ERROR: Could not determine release status for tag $TAG_NAME from GitHub API." | |
# exit 1 | |
fi | |
# --- Skip check logic (no changes needed here) --- | |
- 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 (from GitHub): ${{ env.IS_PRERELEASE }}" | |
if [[ "${{ env.sharpver }}" == "${{ steps.version.outputs.prop }}" ]]; then | |
echo "Version hasn't changed (${{ steps.version.outputs.prop }}). Skipping release." | |
# Use set-output for cross-platform compatibility if needed, but GITHUB_ENV works here | |
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 logic (no changes needed here) --- | |
- name: Update version.txt for new stable release | |
if: env.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 | |
if: env.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 | |
# --- Create GitHub Release logic (no changes needed here, uses IS_PRERELEASE env var) --- | |
- name: Create GitHub Release | |
if: env.SKIP_RELEASE == 'false' | |
uses: softprops/action-gh-release@v1 | |
with: | |
files: artifacts/**/*.zip | |
body: | | |
Sharp version ${{ steps.version.outputs.prop }} Lambda Layer. | |
Installed from npm, GitHub release status checked. | |
Architectures included: | |
- arm64 | |
- x64 | |
- all (combined node_modules for arm64 & x64) | |
tag_name: v${{ steps.version.outputs.prop }} | |
name: Sharp Layer v${{ steps.version.outputs.prop }} | |
# Uses the IS_PRERELEASE variable set by the gh_release_check step | |
prerelease: ${{ env.IS_PRERELEASE }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# --- Notify Discord logic (no changes needed here) --- | |
- name: Notify Discord | |
if: env.SKIP_RELEASE == 'false' | |
run: | | |
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 }}" |