-
Notifications
You must be signed in to change notification settings - Fork 6
252 lines (220 loc) · 9.28 KB
/
build-layers.yml
File metadata and controls
252 lines (220 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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 }}"