Skip to content

Commit 3d84e36

Browse files
committed
updates to the readme, inclusion of both x64 and arm64 - debug
1 parent fed80a1 commit 3d84e36

File tree

2 files changed

+110
-82
lines changed

2 files changed

+110
-82
lines changed
Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,108 @@
1-
name: Build Sharp Lambda Layers
1+
name: Build Sharp Lambda Layer
22

33
on:
44
push:
5-
branches: [main]
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
610
workflow_dispatch:
711

812
jobs:
9-
build-layers:
13+
setup:
14+
runs-on: ubuntu-latest
15+
outputs:
16+
sharp_version: ${{ steps.vars.outputs.sharp_version }}
17+
release_exists: ${{ steps.vars.outputs.release_exists }}
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Install jq
25+
run: sudo apt-get update && sudo apt-get install -y jq
26+
27+
- name: Get Sharp Version and Check Release
28+
id: vars
29+
run: |
30+
sharp_version=$(jq -r '.packages."node_modules/sharp".version' package-lock.json)
31+
if [ -z "$sharp_version" ] || [ "$sharp_version" == "null" ]; then
32+
echo "Error: Could not extract sharp version"
33+
exit 1
34+
fi
35+
echo "sharp_version=$sharp_version" >> $GITHUB_OUTPUT
36+
37+
release_exists="false"
38+
if git rev-parse "v${sharp_version}" >/dev/null 2>&1 || git ls-remote --tags origin | grep -q "refs/tags/v${sharp_version}$"; then
39+
release_exists="true"
40+
fi
41+
echo "release_exists=$release_exists" >> $GITHUB_OUTPUT
42+
43+
build:
44+
needs: setup
1045
runs-on: ubuntu-latest
1146
strategy:
1247
matrix:
1348
include:
14-
- arch: x64
15-
image: public.ecr.aws/lambda/nodejs:20
16-
- arch: arm64
17-
image: public.ecr.aws/lambda/nodejs:20-arm64
18-
49+
- platform: linux/amd64
50+
arch: x64
51+
- platform: linux/arm64
52+
arch: arm64
1953
steps:
20-
- name: Checkout repo
54+
- name: Checkout
2155
uses: actions/checkout@v4
2256

23-
- name: Build & zip Sharp layer (${{ matrix.arch }})
24-
run: |
25-
# mount workspace into the Lambda build image
26-
docker run --rm \
27-
-v "${{ github.workspace }}":/project \
28-
-w /project \
29-
${{ matrix.image }} \
30-
bash -exlc "
31-
# install only runtime deps for Sharp
32-
npm ci --no-optional --no-audit --production --arch=${{ matrix.arch }} --platform=linux
33-
34-
# stage into the Lambda layer layout
35-
mkdir -p layer/nodejs/node_modules
36-
cp -R node_modules/sharp layer/nodejs/node_modules/
57+
- name: Set up QEMU
58+
uses: docker/setup-qemu-action@v3
59+
60+
- name: Set up Docker Buildx
61+
uses: docker/setup-buildx-action@v3
3762

38-
# zip it
39-
(cd layer && zip -r ../sharp-layer-${{ matrix.arch }}.zip .)
40-
"
63+
- name: Build Docker Image
64+
uses: docker/build-push-action@v6
65+
with:
66+
context: .
67+
file: ./Dockerfile
68+
platforms: ${{ matrix.platform }}
69+
build-args: |
70+
TARGET_ARCH=${{ matrix.arch }}
71+
load: true
72+
tags: sharp-layer:${{ matrix.arch }}
4173

42-
- name: Upload artifacts
74+
- name: Create dist Directory
75+
run: mkdir -p dist
76+
77+
- name: Copy Artifact
78+
run: |
79+
docker run --rm -v "${{ github.workspace }}/dist:/output" \
80+
sharp-layer:${{ matrix.arch }} \
81+
cp /build/dist/sharp-layer-${{ matrix.arch }}.zip /output/
82+
83+
- name: Upload Artifact
4384
uses: actions/upload-artifact@v4
4485
with:
4586
name: sharp-layer-${{ matrix.arch }}
46-
path: sharp-layer-${{ matrix.arch }}.zip
87+
path: dist/sharp-layer-${{ matrix.arch }}.zip
4788

4889
release:
49-
needs: build-layers
50-
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
90+
needs: [setup, build]
5191
runs-on: ubuntu-latest
52-
92+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
5393
steps:
54-
- name: Download Sharp zips
94+
- name: Download Artifacts
5595
uses: actions/download-artifact@v4
5696
with:
5797
path: dist
5898

59-
- name: Create or update GitHub Release
99+
- name: Create GitHub Release
60100
uses: svenstaro/upload-release-action@v2
61101
with:
62102
repo_token: ${{ secrets.GITHUB_TOKEN }}
63-
tag: v${{ github.sha }}
64-
release_name: Sharp Lambda Layer
65-
file: dist/*.zip
103+
tag: v${{ needs.setup.outputs.sharp_version }}
104+
release_name: Sharp v${{ needs.setup.outputs.sharp_version }} Layer
105+
file: dist/sharp-layer-*/*.zip
66106
file_glob: true
67107
overwrite: true
68-
body: |
69-
AWS Lambda layers for Sharp (x64 and arm64).
108+
body: "AWS Lambda Layer for Sharp v${{ needs.setup.outputs.sharp_version }} (x64 and arm64)."

Dockerfile

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,48 @@
1-
# Use a multi-arch base image
2-
FROM public.ecr.aws/sam/build-nodejs20.x:latest
1+
# Use Amazon Linux 2 base image to match AWS Lambda runtime
2+
FROM public.ecr.aws/lambda/nodejs:20
33

44
WORKDIR /build
55

6-
# Declare build argument for target architecture
6+
# Declare build arguments
77
ARG TARGET_ARCH
8-
ARG TARGET_PLATFORM=linux # Default platform
8+
ARG TARGET_PLATFORM=linux
99

10-
# Verify the build environment architecture (optional debug step)
11-
RUN echo "Building on architecture: $(uname -m)"
12-
RUN echo "TARGET_ARCH argument: ${TARGET_ARCH}"
13-
RUN echo "TARGET_PLATFORM argument: ${TARGET_PLATFORM}"
10+
# Install build dependencies for sharp and libvips
11+
RUN yum update -y && \
12+
yum install -y gcc-c++ make python3 pkgconfig tar gzip && \
13+
# Install libvips and its dependencies
14+
yum install -y libvips42 libvips-devel && \
15+
# Clean up to reduce layer size
16+
yum clean all && rm -rf /var/cache/yum
1417

15-
# Copy necessary files
18+
# Verify build environment
19+
RUN echo "Building for architecture: ${TARGET_ARCH}" && \
20+
echo "Target platform: ${TARGET_PLATFORM}"
21+
22+
# Copy package files
1623
COPY package.json package-lock.json webpack.config.js ./
17-
# Copy other potential source files if needed by webpack or sharp install
18-
# COPY src ./src
1924

20-
# Install dependencies
21-
RUN npm --no-optional --no-audit --progress=false --arch=${TARGET_ARCH} --platform=${TARGET_PLATFORM} install
25+
# Install dependencies with architecture-specific flags
26+
RUN npm install --no-optional --no-audit --progress=false \
27+
--arch=${TARGET_ARCH} --platform=${TARGET_PLATFORM}
28+
29+
# Debug: List sharp directory contents
30+
RUN echo "Listing /build/node_modules/sharp/:" && \
31+
ls -lR /build/node_modules/sharp/ || echo "Sharp directory listing failed"
2232

23-
# --- TEMPORARY DEBUG STEP ---
24-
# List the FULL contents of the sharp directory AFTER install
25-
RUN echo ">>> Listing FULL contents of /build/node_modules/sharp/ after install <<<" && \
26-
ls -lR /build/node_modules/sharp/ || echo "Sharp directory listing failed?"
27-
# --- END TEMPORARY DEBUG STEP ---
33+
# Run Webpack to package the layer
34+
RUN TARGET_ARCH=${TARGET_ARCH} npm run build
2835

29-
# Run webpack (this will likely still error, but we need the listing first)
30-
RUN TARGET_ARCH=${TARGET_ARCH} node ./node_modules/webpack/bin/webpack.js
36+
# Debug: List dist directory contents
37+
RUN echo "Listing /build/dist/:" && \
38+
ls -lR /build/dist/
3139

32-
# --- Smoke Test ---
33-
RUN echo ">>> Running smoke test on packaged layer..." && \
34-
# Try to execute the node command in a subshell group ()
35-
( \
40+
# Smoke test to verify sharp
41+
RUN echo "Running smoke test..." && \
3642
node -e " \
37-
const sharp = require('/build/dist/nodejs/node_modules/sharp'); \
38-
console.log('>>> SUCCESS: require(\'sharp\') loaded.'); \
39-
if (sharp && sharp.versions) { \
43+
const sharp = require('/build/dist/nodejs/node_modules/sharp'); \
44+
console.log('Sharp loaded successfully'); \
4045
console.log('Sharp versions:', sharp.versions); \
41-
} else { \
42-
console.log('Sharp loaded, but versions property not found.'); \
43-
} \
44-
" \
45-
) || \
46-
# If the node command fails (exits non-zero), execute this block
47-
( \
48-
echo ">>> FAILURE: Node smoke test failed!" && \
49-
echo ">>> Listing /build/dist/nodejs/node_modules/sharp contents on failure:" && \
50-
ls -lR /build/dist/nodejs/node_modules/sharp && \
51-
exit 1 \
52-
)
53-
# --- End Smoke Test ---
54-
55-
# Simple test to ensure sharp loads correctly for the target architecture
56-
# Note: This might fail now if webpack failed, run it only after successful webpack
57-
# RUN node -e "console.log(require('sharp')('./package.json'))"
46+
" || (echo "Smoke test failed!" && ls -lR /build/dist/nodejs/node_modules/sharp && exit 1)
5847

5948
# No entrypoint needed

0 commit comments

Comments
 (0)