77 pull_request :
88 branches :
99 - main
10- # schedule:
11- # - cron: '0 0 * * *'
10+ workflow_dispatch : # Allows manual triggering
1211
1312jobs :
14- build :
13+ # Setup job to determine Sharp version and check release existence once
14+ setup :
1515 runs-on : ubuntu-latest
16+ outputs :
17+ sharp_version : ${{ steps.vars.outputs.sharp_version }}
18+ release_exists : ${{ steps.vars.outputs.release_exists }}
1619 steps :
1720 - name : Checkout
18- uses : actions/checkout@v3
21+ uses : actions/checkout@v4
1922 with :
20- fetch-depth : 0
21- - name : Variables
23+ fetch-depth : 0 # Needed for tag checking
24+
25+ - name : Install jq
26+ run : sudo apt-get update && sudo apt-get install -y jq
27+
28+ - name : Get Sharp Version and Check Release
2229 id : vars
2330 run : |
2431 content=$(cat ./package-lock.json)
2532 sharp_version=$(echo $content | jq -r '.packages."node_modules/sharp".version')
26- echo "sharp_version=$sharp_version"
33+ if [ -z "$sharp_version" ] || [ "$sharp_version" == "null" ]; then
34+ echo "Error: Could not extract sharp version from package-lock.json"
35+ exit 1
36+ fi
37+ echo "Determined Sharp version: $sharp_version"
2738 echo "sharp_version=$sharp_version" >> $GITHUB_OUTPUT
28- release_exists="true"
29- git show-ref --tags --quiet --verify -- "refs/tags/$sharp_version" || release_exists="false"
30- echo "release_exists=$release_exists"
39+
40+ release_exists="false"
41+ # Check if tag exists locally first (faster)
42+ if git rev-parse "v${sharp_version}" >/dev/null 2>&1; then
43+ release_exists="true"
44+ else
45+ # If not local, check remote tags
46+ git ls-remote --tags origin | grep -q "refs/tags/v${sharp_version}$" && release_exists="true" || release_exists="false"
47+ fi
48+
49+ echo "Release tag v${sharp_version} exists: $release_exists"
3150 echo "release_exists=$release_exists" >> $GITHUB_OUTPUT
32- echo "sharp_version=$sharp_version" >> $GITHUB_OUTPUT
33- - name : Build
34- id : docker_build
35- uses : docker/build-push-action@v4
51+
52+
53+ # Build job using matrix for architectures
54+ build :
55+ needs : setup
56+ runs-on : ubuntu-latest
57+ strategy :
58+ matrix :
59+ # Define the platforms and corresponding simple architecture names
60+ include :
61+ - platform : linux/amd64
62+ arch : x64
63+ - platform : linux/arm64
64+ arch : arm64
65+ steps :
66+ - name : Checkout
67+ uses : actions/checkout@v4
68+
69+ - name : Set up QEMU
70+ uses : docker/setup-qemu-action@v3
71+
72+ - name : Set up Docker Buildx
73+ uses : docker/setup-buildx-action@v3
74+
75+ - name : Build Docker image for ${{ matrix.arch }}
76+ uses : docker/build-push-action@v6
3677 with :
3778 context : .
3879 file : ./Dockerfile
39- tags : sharp-aws-lambda-layer:dev
40- - name : Copy artifacts
41- run : docker run -v "${{ github.workspace }}/dist":/dist sharp-aws-lambda-layer:dev
42- - name : Create release
80+ # Specify the platform for the build
81+ platforms : ${{ matrix.platform }}
82+ # Pass the architecture name as a build argument
83+ build-args : |
84+ TARGET_ARCH=${{ matrix.arch }}
85+ # Load the image into the local Docker daemon to extract files
86+ # Alternatively, use 'outputs: type=local,dest=output-docker'
87+ # then copy from 'output-docker/dist/...' in the next step.
88+ # Loading is simpler here if we just need the zip.
89+ load : true
90+ tags : sharp-aws-lambda-layer:${{ matrix.arch }}-dev # Tag with arch
91+
92+ - name : Create dist directory
93+ run : mkdir -p dist # Create directory if it doesn't exist
94+
95+ - name : Copy artifact (${{ matrix.arch }})
96+ # Run a temporary container from the built image to copy the specific zip file
97+ run : |
98+ docker run --rm -v "${{ github.workspace }}/dist":/output \
99+ sharp-aws-lambda-layer:${{ matrix.arch }}-dev \
100+ cp /build/dist/sharp-layer-${{ matrix.arch }}.zip /output/
101+
102+ - name : Upload artifact (${{ matrix.arch }})
103+ uses : actions/upload-artifact@v4
104+ with :
105+ name : sharp-layer-${{ matrix.arch }} # Artifact name includes arch
106+ path : dist/sharp-layer-${{ matrix.arch }}.zip # Path to the specific zip file
107+
108+ # Release job runs after all builds complete
109+ release :
110+ needs : [setup, build] # Depends on setup for version and build for artifacts
111+ runs-on : ubuntu-latest
112+ # Only run on push to main, and if the release doesn't exist yet
113+ # You might want to adjust this condition (e.g., always create/overwrite?)
114+ if : github.event_name == 'push' && github.ref == 'refs/heads/main' # && needs.setup.outputs.release_exists == 'false'
115+ steps :
116+ - name : Download all artifacts
117+ uses : actions/download-artifact@v4
118+ with :
119+ # No name specified, downloads all artifacts from the workflow run
120+ path : dist # Download to 'dist' directory
121+ # Optional: Pattern matching if you have other artifacts
122+ # pattern: sharp-layer-*
123+ # merge-multiple: true # Merges artifacts if they have the same name (not needed here)
124+
125+ - name : List downloaded files
126+ run : ls -R dist
127+
128+ - name : Create or Update GitHub Release
43129 uses : svenstaro/upload-release-action@v2
44130 with :
45131 repo_token : ${{ secrets.GITHUB_TOKEN }}
46- tag : ${{ steps.vars.outputs.sharp_version }}
47- file : dist/sharp-layer.zip
48- overwrite : true
132+ # Use the sharp version determined in the setup job
133+ tag : v${{ needs.setup.outputs.sharp_version }}
134+ release_name : Sharp v${{ needs.setup.outputs.sharp_version }} Layer
135+ # Upload all zip files found in the 'dist' directory's subdirectories
136+ # Adjust the pattern if download-artifact structure is different
137+ file : dist/sharp-layer-*/*.zip
138+ file_glob : true # Enable globbing for the 'file' input
139+ overwrite : true # Overwrite existing assets in the release if tag exists
140+ body : " AWS Lambda Layer for Sharp v${{ needs.setup.outputs.sharp_version }}. Includes builds for x64 (amd64) and arm64."
141+ # make_latest: true # Optionally mark as latest release
0 commit comments