Repository navigation
245 lines (225 loc) · 10.9 KB
/
Copy pathversion.yml
File metadata and controls
245 lines (225 loc) · 10.9 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
name: Version
# Versions the code and publishes each release. Rules: docs/specs/versioning.md.
#
# 1. version: python-semantic-release reads the Conventional Commits since the last tag, and when
# one warrants a bump it updates pyproject.toml, uv.lock and CHANGELOG.md, commits
# "chore(release): vX.Y.Z [skip ci]" and tags vX.Y.Z. Configuration: [tool.semantic_release]
# in pyproject.toml.
# 2. release: builds the wheel and a hashed requirements file from the tag and attaches them to
# the GitHub release vX.Y.Z, whose notes are the tag's CHANGELOG.md section.
# 3. homebrew: points Formula/cstudy.rb in bkuberek/homebrew-tap at those two files.
#
# A release is cut only after CI passes on the commit it releases. The workflow runs when a run
# of ci.yml completes on main, and the version job goes on only when that run succeeded, was for
# a push, and its commit is still the tip of main. A job that ci.yml's "changes" job skips does
# not fail the run.
#
# Release and homebrew run for the tag this run created on main. A manual run on main publishes
# the newest tag reachable from it instead, so it repairs a failed or missing publish. It cuts a
# release first only when CI passed on a push of the commit it runs on. Both jobs skip what is
# already done: a release that has its files keeps them, and an unchanged formula is not
# committed. Nothing goes to PyPI.
#
# The push uses GITHUB_TOKEN, never a personal access token. Events caused by GITHUB_TOKEN do
# not start new workflow runs (workflow_dispatch and repository_dispatch excepted), so the bot
# commit and the tag start no CI run, hence no run of this workflow, and no loop is possible:
# https://docs.github.com/en/actions/concepts/security/github_token
# For the same reason a workflow triggered by the tag or the release would never run, so
# publishing is part of this workflow. Only the homebrew job holds HOMEBREW_TAP_GITHUB_TOKEN,
# which can push to the tap.
#
# A workflow_run run uses this file as it is on the default branch and gets the write
# permissions below whatever started it, so it must never check out or run code from a fork or
# a pull request. `branches: [main]` keeps out CI runs of other branches, and the version job's
# `event == 'push'` keeps out those of pull requests, a fork's branch named main included: a push
# to main is the only CI run that leads here, and it is of this repository's main.
# https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#workflow_run
on:
workflow_run:
workflows: [CI]
types: [completed]
branches: [main]
workflow_dispatch:
permissions: {}
# A run that can release waits for the one in progress instead of cancelling it, so two runs
# never race on the tag. A workflow_run run's github.ref is the default branch, so it shares the
# group with a manual run on main. GitHub keeps one pending run per group and cancels the pending
# one when another arrives, so a run the version job's condition rules out (a failed CI run, a
# pull request's) gets a group of its own and never displaces one that can release. The
# condition here is the version job's.
concurrency:
group: >-
${{ (github.event_name == 'workflow_dispatch'
|| (github.event.workflow_run.conclusion == 'success'
&& github.event.workflow_run.event == 'push'))
&& format('version-{0}', github.ref)
|| format('version-skip-{0}', github.run_id) }}
cancel-in-progress: false
jobs:
version:
# A CI run that passed on a push to main, or a manual run. A failed, cancelled or pull
# request CI run versions nothing.
if: >-
github.event_name == 'workflow_dispatch'
|| (github.event.workflow_run.conclusion == 'success'
&& github.event.workflow_run.event == 'push')
runs-on: ubuntu-latest
permissions:
actions: read
contents: write
outputs:
tag: ${{ steps.publish.outputs.tag }}
steps:
# The branch, not a detached commit: semantic-release releases only from a branch. In a
# workflow_run run, github.ref_name is the default branch.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
ref: ${{ github.ref_name }}
# semantic-release releases the tip of the branch, so the commit CI passed on is versioned
# only while it is that tip. When main has moved past it, this run versions nothing: the
# newer commit's own CI run versions both once it passes. Before it pushes, semantic-release
# checks that main has not moved since, so nothing CI did not pass is tagged.
# A manual run versions the commit it was started on, whatever reached the branch while it
# was queued, and bumps only when a CI run of a push of that commit to the branch passed.
# Otherwise it only publishes the newest tag, which repairs a publish; a release commit,
# which CI does not run on, is such a commit.
- name: Pin the branch to the commit this run is for
id: pin
env:
EVENT: ${{ github.event_name }}
CI_SHA: ${{ github.event.workflow_run.head_sha }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
if [ "$EVENT" = workflow_run ]; then
tip=$(git rev-parse HEAD)
if [ "$tip" != "$CI_SHA" ]; then
echo "CI passed on $CI_SHA, but the branch is at $tip now: its CI run versions it"
echo "current=false" >> "$GITHUB_OUTPUT"
exit 0
fi
bump=true
else
git reset --hard "$GITHUB_SHA"
passed='any(.[]; .conclusion == "success")'
if ci=$(gh run list --workflow ci.yml --commit "$GITHUB_SHA" --branch "$GITHUB_REF_NAME" \
--event push --limit 100 --json conclusion --jq "$passed") && [ "$ci" = true ]; then
bump=true
else
echo "CI has not passed on $GITHUB_SHA: no new release, the newest tag is published"
bump=false
fi
fi
echo "current=true" >> "$GITHUB_OUTPUT"
echo "bump=$bump" >> "$GITHUB_OUTPUT"
- uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
if: steps.pin.outputs.bump == 'true'
# Only `main` is a release branch: on any other ref (a manual run from a feature branch)
# semantic-release makes no release and exits 0, as it does when no commit since the last
# tag warrants a bump.
- name: Bump version, update the changelog, commit and tag
if: steps.pin.outputs.bump == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: uv run --locked semantic-release version --no-vcs-release
# HEAD is the release commit when semantic-release made one. An empty tag publishes
# nothing.
- name: Choose the tag to publish
id: publish
if: steps.pin.outputs.current == 'true' && github.ref == 'refs/heads/main'
env:
EVENT: ${{ github.event_name }}
run: |
if [ "$EVENT" = workflow_dispatch ]; then
tag=$(git describe --tags --abbrev=0 --match 'v[0-9]*' HEAD 2>/dev/null || true)
else
tag=$(git tag --points-at HEAD --list 'v[0-9]*' --sort=-version:refname | head -n 1)
fi
echo "tag=$tag" >> "$GITHUB_OUTPUT"
echo "Tag to publish: ${tag:-none}"
release:
needs: version
if: needs.version.outputs.tag != ''
runs-on: ubuntu-latest
permissions:
contents: write
env:
TAG: ${{ needs.version.outputs.tag }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
steps:
# The release tooling comes from the commit this run is for; the artifacts come from the
# tag, which may predate the tooling.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ needs.version.outputs.tag }}
path: tagged
persist-credentials: false
- uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
- name: Attach the wheel and the requirements to the GitHub release
run: |
has_files='[.assets[].name] | any(endswith("-py3-none-any.whl")) and any(. == "requirements.txt")'
if complete=$(gh release view "$TAG" --json assets --jq "$has_files"); then
if [ "$complete" = true ]; then
echo "$TAG already has its files"
exit 0
fi
release_exists=true
fi
uv build --directory tagged --wheel --out-dir "$PWD/dist"
uv export --directory tagged --locked --no-dev --no-emit-project --no-header \
--format requirements.txt --output-file "$PWD/dist/requirements.txt" --quiet
if [ "${release_exists:-false}" = true ]; then
gh release upload "$TAG" dist/* --clobber
else
python3 packaging/release_notes.py tagged/CHANGELOG.md "$TAG" > notes.md
gh release create "$TAG" dist/* --verify-tag --title "$TAG" --notes-file notes.md
fi
homebrew:
needs: [version, release]
runs-on: ubuntu-latest
permissions:
contents: read
env:
TAG: ${{ needs.version.outputs.tag }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: bkuberek/homebrew-tap
ref: main
path: tap
token: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
# The formula hashes the files the release serves, not a rebuild of them.
- name: Download the release files
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release download "$TAG" --repo "$GITHUB_REPOSITORY" --dir assets \
--pattern '*-py3-none-any.whl' --pattern requirements.txt
- name: Render the formula
run: |
python3 packaging/homebrew/render_formula.py \
--base-url "https://github.com/$GITHUB_REPOSITORY/releases/download/$TAG" \
--wheel assets/*-py3-none-any.whl \
--requirements assets/requirements.txt \
--output tap/Formula/cstudy.rb
ruby -c tap/Formula/cstudy.rb
- name: Commit and push the formula to the tap
working-directory: tap
run: |
git add Formula/cstudy.rb
if git diff --cached --quiet; then
echo "Formula/cstudy.rb is already at $TAG"
exit 0
fi
git -c user.name="github-actions[bot]" \
-c user.email="41898282+github-actions[bot]@users.noreply.github.com" \
commit --message "cstudy ${TAG#v}"
git push origin HEAD:main