Repository navigation
256 lines (247 loc) · 10.3 KB
/
Copy pathrelease.yml
File metadata and controls
256 lines (247 loc) · 10.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
name: Release
# Manually triggered (Actions tab -> Release -> Run workflow) by someone
# with write access to this repo -- deliberately NOT a tag-push trigger,
# so a release/PyPI publish can never fire from a stray tag or from a
# fork/PR (which never has write access or secrets on a public repo
# regardless). Builds standalone binaries for every supported platform
# (PyInstaller can't cross-compile, so each one runs on a real runner of
# that OS/arch), attaches them to a GitHub Release, then publishes to
# PyPI, npm (as platform-specific optionalDependencies packages), and
# GHCR.
#
# Required repo secrets (Settings -> Secrets and variables -> Actions):
# PYPI_API_TOKEN - PyPI account -> API tokens
# NPM_PROD_TOKEN - npmjs.com account -> Access Tokens -> Automation
# (NPM_TOKEN is still read as a fallback)
# GITHUB_TOKEN (GHCR push) is provided automatically, no setup needed.
on:
workflow_dispatch:
inputs:
tag:
description: "Tag to release (e.g. v0.3.0) -- must already exist and match src/secfoo/__about__.py"
required: true
npm-only:
description: "Only publish npm, from the binaries already attached to this tag's GitHub Release"
type: boolean
default: false
# Read-only by default; the two jobs that need to write (the GitHub
# Release, the GHCR push) grant themselves exactly that permission.
permissions:
contents: read
jobs:
check-version:
name: Verify tag matches package version
runs-on: ubuntu-latest
outputs:
version: ${{ steps.check.outputs.version }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.tag }}
- id: check
env:
# Through the environment, not ${{ }} inside the script, so the tag
# text can never become shell code.
TAG: ${{ inputs.tag }}
run: |
TAG_VERSION="${TAG#v}"
PKG_VERSION=$(grep -oP '(?<=__version__ = ")[^"]+' src/secfoo/__about__.py)
echo "Tag version: $TAG_VERSION"
echo "Package version: $PKG_VERSION"
if [ "$TAG_VERSION" != "$PKG_VERSION" ]; then
echo "::error::Tag $TAG does not match src/secfoo/__about__.py's __version__ ($PKG_VERSION). Bump __about__.py and retag."
exit 1
fi
echo "version=$PKG_VERSION" >> "$GITHUB_OUTPUT"
build-binaries:
name: Build binary (${{ matrix.name }})
needs: check-version
if: ${{ !inputs.npm-only }}
strategy:
fail-fast: false
matrix:
include:
# macos-14 runners are retired (the job is cancelled before it starts).
- os: macos-15
name: darwin-arm64
- os: macos-15-intel
name: darwin-x64
- os: ubuntu-latest
name: linux-x64
- os: ubuntu-24.04-arm
name: linux-arm64
- os: windows-latest
name: win32-x64
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.tag }}
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"
- name: Install package + build deps
run: pip install -e ".[build]"
- name: Build binary
run: pyinstaller packaging/secfoo.spec --distpath dist/binary --workpath build/pyinstaller --noconfirm
- name: Stage artifact under its final release name
shell: bash
run: |
mkdir -p out
if [ "${{ runner.os }}" = "Windows" ]; then
cp dist/binary/secfoo.exe out/secfoo-${{ matrix.name }}.exe
else
cp dist/binary/secfoo out/secfoo-${{ matrix.name }}
fi
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: secfoo-${{ matrix.name }}
path: out/*
if-no-files-found: error
release:
name: Create GitHub Release
needs: build-binaries
if: ${{ !inputs.npm-only }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
path: artifacts
# Only the binaries: the Docker job also uploads a build-record artifact.
pattern: secfoo-*
- name: Flatten artifacts and compute checksums
run: |
mkdir -p release
find artifacts -type f -exec cp {} release/ \;
(cd release && sha256sum * > SHA256SUMS)
- uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4
with:
tag_name: ${{ inputs.tag }}
files: release/*
generate_release_notes: true
publish-pypi:
name: Publish to PyPI
needs: [check-version]
if: ${{ !inputs.npm-only }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.tag }}
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"
- run: pip install build twine
- run: python -m build
- env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
# --skip-existing: a PyPI version can't be re-uploaded, so a re-run for a
# tag whose upload already succeeded must not fail here.
run: twine upload --skip-existing dist/*.tar.gz dist/*.whl
publish-npm:
name: Publish npm packages
needs: [check-version, release]
# Runs after a fresh GitHub Release, or alone with npm-only for a tag
# whose release already exists.
if: ${{ !cancelled() && needs.check-version.result == 'success' && (needs.release.result == 'success' || inputs.npm-only) }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.tag }}
- name: Download the release binaries
# From the GitHub Release itself, so npm ships exactly the files (and
# SHA256SUMS) users download there, and an npm-only run needs no rebuild.
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ inputs.tag }}
run: gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --pattern 'secfoo-*' --pattern SHA256SUMS --dir release-bin
- name: Verify checksums
working-directory: release-bin
run: grep -E ' secfoo-(darwin|linux|win32)-' SHA256SUMS | sha256sum -c -
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: "20"
registry-url: "https://registry.npmjs.org"
- name: Populate platform binaries and publish all 6 packages
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PROD_TOKEN || secrets.NPM_TOKEN }}
VERSION: ${{ needs.check-version.outputs.version }}
run: |
set -euo pipefail
if [ -z "${NODE_AUTH_TOKEN:-}" ]; then
echo "::error::NPM_PROD_TOKEN secret is not set (Settings -> Secrets and variables -> Actions)."
exit 1
fi
# A version can't be re-published; skip any package already at it,
# so a re-run after a partial publish finishes the rest.
publish() {
local dir="$1" name
name=$(node -p "require('./$dir/package.json').name")
npm version "$VERSION" --no-git-tag-version --prefix "$dir" --allow-same-version
if npm view "$name@$VERSION" version >/dev/null 2>&1; then
echo "$name@$VERSION is already on npm, skipping"
return
fi
# "./" makes it a folder: "npm/secfoo-x" alone is read as a GitHub user/repo.
npm publish --access public "./$dir"
}
declare -A DIRS=(
[darwin-arm64]=npm/secfoo-darwin-arm64
[darwin-x64]=npm/secfoo-darwin-x64
[linux-x64]=npm/secfoo-linux-x64
[linux-arm64]=npm/secfoo-linux-arm64
[win32-x64]=npm/secfoo-win32-x64
)
for plat in "${!DIRS[@]}"; do
pkgdir="${DIRS[$plat]}"
mkdir -p "$pkgdir/bin"
if [ "$plat" = "win32-x64" ]; then
cp "release-bin/secfoo-$plat.exe" "$pkgdir/bin/secfoo.exe"
else
cp "release-bin/secfoo-$plat" "$pkgdir/bin/secfoo"
chmod +x "$pkgdir/bin/secfoo"
fi
publish "$pkgdir"
done
# The meta-package last: its optionalDependencies must already exist,
# and must point at this version (package.json keeps a placeholder).
node -e '
const fs = require("fs"), f = "./npm/secfoo/package.json", p = require(f);
for (const k of Object.keys(p.optionalDependencies)) p.optionalDependencies[k] = process.env.VERSION;
fs.writeFileSync(f, JSON.stringify(p, null, 2) + "\n");
'
publish npm/secfoo
publish-docker:
name: Publish Docker image (GHCR)
needs: [check-version, publish-pypi]
if: ${{ !inputs.npm-only }}
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
# The image installs secfoo==<version> from PyPI, so only the build
# recipe comes from the checkout. Take it from the workflow's own
# commit, not the tag: a broken base image can then be fixed on main
# and the release re-run without moving a published tag.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
push: true
# Pinned so the image is exactly the release; the Dockerfile retries
# while PyPI's index catches up.
build-args: |
SECFOO_VERSION=${{ needs.check-version.outputs.version }}
tags: |
ghcr.io/secfoo-com/secfoo:${{ needs.check-version.outputs.version }}
ghcr.io/secfoo-com/secfoo:latest