-
Notifications
You must be signed in to change notification settings - Fork 196
356 lines (320 loc) · 12.1 KB
/
release.yml
File metadata and controls
356 lines (320 loc) · 12.1 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
name: Release
on:
workflow_dispatch:
inputs:
version:
description: "Release version (e.g. 3.2.0)"
required: true
type: string
dry_run:
description: "Dry run (skip publishing)"
required: false
type: boolean
default: false
skip_crates:
description: "Skip crates.io publish"
required: false
type: boolean
default: false
env:
CARGO_TERM_COLOR: always
permissions:
contents: read
jobs:
# ---------------------------------------------------------------------------
# 1. Validate input & run tests
# ---------------------------------------------------------------------------
validate:
name: Validate & Test
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v4
- name: Validate branch and semver
id: version
run: |
if [[ "${{ github.ref_name }}" != "main" ]]; then
echo "::error::Releases must be triggered from the main branch, got '${{ github.ref_name }}'"
exit 1
fi
VERSION="${{ inputs.version }}"
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then
echo "::error::Invalid semver: $VERSION"
exit 1
fi
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy -- -D warnings
# ---------------------------------------------------------------------------
# 2. Bump versions in Cargo.toml files, commit, tag, push
# ---------------------------------------------------------------------------
version-bump:
name: Bump Version & Tag
needs: validate
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
VERSION: ${{ needs.validate.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Bump versions
run: |
# Bump package version (only the version line directly under [package])
for toml in guard/Cargo.toml guard-lambda/Cargo.toml guard-ffi/Cargo.toml; do
sed -i '0,/^version = "[^"]*"/{s/^version = "[^"]*"/version = "'"$VERSION"'"/}' "$toml"
done
# Update cross-crate dependency references
sed -i "s/cfn-guard = { version = \"[^\"]*\"/cfn-guard = { version = \"$VERSION\"/" guard-lambda/Cargo.toml
sed -i "s/cfn-guard = { version = \"[^\"]*\"/cfn-guard = { version = \"$VERSION\"/" guard-ffi/Cargo.toml
sed -i "s/cfn-guard = { version = \"[^\"]*\"/cfn-guard = { version = \"$VERSION\"/" guard-examples/library/Cargo.toml
# Update test fixtures that embed the version
sed -i "s/\"semanticVersion\": \"[^\"]*\"/\"semanticVersion\": \"$VERSION\"/" guard/resources/validate/output-dir/structured.sarif
sed -i "s/\"fullName\": \"cfn-guard [^\"]*\"/\"fullName\": \"cfn-guard $VERSION\"/" guard/resources/validate/output-dir/structured.sarif
# Update lockfile to reflect new versions
cargo check
- name: Commit & tag
run: |
BRANCH="release/$VERSION"
git checkout -b "$BRANCH"
git add -A
if ! git diff --cached --quiet; then
git commit -m "chore: bump version to $VERSION"
fi
if git ls-remote --tags origin "$VERSION" | grep -q "$VERSION"; then
echo "Tag $VERSION already exists on remote, skipping"
else
git tag "$VERSION"
git push origin "$VERSION"
fi
git fetch origin "$BRANCH" 2>/dev/null || true
git push origin "$BRANCH" --force-with-lease
- name: Create PR to merge version bump back to main
if: ${{ !inputs.dry_run }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr create \
--base main \
--head "release/$VERSION" \
--title "chore: bump version to $VERSION" \
--body "Automated version bump from release workflow. Merge after release is complete."
# ---------------------------------------------------------------------------
# 3. Build cross-platform binaries
# ---------------------------------------------------------------------------
build:
name: Build (${{ matrix.target }})
needs: [validate, version-bump]
strategy:
matrix:
include:
- target: x86_64-unknown-linux-musl
os: ubuntu-latest
asset_name: cfn-guard-v3-x86_64-ubuntu-latest
extra_asset_names: "cfn-guard-v3-ubuntu-latest cfn-guard-v3-x86_64-linux-latest cfn-guard-v3-linux-latest"
- target: aarch64-unknown-linux-musl
os: ubuntu-latest
asset_name: cfn-guard-v3-aarch64-ubuntu-latest
extra_asset_names: "cfn-guard-v3-aarch64-linux-latest"
- target: x86_64-apple-darwin
os: macos-latest
asset_name: cfn-guard-v3-x86_64-macos-latest
extra_asset_names: "cfn-guard-v3-macos-latest"
- target: aarch64-apple-darwin
os: macos-latest
asset_name: cfn-guard-v3-aarch64-macos-latest
extra_asset_names: ""
- target: x86_64-pc-windows-msvc
os: windows-latest
asset_name: cfn-guard-v3-x86_64-windows-latest
extra_asset_names: "cfn-guard-v3-windows-latest"
- target: aarch64-pc-windows-msvc
os: windows-latest
asset_name: cfn-guard-v3-aarch64-windows-latest
extra_asset_names: ""
- target: i686-pc-windows-msvc
os: windows-latest
asset_name: cfn-guard-v3-i686-windows-latest
extra_asset_names: ""
runs-on: ${{ matrix.os }}
env:
VERSION: ${{ needs.validate.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.validate.outputs.version }}
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: ${{ matrix.target }}
- name: Install cross-compilation deps (aarch64 linux)
if: matrix.target == 'aarch64-unknown-linux-musl'
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu
echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV"
- name: Build
run: cargo build --release --target ${{ matrix.target }}
- name: Package (unix)
if: runner.os != 'Windows'
run: |
BIN="target/${{ matrix.target }}/release/cfn-guard"
for NAME in ${{ matrix.asset_name }} ${{ matrix.extra_asset_names }}; do
mkdir "$NAME"
cp "$BIN" README.md "$NAME/"
tar czvf "${NAME}.tar.gz" "$NAME"
done
- name: Package (windows)
if: runner.os == 'Windows'
shell: bash
run: |
BIN="target/${{ matrix.target }}/release/cfn-guard.exe"
for NAME in ${{ matrix.asset_name }} ${{ matrix.extra_asset_names }}; do
mkdir "$NAME"
cp "$BIN" README.md "$NAME/"
tar czvf "${NAME}.tar.gz" "$NAME"
done
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.asset_name }}
path: "*.tar.gz"
retention-days: 1
# ---------------------------------------------------------------------------
# 4. Create GitHub Release & upload all assets
# ---------------------------------------------------------------------------
github-release:
name: GitHub Release
needs: [validate, build]
runs-on: ubuntu-latest
permissions:
contents: write
env:
VERSION: ${{ needs.validate.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.validate.outputs.version }}
- uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: true
- name: List assets
run: ls -lhR artifacts/
- name: Check if release exists
id: check
run: |
if gh release view "$VERSION" --repo "${{ github.repository }}" >/dev/null 2>&1; then
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create release
if: ${{ !inputs.dry_run }}
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.VERSION }}
name: v${{ env.VERSION }}
generate_release_notes: ${{ steps.check.outputs.exists == 'false' }}
files: artifacts/*.tar.gz
# ---------------------------------------------------------------------------
# 5. Publish to crates.io
# ---------------------------------------------------------------------------
publish-crates:
name: Publish to crates.io
needs: [validate, version-bump, github-release]
if: ${{ !inputs.dry_run && !inputs.skip_crates }}
runs-on: ubuntu-latest
env:
VERSION: ${{ needs.validate.outputs.version }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.validate.outputs.version }}
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Publish cfn-guard
run: |
cd guard
cargo publish --no-verify 2>&1 | tee /tmp/publish.log || {
if grep -q "already uploaded" /tmp/publish.log; then
echo "::warning::cfn-guard already published, skipping"
else
exit 1
fi
}
- name: Wait for crates.io index
run: sleep 30
- name: Publish cfn-guard-lambda
run: |
cd guard-lambda
cargo publish --no-verify 2>&1 | tee /tmp/publish.log || {
if grep -q "already uploaded" /tmp/publish.log; then
echo "::warning::cfn-guard-lambda already published, skipping"
else
exit 1
fi
}
- name: Publish cfn-guard-ffi
run: |
cd guard-ffi
cargo publish --no-verify 2>&1 | tee /tmp/publish.log || {
if grep -q "already uploaded" /tmp/publish.log; then
echo "::warning::cfn-guard-ffi already published, skipping"
else
exit 1
fi
}
# ---------------------------------------------------------------------------
# 6. Publish Docker to ECR Public
# ---------------------------------------------------------------------------
publish-docker:
name: Publish Docker to ECR
needs: [validate, version-bump]
if: ${{ !inputs.dry_run }}
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
env:
VERSION: ${{ needs.validate.outputs.version }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ needs.validate.outputs.version }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ secrets.AWS_REGION }}
role-to-assume: ${{ secrets.PUBLISHER_ROLE_NAME }}
role-session-name: PublishToECR
- name: Login to ECR Public
id: ecr
uses: aws-actions/amazon-ecr-login@v2
with:
registry-type: public
- name: Build & push
env:
REGISTRY: ${{ steps.ecr.outputs.registry }}
REGISTRY_ALIAS: ${{ secrets.REGISTRY_ALIAS }}
REPOSITORY: cloudformation-guard
run: |
PREFIX="$REGISTRY/$REGISTRY_ALIAS/$REPOSITORY"
docker build \
-t "$PREFIX:$VERSION" \
-t "$PREFIX:latest" \
.
docker push "$PREFIX:$VERSION"
docker push "$PREFIX:latest"