Repository navigation
217 lines (209 loc) · 9.35 KB
/
Copy pathrelease.yml
File metadata and controls
217 lines (209 loc) · 9.35 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
name: Release
# Publishes psychoscript to npm from dev.
#
# - build runs repository code (bun install, bun run pack) with contents: read,
# no environment and no id-token. It computes the next version, packs the
# tarball and uploads it as an artifact.
# - publish runs in the release environment (allowed only from dev) with
# contents: write and id-token: write. It never checks out the repository or
# runs a script from it: it downloads the tarball, checks the artifact
# against the build's digest and its manifest as data, publishes it with the job's OIDC token, then tags the build's commit
# and creates the GitHub release.
#
# There is no npm token: the trusted publisher on psychoscript matches this
# file name, anomalyco/psychoscript and the release environment. Provenance
# comes from the publish job's OIDC token, so publishing a prebuilt tarball
# keeps it.
#
# The version comes from the newest v<X.Y.Z> tag (or package.json before the
# first tag), is set in the build workspace only and is never committed. The
# tag on the dispatched commit is the version of record.
on:
workflow_dispatch:
inputs:
bump:
description: Which part of the version to bump
required: true
type: choice
options: [patch, minor, major]
default: patch
dry_run:
description: Build and check the package without publishing, tagging, or releasing
required: false
type: boolean
default: false
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
# Finish an in-flight publish before another run checks the registry.
cancel-in-progress: false
defaults:
run:
shell: bash
jobs:
build:
name: Build package
runs-on: ubuntu-24.04
timeout-minutes: 15
permissions:
contents: read
outputs:
version: ${{ steps.version.outputs.version }}
sha: ${{ steps.version.outputs.sha }}
artifact: ${{ steps.package.outputs.artifact-id }}
digest: ${{ steps.package.outputs.artifact-digest }}
steps:
- name: Require dev
env:
REF: ${{ github.ref }}
run: |
if [[ $REF != refs/heads/dev ]]; then
echo "::error::Dispatch the release from dev, not $REF."
exit 1
fi
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
with:
fetch-depth: 0 # every tag, for the next version
persist-credentials: false
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
with:
node-version: 24.16.0
package-manager-cache: false
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: 1.4.4
- id: version
name: Compute the next version
env:
BUMP: ${{ inputs.bump }}
run: |
latest=$(git tag --list 'v*' --sort=-v:refname | grep -Ex 'v[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 || true)
if [ -n "$latest" ]; then
base=${latest#v}
else
base=$(node -p "require('./package.json').version")
fi
[[ $base =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || { echo "::error::Cannot bump $base; expected X.Y.Z."; exit 1; }
major=${BASH_REMATCH[1]} minor=${BASH_REMATCH[2]} patch=${BASH_REMATCH[3]}
case "$BUMP" in
major) version="$((major + 1)).0.0" ;;
minor) version="$major.$((minor + 1)).0" ;;
patch) version="$major.$minor.$((patch + 1))" ;;
*) echo "::error::Unknown bump $BUMP."; exit 1 ;;
esac
if git rev-parse -q --verify "refs/tags/v$version" > /dev/null; then
echo "::error::v$version is already tagged; publish a newer version instead."
exit 1
fi
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "sha=$GITHUB_SHA" >> "$GITHUB_OUTPUT"
echo "Releasing psychoscript@$version from $GITHUB_SHA." >> "$GITHUB_STEP_SUMMARY"
- name: Set the version
env:
VERSION: ${{ steps.version.outputs.version }}
run: npm version "$VERSION" --no-git-tag-version --allow-same-version
- run: bun install --frozen-lockfile
- run: bun run typecheck
- run: bun test
# Builds, packs into .pack/ and imports the tarball under Node and Bun.
- run: bun run pack
# upload-artifact skips hidden directories such as .pack.
- run: mkdir "$RUNNER_TEMP/dist" && cp .pack/*.tgz "$RUNNER_TEMP/dist/"
- id: package
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: package
path: ${{ runner.temp }}/dist/*.tgz
if-no-files-found: error
publish:
name: Publish release
needs: build
runs-on: ubuntu-24.04
timeout-minutes: 10
environment: release
permissions:
contents: write # the tag and the GitHub release
id-token: write # npm trusted publishing and provenance
actions: read # download this build's artifact by its immutable id
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ needs.build.outputs.version }}
SHA: ${{ needs.build.outputs.sha }}
DRY_RUN: ${{ inputs.dry_run }}
steps:
# No checkout, bun or repository scripts in this job.
- uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
with:
node-version: 24.16.0
package-manager-cache: false
# Node 24.16.0 bundles npm 11.13.0 (trusted publishing requires >=11.5.1);
# no unpinned npm install in a job holding publish rights.
- name: Download the package
env:
ARTIFACT_ID: ${{ needs.build.outputs.artifact }}
DIGEST: ${{ needs.build.outputs.digest }}
run: |
[[ $ARTIFACT_ID =~ ^[0-9]+$ ]]
[[ $DIGEST =~ ^[0-9a-f]{64}$ ]]
gh api "repos/$GITHUB_REPOSITORY/actions/artifacts/$ARTIFACT_ID/zip" > "$RUNNER_TEMP/artifact.zip"
echo "$DIGEST $RUNNER_TEMP/artifact.zip" | sha256sum --check --strict
python3 - <<'PY'
import os, pathlib, stat, zipfile
root = pathlib.Path(os.environ['RUNNER_TEMP'])
archive = root / 'artifact.zip'
limit = 32 * 1024 * 1024
if archive.stat().st_size > limit:
raise SystemExit('Package archive exceeds 32 MiB.')
with zipfile.ZipFile(archive) as source:
entries = source.infolist()
expected = 'psychoscript-' + os.environ['VERSION'] + '.tgz'
if len(entries) != 1 or entries[0].filename != expected:
raise SystemExit('Expected one versioned psychoscript tarball.')
entry = entries[0]
kind = stat.S_IFMT(entry.external_attr >> 16)
if kind not in (0, stat.S_IFREG) or entry.flag_bits & 1 or entry.file_size > limit or entry.compress_type not in (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED):
raise SystemExit('Expected a regular tarball of at most 32 MiB.')
target = root / 'package'
target.mkdir()
# Read the entry as bytes; its name never chooses an output path.
with source.open(entry) as content, (target / 'package.tgz').open('xb') as output:
written = 0
while chunk := content.read(65536):
written += len(chunk)
if written > limit:
raise SystemExit('Tarball exceeds 32 MiB.')
output.write(chunk)
PY
- name: Check the package
run: |
tarball="$RUNNER_TEMP/package/package.tgz"
IFS=$'\t' read -r name version install_scripts < <(tar -xzOf "$tarball" package/package.json |
jq -r '[.name, .version, ((.scripts // {}) | (has("preinstall") or has("install") or has("postinstall")))] | @tsv')
[ "$name" = 'psychoscript' ] || { echo "::error::Unexpected package $name."; exit 1; }
[ "$version" = "$VERSION" ] || { echo "::error::Tarball has version $version, expected $VERSION."; exit 1; }
[ "$install_scripts" = false ] || { echo "::error::$name has an install script."; exit 1; }
if [ "$(npm view "$name@$VERSION" version 2>/dev/null)" = "$VERSION" ]; then
echo "::error::$name@$VERSION is already published; publish a newer version."
exit 1
fi
echo "Publishing $name@$VERSION." >> "$GITHUB_STEP_SUMMARY"
- name: Publish
run: |
# A clean user config, so nothing can redirect the registry or supply
# a token: trusted publishing uses the OIDC token.
printf 'registry=https://registry.npmjs.org/\n' > "$RUNNER_TEMP/npmrc"
export NPM_CONFIG_USERCONFIG="$RUNNER_TEMP/npmrc"
flags=(--access public --tag latest)
if [ "$DRY_RUN" = 'true' ]; then
flags+=(--dry-run)
else
flags+=(--provenance)
fi
npm publish "$RUNNER_TEMP/package/package.tgz" "${flags[@]}"
# Only after a successful publish: the tag names the build's commit.
- name: Tag and release
if: inputs.dry_run == false
run: |
tag="v$VERSION"
gh api -X POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/tags/$tag" -f sha="$SHA" > /dev/null
gh release create "$tag" -R "$GITHUB_REPOSITORY" --verify-tag --title "$tag" --generate-notes