Repository navigation
Expand file tree
/
Copy pathrelease-please-python.yml
More file actions
169 lines (168 loc) · 8.49 KB
/
Copy pathrelease-please-python.yml
File metadata and controls
169 lines (168 loc) · 8.49 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
# Place at .github/workflows/release.yml in an adopting repository: a published Python package (graph) and a
# root component that depends on it from PyPI. Variant of examples/release-please.yml for that case.
# One push on main runs, in order: tag graph -> build -> publish to PyPI -> verify the root's public
# dependencies install -> tag root (and publish its GitHub Release) -> release-please.
# The root tag, hence its Release, only exists once the published dependency is installable.
# Register this exact workflow filename and the `pypi` environment (limited to the main branch) as the
# PyPI Trusted Publisher. Replace example-graph, graph/ and the verification command with your own.
name: Release
on:
push:
branches: [main]
# Recovery when GitHub created no run for a release push: replays that push's validation on the given commit.
workflow_dispatch:
inputs:
release-commit:
description: Full SHA of the release commit on main whose push produced no run
type: string
required: true
permissions:
contents: read
# One run per pushed commit, never cancelled or replaced: release-tag validates only before..after, so a replaced run would leave its bump untagged.
concurrency:
group: release-${{ inputs.release-commit || github.sha }}
cancel-in-progress: false
# Keep this per-commit group: a ref-level group would replace a pending run and drop its release bump.
# Recover from a failed job with "Re-run failed jobs"; prepare only uploads the files still missing, and verify
# waits up to 660 s for the registry, so a re-run is only needed after a real failure.
jobs:
tag-graph:
permissions:
contents: write
# Replace with the full merged, reviewed github-workflows commit SHA (the same SHA for every job below).
uses: BehindTheMusicTree/github-workflows/.github/workflows/release-tag.yml@REPLACE_WITH_MERGED_COMMIT_SHA
with:
app-id: ${{ vars.RELEASE_APP_ID }}
release-policy-path: .github/graph-release-policy.json
secrets:
release-app-private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
build-graph:
needs: tag-graph
# The tag is empty when this push carries no graph release; skipped dependents are handled below.
if: needs.tag-graph.outputs.tag != ''
uses: BehindTheMusicTree/github-workflows/.github/workflows/publish-python.yml@REPLACE_WITH_MERGED_COMMIT_SHA
with:
release-tag: ${{ needs.tag-graph.outputs.tag }}
package-name: example-graph
package-directory: graph
release-policy-path: .github/graph-release-policy.json
artifact-name: graph-distributions
publish-graph:
needs: [tag-graph, build-graph]
if: needs.tag-graph.outputs.tag != ''
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/example-graph
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v5
with:
repository: BehindTheMusicTree/github-workflows
ref: ${{ needs.build-graph.outputs.foundation-sha }}
path: foundation
persist-credentials: false
- uses: actions/setup-python@v6
with:
python-version: '3.12'
- uses: actions/download-artifact@v4
with:
name: ${{ needs.build-graph.outputs.artifact-name }}
path: dist
- name: Verify downloaded files and check PyPI retries
id: retry
env:
VERSION: ${{ needs.build-graph.outputs.version }}
REGISTRY: ${{ needs.build-graph.outputs.registry }}
run: python3 foundation/release/python-artifact.py prepare example-graph "$VERSION" dist pending --registry "$REGISTRY"
- name: Publish missing distributions with Trusted Publishing
if: steps.retry.outputs.upload-needed == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: pending/
skip-existing: false
- name: Verify the complete published file set
env:
VERSION: ${{ needs.build-graph.outputs.version }}
REGISTRY: ${{ needs.build-graph.outputs.registry }}
run: python3 foundation/release/python-artifact.py verify example-graph "$VERSION" dist --registry "$REGISTRY"
verify-public-dependency:
needs: [tag-graph, publish-graph]
# Without a graph tag (root-only release) publish-graph is skipped by design. With one, it must have succeeded:
# a failed build-graph also leaves publish-graph "skipped", so its result alone cannot tell the cases apart.
if: ${{ !cancelled() && needs.tag-graph.result == 'success' && (needs.tag-graph.outputs.tag == '' || needs.publish-graph.result == 'success') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
with:
ref: ${{ inputs.release-commit || github.sha }}
persist-credentials: false
- uses: actions/setup-python@v6
with:
python-version: '3.12'
- name: Install the root from public indexes only
env:
GRAPH_TAG: ${{ needs.tag-graph.outputs.tag }}
run: |
# The installer's Simple index page is cached for up to 600 s after an upload, and pip happily
# installs an older graph that the root's range still allows. Pin the exact version just
# published (same source as the check below) so a stale page fails, then retry up to 660 s.
# Without a graph tag in this run nothing was just published: the version is already public,
# so a failure is real and a single attempt is enough.
GRAPH_VERSION=$(python3 -c "import tomllib; print(tomllib.load(open('graph/pyproject.toml', 'rb'))['project']['version'])")
if [ -n "$GRAPH_TAG" ]; then deadline=$((SECONDS + 660)); else deadline=$SECONDS; fi
attempt=1
while true; do
python3 -m pip install --no-cache-dir . "example-graph==$GRAPH_VERSION" && exit 0
if [ "$SECONDS" -ge "$deadline" ]; then
echo "example-graph $GRAPH_VERSION is not installable (${SECONDS}s elapsed)" >&2
if [ -z "$GRAPH_TAG" ]; then
echo "If the push that released it produced no run, dispatch this workflow with release-commit=<that commit's SHA>; once its verify-public-dependency succeeds, re-run this run's failed jobs" >&2
fi
exit 1
fi
echo "example-graph $GRAPH_VERSION not yet installable (attempt $attempt, ${SECONDS}s elapsed)" >&2
attempt=$((attempt + 1))
sleep 30
done
- name: Require the intended graph package version
# Explicit invariant: fails with a clear message if the exact pin above is ever removed.
run: |
python3 - <<'PY'
from importlib.metadata import version
from pathlib import Path
import tomllib
expected = tomllib.loads(Path('graph/pyproject.toml').read_text())['project']['version']
if version('example-graph') != expected:
raise SystemExit('Publish the intended graph package version before releasing the root')
PY
tag-root:
needs: verify-public-dependency
if: ${{ !cancelled() && needs.verify-public-dependency.result == 'success' }}
# contents: write lets release-tag.yml publish the GitHub Release once the dependency is verified.
permissions:
contents: write
uses: BehindTheMusicTree/github-workflows/.github/workflows/release-tag.yml@REPLACE_WITH_MERGED_COMMIT_SHA
with:
app-id: ${{ vars.RELEASE_APP_ID }}
release-policy-path: .github/release-policy.json
secrets:
release-app-private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
release-please:
# Explicit condition: the default success() also looks at transitive ancestors, so a skipped publish-graph
# (no graph release in this push) would skip this job. tag-root success implies verify-public-dependency
# success, so a real upstream failure still blocks the release PR reconciliation until rerun.
needs: [tag-graph, tag-root]
if: ${{ !cancelled() && needs.tag-graph.result == 'success' && needs.tag-root.result == 'success' }}
uses: BehindTheMusicTree/github-workflows/.github/workflows/release-please.yml@REPLACE_WITH_MERGED_COMMIT_SHA
with:
app-id: ${{ vars.RELEASE_PR_APP_ID }}
release-policies: |
.github/release-policy.json
.github/graph-release-policy.json
# Publishes without human review once the required checks pass; see README "Auto-merging release PRs".
# auto-merge: true
secrets:
release-pr-app-private-key: ${{ secrets.RELEASE_PR_APP_PRIVATE_KEY }}