-
Notifications
You must be signed in to change notification settings - Fork 2
331 lines (331 loc) · 14.3 KB
/
Copy pathrelease.yml
File metadata and controls
331 lines (331 loc) · 14.3 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
# SPDX-License-Identifier: Apache-2.0
name: Release
run-name: Release ${{ inputs.tag }}
on:
workflow_dispatch:
inputs:
tag:
description: "Immutable v* tag"
required: true
type: string
prepare_run_id:
description: "Successful Release Prepare run ID"
required: true
type: string
distribution_ref:
description: "Optional ref for distribution workflow/helper fixes"
required: false
type: string
crates:
description: "Publish crates.io packages"
required: true
default: true
type: boolean
pypi:
description: "Publish prepared Python files"
required: true
default: true
type: boolean
npm:
description: "Publish prepared npm tarballs"
required: true
default: true
type: boolean
github:
description: "Create/update GitHub release"
required: true
default: true
type: boolean
concurrency:
group: release-distribution-${{ inputs.tag }}
cancel-in-progress: false
permissions:
contents: read
actions: read
jobs:
validate:
uses: ./.github/workflows/release-validate.yml
with:
tag: ${{ inputs.tag }}
provenance:
needs: validate
runs-on: ubuntu-latest
outputs:
artifact: ${{ steps.check.outputs.artifact }}
steps:
- uses: actions/checkout@v6
with:
ref: ${{ inputs.distribution_ref || inputs.tag }}
- id: check
shell: bash
env:
GH_TOKEN: ${{ github.token }}
RUN: ${{ inputs.prepare_run_id }}
TAG: ${{ inputs.tag }}
SHA: ${{ needs.validate.outputs.source_sha }}
PUBLISH_CRATES: ${{ inputs.crates }}
PUBLISH_PYPI: ${{ inputs.pypi }}
PUBLISH_NPM: ${{ inputs.npm }}
PUBLISH_GITHUB: ${{ inputs.github }}
IS_PRERELEASE: ${{ needs.validate.outputs.is_prerelease }}
run: |
set -euo pipefail
[[ "$RUN" =~ ^[0-9]+$ ]] || { echo "invalid prepare run ID" >&2; exit 1; }
[[ "$PUBLISH_CRATES$PUBLISH_PYPI$PUBLISH_NPM$PUBLISH_GITHUB" == *true* ]] || { echo "enable at least one distribution target" >&2; exit 1; }
if [[ "$IS_PRERELEASE" = true && "$PUBLISH_CRATES$PUBLISH_PYPI$PUBLISH_NPM" != *true* ]]; then
echo "GitHub Releases are created only for stable versions; enable a registry target" >&2
exit 1
fi
RUN_JSON="$(gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$RUN")"
test "$(jq -r .event <<<"$RUN_JSON")" = push
test "$(jq -r .conclusion <<<"$RUN_JSON")" = success
test "$(jq -r .head_sha <<<"$RUN_JSON")" = "$SHA"
RUN_PATH="$(jq -r .path <<<"$RUN_JSON")"
case "$RUN_PATH" in
.github/workflows/release-prepare.yml|.github/workflows/release-prepare.yml@*) ;;
*) echo "prepare run used unexpected workflow path: $RUN_PATH" >&2; exit 1 ;;
esac
test "$(jq -r .repository.full_name <<<"$RUN_JSON")" = "$GITHUB_REPOSITORY"
test "$(jq -r .head_repository.full_name <<<"$RUN_JSON")" = "$GITHUB_REPOSITORY"
ARTIFACT="release-bundle-${{ needs.validate.outputs.version }}"
COUNT="$(gh api "/repos/$GITHUB_REPOSITORY/actions/runs/$RUN/artifacts" --paginate --jq ".artifacts[] | select(.name == \"$ARTIFACT\" and .expired == false) | .id" | wc -l)"
test "$COUNT" -eq 1 || { echo "expected exactly one live $ARTIFACT artifact, got $COUNT" >&2; exit 1; }
echo "artifact=$ARTIFACT" >> "$GITHUB_OUTPUT"
- uses: actions/download-artifact@v8
with:
name: ${{ steps.check.outputs.artifact }}
run-id: ${{ inputs.prepare_run_id }}
github-token: ${{ github.token }}
path: bundle
- name: Verify exact prepared bundle before protected environments
shell: bash
run: |
set -euo pipefail
python3 scripts/release/artifact_manifest.py verify --directory bundle --manifest bundle/release-manifest.json --repository "$GITHUB_REPOSITORY" --tag "${{ inputs.tag }}" --source-sha "${{ needs.validate.outputs.source_sha }}" --prepare-run-id "${{ inputs.prepare_run_id }}" --workflow release-prepare.yml --version "${{ needs.validate.outputs.version }}"
python3 scripts/release/artifact_manifest.py verify-checksums --directory bundle --manifest bundle/release-manifest.json --checksums bundle/SHA256SUMS
publish-crates:
needs: [validate, provenance]
if: inputs.crates
runs-on: ubuntu-latest
environment: crates.io
steps:
- uses: actions/checkout@v6
with:
ref: ${{ inputs.distribution_ref || inputs.tag }}
- uses: actions/checkout@v6
with:
ref: ${{ needs.validate.outputs.source_sha }}
path: tagged-source
- uses: dtolnay/rust-toolchain@stable
- run: python3 scripts/release/version_contract.py stamp "${{ needs.validate.outputs.version }}" --source tagged-source --destination release-source
- shell: bash
working-directory: release-source
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
set -euo pipefail
VERSION="${{ needs.validate.outputs.version }}"
USER_AGENT="code2graph-release/$VERSION ($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)"
available() { curl -fsS -A "$USER_AGENT" "https://crates.io/api/v1/crates/$1/$VERSION" >/dev/null; }
verify_remote() {
local package="$1" remote
remote="$(mktemp)"
curl -fsSL -A "$USER_AGENT" "https://crates.io/api/v1/crates/$package/$VERSION/download" -o "$remote"
cmp --silent "target/package/$package-$VERSION.crate" "$remote" || { echo "crates.io archive differs for $package@$VERSION" >&2; return 1; }
}
publish() {
local package="$1"
cargo package -p "$package" --allow-dirty --no-verify
if available "$package"; then
verify_remote "$package"
return
fi
if ! cargo publish -p "$package" --allow-dirty --no-verify; then
echo "cargo publish returned an error; reconciling against crates.io before failing" >&2
fi
for _ in {1..30}; do
if available "$package"; then verify_remote "$package" && return; fi
sleep 10
done
echo "$package@$VERSION did not become available after publish reconciliation" >&2
return 1
}
publish code2graph
publish code2graph-query
publish code2graph-cli
publish-pypi:
needs: [validate, provenance]
if: inputs.pypi
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
actions: read
contents: read
steps:
- uses: actions/checkout@v6
with:
ref: ${{ inputs.distribution_ref || inputs.tag }}
- uses: actions/download-artifact@v8
with:
name: ${{ needs.provenance.outputs.artifact }}
run-id: ${{ inputs.prepare_run_id }}
github-token: ${{ github.token }}
path: bundle
- name: Reject different existing files and verify eventual registry hashes
shell: bash
run: |
set -euo pipefail
project=code2graph-rs
version="${{ needs.validate.outputs.python_version }}"
release_json() { curl -fsSL "https://pypi.org/pypi/$project/$version/json"; }
check_hashes() {
local json="$1" file name local_hash remote_hash
mapfile -t local_names < <(find bundle/python -maxdepth 1 -type f -printf '%f\n' | sort)
mapfile -t remote_names < <(jq -r '.urls[].filename' <<<"$json" | sort)
for name in "${remote_names[@]}"; do
printf '%s\n' "${local_names[@]}" | grep -Fxq "$name" || { echo "unexpected PyPI artifact $name" >&2; return 1; }
file="bundle/python/$name"
local_hash="$(sha256sum "$file" | awk '{print $1}')"
remote_hash="$(jq -r --arg name "$name" '.urls[] | select(.filename == $name) | .digests.sha256' <<<"$json")"
test "$remote_hash" = "$local_hash" || { echo "PyPI artifact mismatch for $name" >&2; return 1; }
done
}
if json="$(release_json 2>/dev/null)"; then check_hashes "$json"; fi
- uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: bundle/python
skip-existing: true
- name: Poll and re-verify published Python files
shell: bash
run: |
set -euo pipefail
for attempt in {1..90}; do
if json="$(curl -fsSL -H 'Cache-Control: no-cache' "https://pypi.org/pypi/code2graph-rs/${{ needs.validate.outputs.python_version }}/json?attempt=$attempt" 2>/dev/null)"; then
mapfile -t local_names < <(find bundle/python -maxdepth 1 -type f -printf '%f\n' | sort)
mapfile -t remote_names < <(jq -r '.urls[].filename' <<<"$json" | sort)
complete=true
test "${local_names[*]}" = "${remote_names[*]}" || complete=false
for file in bundle/python/*; do
name="$(basename "$file")"; hash="$(sha256sum "$file" | awk '{print $1}')"
test "$(jq -r --arg name "$name" '.urls[] | select(.filename == $name) | .digests.sha256' <<<"$json")" = "$hash" || complete=false
done
"$complete" && exit 0
fi
sleep 5
done
exit 1
publish-npm:
needs: [validate, provenance]
if: inputs.npm
runs-on: ubuntu-latest
environment: npm
permissions:
id-token: write
actions: read
contents: read
steps:
- uses: actions/checkout@v6
with:
ref: ${{ inputs.distribution_ref || inputs.tag }}
- uses: actions/setup-node@v5
with:
node-version: "22"
registry-url: https://registry.npmjs.org
- uses: actions/download-artifact@v8
with:
name: ${{ needs.provenance.outputs.artifact }}
run-id: ${{ inputs.prepare_run_id }}
github-token: ${{ github.token }}
path: bundle
- name: Publish prepared platform packages before the root package
shell: bash
env:
# A token is required to bootstrap previously unpublished platform packages;
# provenance remains attached and subsequent releases stay idempotent.
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
set -euo pipefail
publish() {
local tarball="$1" name version remote
name=$(tar -xOf "$tarball" package/package.json | jq -r .name)
version=$(tar -xOf "$tarball" package/package.json | jq -r .version)
if npm view "$name@$version" version >/dev/null 2>&1; then
remote="$(mktemp -d)"
npm pack "$name@$version" --pack-destination "$remote"
python3 scripts/release/artifact_manifest.py compare-npm --local "$tarball" --remote "$(find "$remote" -name '*.tgz' -print -quit)"
else
npm publish "$tarball" --access public --provenance --tag "${{ needs.validate.outputs.is_prerelease == 'true' && 'next' || 'latest' }}"
fi
for _ in {1..30}; do
if npm view "$name@$version" version >/dev/null 2>&1; then
remote="$(mktemp -d)"
npm pack "$name@$version" --pack-destination "$remote"
python3 scripts/release/artifact_manifest.py compare-npm --local "$tarball" --remote "$(find "$remote" -name '*.tgz' -print -quit)"
return
fi
sleep 2
done
return 1
}
shopt -s nullglob
platform_tarballs=(bundle/npm/*linux-*.tgz bundle/npm/*darwin-*.tgz bundle/npm/*win32-*.tgz)
test "${#platform_tarballs[@]}" -eq 6
for file in "${platform_tarballs[@]}"; do publish "$file"; done
root_tarballs=(bundle/npm/nodedb-lab-code2graph-*.tgz)
roots=()
for file in "${root_tarballs[@]}"; do [[ "$file" == *linux-* || "$file" == *darwin-* || "$file" == *win32-* ]] || roots+=("$file"); done
test "${#roots[@]}" -eq 1
publish "${roots[0]}"
github-release:
needs: [validate, provenance]
if: inputs.github && needs.validate.outputs.is_prerelease != 'true'
runs-on: ubuntu-latest
permissions:
contents: write
actions: read
steps:
- uses: actions/checkout@v6
with:
ref: ${{ inputs.tag }}
- uses: actions/download-artifact@v8
with:
name: ${{ needs.provenance.outputs.artifact }}
run-id: ${{ inputs.prepare_run_id }}
github-token: ${{ github.token }}
path: bundle
- name: Verify exact GitHub release input set
id: release-files
shell: bash
run: |
set -euo pipefail
mapfile -t files < <(find bundle -type f | sort)
test "${#files[@]}" -eq 16
# It consumes the job-scoped contents:write GITHUB_TOKEN directly.
- name: Create or update GitHub release
uses: softprops/action-gh-release@v3
with:
tag_name: ${{ inputs.tag }}
name: code2graph ${{ needs.validate.outputs.version }}
target_commitish: ${{ needs.validate.outputs.source_sha }}
generate_release_notes: true
prerelease: false
make_latest: true
files: bundle/**
fail_on_unmatched_files: true
overwrite_files: true
- name: Verify exact GitHub release output
shell: bash
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ inputs.tag }}
SHA: ${{ needs.validate.outputs.source_sha }}
run: |
set -euo pipefail
mapfile -t expected < <(find bundle -type f -printf '%f\n' | sort)
release_json="$(gh api "/repos/$GITHUB_REPOSITORY/releases/tags/$TAG")"
test "$(jq -r .tag_name <<<"$release_json")" = "$TAG"
test "$(jq -r .target_commitish <<<"$release_json")" = "$SHA"
test "$(jq -r .prerelease <<<"$release_json")" = false
mapfile -t actual < <(jq -r '.assets[].name' <<<"$release_json" | sort)
test "${actual[*]}" = "${expected[*]}" || { echo "GitHub release attachment set mismatch" >&2; exit 1; }