Skip to content

Commit 1fcf646

Browse files
committed
ci: automate releases with release-please and publish to PyPI
release-please (release bot App token) keeps a release pull request that writes CHANGELOG.md; merging it pushes vX.Y.Z and a draft release. The tag starts release.yml, which builds the sdist and wheel on GitHub-hosted runners, attests their provenance, publishes to PyPI with trusted publishing from the pypi environment (skipped with a warning until PYPI_TRUSTED_PUBLISHING is true), then uploads the files to the release and publishes it. The version comes from the tag through setuptools_scm, so the simple release type only maintains the changelog and the manifest. Dependabot commits now use the ci and build prefixes. CONTRIBUTING documents Conventional Commits, CI and the one-time PyPI setup. Signed-off-by: NodeRings <17057583+shaygun@users.noreply.github.com>
1 parent 580f764 commit 1fcf646

6 files changed

Lines changed: 230 additions & 21 deletions

File tree

‎.github/dependabot.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
# Commit messages use Conventional Commits so release-please can sort them.
2+
# Both groups only touch CI and development tools, so they stay out of the
3+
# changelog ("ci" and "build" are hidden sections).
14
version: 2
25
updates:
36
- package-ecosystem: "github-actions"
47
directory: "/"
58
schedule:
69
interval: "weekly"
10+
day: "monday"
11+
time: "09:00"
12+
timezone: "Etc/UTC"
13+
commit-message:
14+
prefix: "ci"
715
groups:
816
"github-actions":
917
patterns:
@@ -12,6 +20,11 @@ updates:
1220
directory: "/"
1321
schedule:
1422
interval: "weekly"
23+
day: "monday"
24+
time: "09:00"
25+
timezone: "Etc/UTC"
26+
commit-message:
27+
prefix: "build"
1528
groups:
1629
"pip":
1730
patterns:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: release-please
2+
3+
# Keeps one open release pull request that collects the Conventional Commits
4+
# merged since the last release into CHANGELOG.md and picks the next version.
5+
# Merging it pushes the vX.Y.Z tag and a draft GitHub release; the tag starts
6+
# release.yml, which builds the package (setuptools_scm reads the version from
7+
# the tag), publishes it to PyPI and then publishes the GitHub release.
8+
#
9+
# Needs the organization's release bot GitHub App, because tags and pull
10+
# requests made with the workflow's own GITHUB_TOKEN do not start other
11+
# workflows:
12+
# variable RELEASE_BOT_APP_ID (or RELEASE_BOT_CLIENT_ID) and
13+
# secret RELEASE_BOT_PRIVATE_KEY.
14+
"on":
15+
push:
16+
branches: [main]
17+
workflow_dispatch:
18+
19+
permissions:
20+
contents: read
21+
22+
concurrency:
23+
group: release-please
24+
cancel-in-progress: false
25+
26+
jobs:
27+
release-please:
28+
runs-on: ubuntu-latest
29+
timeout-minutes: 10
30+
steps:
31+
- name: Check the release bot is set up
32+
id: bot
33+
env:
34+
APP_ID: ${{ vars.RELEASE_BOT_APP_ID }}
35+
CLIENT_ID: ${{ vars.RELEASE_BOT_CLIENT_ID }}
36+
HAS_KEY: ${{ secrets.RELEASE_BOT_PRIVATE_KEY != '' }}
37+
run: |
38+
if { [ -n "${APP_ID}" ] || [ -n "${CLIENT_ID}" ]; } && [ "${HAS_KEY}" = "true" ]; then
39+
echo "ready=true" >> "$GITHUB_OUTPUT"
40+
else
41+
echo "::warning title=release-please skipped::Set the variable RELEASE_BOT_APP_ID and secret RELEASE_BOT_PRIVATE_KEY (see CONTRIBUTING.md, Releasing)."
42+
echo "ready=false" >> "$GITHUB_OUTPUT"
43+
fi
44+
- uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0
45+
if: steps.bot.outputs.ready == 'true'
46+
id: app
47+
with:
48+
# GitHub accepts either the App ID or the Client ID here.
49+
client-id: ${{ vars.RELEASE_BOT_CLIENT_ID || vars.RELEASE_BOT_APP_ID }}
50+
private-key: ${{ secrets.RELEASE_BOT_PRIVATE_KEY }}
51+
- uses: googleapis/release-please-action@45996ed1f6d02564a971a2fa1b5860e934307cf7 # v5.0.0
52+
if: steps.bot.outputs.ready == 'true'
53+
with:
54+
token: ${{ steps.app.outputs.token }}
55+
config-file: release-please-config.json
56+
manifest-file: .release-please-manifest.json

‎.github/workflows/release.yml‎

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,128 @@
1-
# Publishes a release to PyPI with trusted publishing (no stored token).
2-
# One-time setup on pypi.org: add a pending publisher for project
3-
# "saltext.noderings", owner "noderings", repository "saltext-noderings",
4-
# workflow "release.yml", environment "release".
1+
# Builds and publishes a release when release-please pushes a vX.Y.Z tag.
2+
#
3+
# 1. build: sdist and wheel from the tag (setuptools_scm turns the tag into
4+
# the version) with build provenance attestations.
5+
# 2. pypi: publishes to https://pypi.org/project/saltext.noderings/ with
6+
# trusted publishing (no stored token) from the "pypi" environment. Skipped
7+
# with a warning until the repository variable PYPI_TRUSTED_PUBLISHING is
8+
# "true" (set it once the pending publisher exists on pypi.org, see
9+
# CONTRIBUTING.md, Releasing).
10+
# 3. github-release: uploads the files to the draft release made by
11+
# release-please and publishes it.
12+
#
13+
# Always runs on GitHub-hosted runners: release builds never touch the
14+
# self-hosted runner.
515
name: Release
616

7-
on:
17+
"on":
818
push:
919
tags: ["v*.*.*"]
20+
workflow_dispatch: # run it from a tag (Use workflow from: Tags) to retry a release
1021

1122
permissions:
12-
contents: write
23+
contents: read
24+
25+
concurrency:
26+
group: release-${{ github.ref_name }}
27+
cancel-in-progress: false
28+
29+
env:
30+
TAG: ${{ github.ref_name }}
1331

1432
jobs:
1533
build:
34+
name: Build
1635
runs-on: ubuntu-latest
36+
timeout-minutes: 15
37+
permissions:
38+
contents: read
39+
id-token: write
40+
attestations: write
1741
steps:
18-
- uses: actions/checkout@v7
42+
- name: Check this runs for a version tag
43+
run: |
44+
case "${TAG}" in
45+
v[0-9]*.[0-9]*.[0-9]*) ;;
46+
*) echo "::error::Run this workflow from a vX.Y.Z tag, not ${TAG}."; exit 1 ;;
47+
esac
48+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
1949
with:
2050
fetch-depth: 0
21-
- uses: actions/setup-python@v7
51+
persist-credentials: false
52+
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
2253
with:
2354
python-version: "3.12"
24-
- run: pip install build
55+
- run: python -m pip install build twine
2556
- run: python -m build
26-
- uses: actions/upload-artifact@v7
57+
- run: python -m twine check --strict dist/*
58+
- name: Check the version matches the tag
59+
run: |
60+
want="${TAG#v}"
61+
ls dist
62+
test -f "dist/saltext_noderings-${want}.tar.gz" || test -f "dist/saltext.noderings-${want}.tar.gz" \
63+
|| { echo "::error::The sdist version does not match ${TAG}."; exit 1; }
64+
ls dist/*-"${want}"-py*.whl >/dev/null || { echo "::error::The wheel version does not match ${TAG}."; exit 1; }
65+
- uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2
66+
with:
67+
subject-path: dist/*
68+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
2769
with:
2870
name: dist
2971
path: dist/
72+
if-no-files-found: error
3073

3174
pypi:
75+
name: Publish to PyPI
3276
needs: build
3377
runs-on: ubuntu-latest
34-
environment: release
78+
timeout-minutes: 10
79+
environment:
80+
name: pypi
81+
url: https://pypi.org/project/saltext.noderings/
3582
permissions:
3683
id-token: write
3784
steps:
38-
- uses: actions/download-artifact@v8
85+
- name: Check PyPI trusted publishing is set up
86+
id: check
87+
env:
88+
ENABLED: ${{ vars.PYPI_TRUSTED_PUBLISHING }}
89+
run: |
90+
if [ "${ENABLED}" = "true" ]; then
91+
echo "publish=true" >> "$GITHUB_OUTPUT"
92+
else
93+
echo "::warning title=PyPI skipped::Add the pending publisher on pypi.org, then set the repository variable PYPI_TRUSTED_PUBLISHING=true (see CONTRIBUTING.md, Releasing). Re-run this workflow from the tag afterwards."
94+
echo "publish=false" >> "$GITHUB_OUTPUT"
95+
fi
96+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
97+
if: steps.check.outputs.publish == 'true'
3998
with:
4099
name: dist
41100
path: dist/
42-
- uses: pypa/gh-action-pypi-publish@release/v1
101+
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
102+
if: steps.check.outputs.publish == 'true'
103+
with:
104+
skip-existing: true
43105

44106
github-release:
45-
needs: pypi
107+
name: GitHub release
108+
needs: [build, pypi]
109+
if: always() && needs.build.result == 'success' && needs.pypi.result != 'failure' && needs.pypi.result != 'cancelled'
46110
runs-on: ubuntu-latest
111+
timeout-minutes: 10
112+
permissions:
113+
contents: write
47114
steps:
48-
- uses: actions/download-artifact@v8
115+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
49116
with:
50117
name: dist
51118
path: dist/
52-
- env:
119+
- name: Upload the files and publish the release
120+
env:
53121
GH_TOKEN: ${{ github.token }}
54-
run: gh release create "$GITHUB_REF_NAME" dist/* --repo "$GITHUB_REPOSITORY" --title "$GITHUB_REF_NAME" --notes "See CHANGELOG.md."
122+
GH_REPO: ${{ github.repository }}
123+
run: |
124+
if ! gh release view "${TAG}" >/dev/null 2>&1; then
125+
gh release create "${TAG}" --verify-tag --draft --title "${TAG}" --notes "See CHANGELOG.md."
126+
fi
127+
gh release upload "${TAG}" dist/* --clobber
128+
gh release edit "${TAG}" --draft=false --latest

‎.release-please-manifest.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{".": "0.0.0"}

‎CONTRIBUTING.md‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,51 @@ Optional settings for the live tests: `NODERINGS_TEST_DOMAIN` (parent of the thr
3838
Run everything CI runs with nox:
3939

4040
```bash
41-
nox -e tests lint lint-tests docs
41+
nox -e tests-3.12 lint docs
42+
pre-commit run --all-files
4243
```
4344

44-
## Changelog
45+
## Commit messages
4546

46-
Add a fragment for user-visible changes: `changelog/<issue number>.<type>.md`, where type is `added`, `changed`, `fixed`, `deprecated`, `removed`, `security` or `breaking`. Releases collect them with towncrier.
47+
Use [Conventional Commits](https://www.conventionalcommits.org/). release-please reads them to pick the next version and write `CHANGELOG.md`:
48+
49+
| Prefix | Changelog section | Version bump (before 1.0) |
50+
|---|---|---|
51+
| `feat:` | Features | minor |
52+
| `fix:` | Bug Fixes | patch |
53+
| `perf:` | Performance | patch |
54+
| `docs:` | Documentation | patch |
55+
| `deps:` | Dependencies | patch |
56+
| `ci:`, `build:`, `chore:`, `test:`, `refactor:` | hidden | none |
57+
| `feat!:` or a `BREAKING CHANGE:` footer | Breaking changes | minor before 1.0, major after |
58+
59+
The changelog is generated, so there are no towncrier fragments to add (the `changelog/` directory and its towncrier settings come from the copier template and are not used).
60+
61+
## CI
62+
63+
`.github/workflows/ci.yml` runs pre-commit, the unit tests on Salt 3006 to 3008, the docs build and a package check. A single **CI result** check is required on `main`. CI runs on GitHub-hosted runners until a NodeRings self-hosted runner exists and the variable `NODERINGS_RUNNER` is `true`. See [.github/self-hosted-runner.md](.github/self-hosted-runner.md).
4764

4865
## Releases
4966

50-
Push a tag such as `v0.2.0`. The release workflow builds the package and publishes it to PyPI with trusted publishing, then publishes the docs.
67+
1. Merge pull requests into `main`. release-please (`.github/workflows/release-please.yml`, using the noderings-release-bot GitHub App) keeps a pull request named `chore: release X.Y.Z` up to date.
68+
2. Merge that pull request when you want to release. release-please pushes the tag `vX.Y.Z` and creates a draft GitHub release.
69+
3. The tag starts `.github/workflows/release.yml`, which:
70+
- builds the sdist and wheel (setuptools_scm reads the version from the tag) and attests their build provenance
71+
- publishes them to PyPI with trusted publishing from the `pypi` environment
72+
- uploads them to the GitHub release and publishes it
73+
4. The docs on GitHub Pages update on every push to `main`.
74+
75+
Do not create tags or releases by hand.
76+
77+
### One-time setup
78+
79+
| Where | What |
80+
|---|---|
81+
| GitHub (done) | Variable `RELEASE_BOT_APP_ID`, secret `RELEASE_BOT_PRIVATE_KEY`, environment `pypi` limited to `v*` tags |
82+
| pypi.org, account `noderings` → *Your account* → *Publishing* → *Add a new pending publisher* (GitHub) | PyPI project name `saltext.noderings`, owner `noderings`, repository `saltext-noderings`, workflow name `release.yml`, environment name `pypi` |
83+
| GitHub → Settings → Secrets and variables → Actions → Variables | `PYPI_TRUSTED_PUBLISHING` = `true` (until then the PyPI step is skipped with a warning) |
84+
85+
A pending publisher turns into a normal trusted publisher with the first upload, which also creates the project on PyPI. If a release ran before PyPI was ready, open Actions → Release → *Run workflow*, pick the tag under *Use workflow from*, and run it again. Files already on PyPI are skipped.
5186

5287
## Code of conduct
5388

‎release-please-config.json‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
3+
"release-type": "python",
4+
"include-v-in-tag": true,
5+
"include-component-in-tag": false,
6+
"bump-minor-pre-major": true,
7+
"bump-patch-for-minor-pre-major": false,
8+
"draft": true,
9+
"force-tag-creation": true,
10+
"bootstrap-sha": "a521b6d30b39d97d05a2378d0edd4364cad1277d",
11+
"pull-request-title-pattern": "chore: release ${version}",
12+
"changelog-sections": [
13+
{ "type": "feat", "section": "Features" },
14+
{ "type": "fix", "section": "Bug Fixes" },
15+
{ "type": "perf", "section": "Performance" },
16+
{ "type": "deps", "section": "Dependencies" },
17+
{ "type": "docs", "section": "Documentation" },
18+
{ "type": "refactor", "section": "Other", "hidden": true },
19+
{ "type": "chore", "section": "Other", "hidden": true },
20+
{ "type": "ci", "section": "Other", "hidden": true },
21+
{ "type": "test", "section": "Other", "hidden": true },
22+
{ "type": "build", "section": "Other", "hidden": true }
23+
],
24+
"packages": {
25+
".": {
26+
"package-name": "saltext.noderings",
27+
"changelog-path": "CHANGELOG.md"
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)