Repository navigation
158 lines (147 loc) · 6.27 KB
/
Copy pathpublish.yml
File metadata and controls
158 lines (147 loc) · 6.27 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
name: Publish
# What reaches PyPI is decided here and in the `pypi` environment, which
# setup_repo.py makes deployable from master only. Both triggers run with
# master as their ref, so the checks below are always master's own; a run from
# any other branch is refused the environment before it can publish.
on:
workflow_run:
workflows: ["Release Please"]
types: [completed]
# A manual publish, from master: gh workflow run publish.yml -f tag=<tag>
workflow_dispatch:
inputs:
tag:
description: "Release tag to publish (e.g. servicewright-v0.1.0)"
required: true
permissions:
contents: read
jobs:
check:
name: Check release
if: >
github.event_name == 'workflow_dispatch' ||
(github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'master')
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: read
actions: read # the CI run on the released commit
outputs:
tag: ${{ steps.release.outputs.tag }}
sha: ${{ steps.release.outputs.sha }}
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0 # master's history and every tag
# The tag names a commit; it is never the ref this run starts from. It has
# to be on master, and the version it carries has to be the one committed
# in pyproject.toml -- the artifacts are built from that file, so a tag
# that disagrees with it is a mistake to stop on, not to paper over.
- name: Resolve the release tag
id: release
env:
EVENT: ${{ github.event_name }}
REF: ${{ github.ref }}
INPUT_TAG: ${{ inputs.tag }}
PUSHED_SHA: ${{ github.event.workflow_run.head_sha }}
# Both tag conventions, the component-prefixed one and the bare one,
# so a Release Please config change cannot silently stop publishing.
TAG_PATTERN: '^(servicewright-)?v[0-9][0-9A-Za-z.+-]*$'
TAG_PREFIX: "servicewright-"
VERSION_FILE: "pyproject.toml"
run: |
if [ "$EVENT" = workflow_dispatch ]; then
if [ "$REF" != refs/heads/master ]; then
echo "::error::Publish from master, not $REF: gh workflow run publish.yml --ref master -f tag=<tag>"
exit 1
fi
if [[ ! "$INPUT_TAG" =~ $TAG_PATTERN ]]; then
echo "::error::'$INPUT_TAG' is not a release tag of this project"
exit 1
fi
TAG=$INPUT_TAG
if ! SHA=$(git rev-parse -q --verify "refs/tags/$TAG^{commit}"); then
echo "::error::There is no tag $TAG"
exit 1
fi
else
SHA=$PUSHED_SHA
TAG=$(git tag --points-at "$SHA" | grep -E "$TAG_PATTERN" | head -1 || true)
if [ -z "$TAG" ]; then
echo "No release tag on $SHA, nothing to publish."
exit 0
fi
fi
if ! git merge-base --is-ancestor "$SHA" origin/master; then
echo "::error::$TAG points at $SHA, which is not on master"
exit 1
fi
VERSION=${TAG#"$TAG_PREFIX"}
VERSION=${VERSION#v}
COMMITTED=$(git show "$SHA:$VERSION_FILE" | sed -n 's/^version = "\([^"]*\)".*/\1/p')
if [ "$COMMITTED" != "$VERSION" ]; then
echo "::error::$TAG is version $VERSION, but $VERSION_FILE at $SHA says '$COMMITTED'"
exit 1
fi
echo "Will publish $TAG ($SHA)"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
echo "sha=$SHA" >> "$GITHUB_OUTPUT"
# Release Please and CI start from the same push, and Release Please is
# done minutes earlier: without this wait a release is uploaded before CI
# has said anything about the commit being released. A version on PyPI
# cannot be replaced. If CI failed, fix or re-run it, then re-run this.
- name: Wait for CI on the released commit
if: steps.release.outputs.tag != ''
env:
GH_TOKEN: ${{ github.token }}
SHA: ${{ steps.release.outputs.sha }}
run: |
runs="repos/$GITHUB_REPOSITORY/actions/workflows/ci.yml/runs?head_sha=$SHA&event=push&branch=master"
RUN=
for _ in 1 2 3 4 5 6; do
RUN=$(gh api "$runs" --jq '.workflow_runs[0].id // empty' || true)
[ -n "$RUN" ] && break
sleep 20
done
if [ -z "$RUN" ]; then
echo "::error::No CI run for $SHA on master"
exit 1
fi
echo "Waiting for $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$RUN"
until [ "$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN" --jq .status)" = completed ]; do
sleep 20
done
CONCLUSION=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN" --jq .conclusion)
if [ "$CONCLUSION" != success ]; then
echo "::error::CI on $SHA ended '$CONCLUSION'"
exit 1
fi
echo "CI passed on $SHA"
publish:
name: Build and publish to PyPI
needs: check
if: needs.check.outputs.tag != ''
runs-on: ubuntu-latest
environment: pypi
permissions:
contents: read
id-token: write # Trusted Publisher (PyPI OIDC)
steps:
# The commit check resolved, not the tag: a tag moved since then changes nothing.
- uses: actions/checkout@v7
with:
ref: ${{ needs.check.outputs.sha }}
- uses: astral-sh/setup-uv@v7
- run: uv build
# PEP 740 provenance. uv publish uploads attestations but does not make
# them: this signs each distribution with the job's OIDC identity
# (Sigstore) into <file>.publish.attestation, and PyPI rejects an upload
# whose attestation does not match the Trusted Publisher.
- run: uvx --from 'pypi-attestations>=0.0.30' pypi-attestations sign dist/*.whl dist/*.tar.gz
# No file arguments: uv's default, dist/*, takes in the .attestation files
# too; naming the distributions would upload them without their
# attestations. --check-url: a run repeated after a failure skips what is
# already on the index instead of failing on it.
- run: uv publish --check-url https://pypi.org/simple/servicewright/