Skip to content

Commit 9cbf70e

Browse files
committed
Add automated release workflow
1 parent 2427c64 commit 9cbf70e

3 files changed

Lines changed: 222 additions & 113 deletions

File tree

‎.github/workflows/publish-pypi.yml‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
name: Publish to PyPI
2+
3+
# Run from the "main" branch and give the tag to publish (for example v7.1.0
4+
# or v7.1.0rc0). Publishing to PyPI waits for approval on the "pypi"
5+
# environment. The GitHub release is only created after PyPI accepts the files.
6+
on:
7+
workflow_dispatch:
8+
inputs:
9+
tag:
10+
description: Tag to publish (for example v7.1.0 or v7.1.0rc0)
11+
required: true
12+
type: string
13+
14+
# A second run of the same tag waits for the first run to finish
15+
concurrency:
16+
group: publish-${{ inputs.tag }}
17+
cancel-in-progress: false
18+
19+
permissions:
20+
contents: read
21+
22+
env:
23+
TAG: ${{ inputs.tag }}
24+
25+
jobs:
26+
validate:
27+
name: Validate tag
28+
runs-on: ubuntu-latest
29+
outputs:
30+
version: ${{ steps.tag.outputs.version }}
31+
prerelease: ${{ steps.tag.outputs.prerelease }}
32+
steps:
33+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
34+
with:
35+
fetch-depth: 0
36+
persist-credentials: false
37+
# Only vX.Y.Z and vX.Y.Z followed by aN, bN or rcN are publishable. The
38+
# optional suffix group is the single source of truth for "pre-release".
39+
- name: Check the tag format
40+
id: tag
41+
run: |
42+
if [[ ! "$TAG" =~ ^v([0-9]+\.[0-9]+\.[0-9]+)((a|b|rc)[0-9]+)?$ ]]; then
43+
echo "::error::Tag $TAG must look like vX.Y.Z, vX.Y.ZaN, vX.Y.ZbN or vX.Y.ZrcN"
44+
exit 1
45+
fi
46+
if [ -n "${BASH_REMATCH[2]}" ]; then prerelease=true; else prerelease=false; fi
47+
echo "version=${TAG#v}" >> "$GITHUB_OUTPUT"
48+
echo "prerelease=$prerelease" >> "$GITHUB_OUTPUT"
49+
echo "Tag $TAG: version ${TAG#v}, prerelease $prerelease"
50+
- name: Check that the tag exists and is on main
51+
run: |
52+
git rev-parse --verify --quiet "refs/tags/$TAG^{commit}" || {
53+
echo "::error::Tag $TAG does not exist"
54+
exit 1
55+
}
56+
git merge-base --is-ancestor "refs/tags/$TAG^{commit}" origin/main || {
57+
echo "::error::Tag $TAG is not reachable from main"
58+
exit 1
59+
}
60+
61+
build:
62+
name: Build files from the tag
63+
needs: validate
64+
runs-on: ubuntu-latest
65+
steps:
66+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
67+
with:
68+
ref: ${{ inputs.tag }}
69+
persist-credentials: false
70+
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
71+
with:
72+
python-version: "3.12"
73+
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
74+
with:
75+
node-version: "22"
76+
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
77+
- name: Build wheel and sdist
78+
run: |
79+
uv venv
80+
source .venv/bin/activate
81+
uv sync --locked --extra dev_build
82+
cd js
83+
npm ci
84+
npm run build
85+
cd ..
86+
python -m build --sdist --wheel -o dist
87+
# The build backend names both files after the version in pyproject.toml,
88+
# so this one check ties pyproject.toml, the wheel and the sdist to the tag.
89+
- name: Verify that the build matches the tag
90+
env:
91+
VERSION: ${{ needs.validate.outputs.version }}
92+
run: |
93+
expected=$(printf '%s\n' "plotly-$VERSION-py3-none-any.whl" "plotly-$VERSION.tar.gz")
94+
found=$(ls -1 dist | LC_ALL=C sort)
95+
if [ "$found" != "$expected" ]; then
96+
echo "::error::Tag $TAG expects these files in dist/, but found different ones"
97+
printf 'Expected:\n%s\nFound:\n%s\n' "$expected" "$found"
98+
exit 1
99+
fi
100+
- name: Extract release notes from CHANGELOG.md
101+
env:
102+
VERSION: ${{ needs.validate.outputs.version }}
103+
run: |
104+
# Print the lines under "## [<version>] - <date>" up to the next "## " heading
105+
awk -v heading="[$VERSION]" '
106+
$1 == "##" && $2 == heading { found = 1; next }
107+
found && /^## / { exit }
108+
found { print }
109+
' CHANGELOG.md > notes.md
110+
if ! grep -q '[^[:space:]]' notes.md; then
111+
echo "::error::CHANGELOG.md has no section for $VERSION"
112+
exit 1
113+
fi
114+
cat notes.md
115+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
116+
with:
117+
name: dist
118+
path: dist/
119+
if-no-files-found: error
120+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
121+
with:
122+
name: notes
123+
path: notes.md
124+
if-no-files-found: error
125+
126+
pypi:
127+
name: Upload to PyPI
128+
needs: [validate, build]
129+
runs-on: ubuntu-latest
130+
# Waits for a libraries_admin approval before the OIDC token is issued
131+
environment:
132+
name: pypi
133+
url: https://pypi.org/p/plotly
134+
# Signs this workflow so PyPI trusts it
135+
permissions:
136+
id-token: write
137+
steps:
138+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
139+
with:
140+
name: dist
141+
path: dist/
142+
- name: Publish package distributions to PyPI
143+
uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
144+
with:
145+
# A re-run of a tag already on PyPI skips instead of failing
146+
skip-existing: true
147+
148+
github-release:
149+
name: Create draft GitHub release
150+
needs: [validate, pypi]
151+
runs-on: ubuntu-latest
152+
# Kept apart from the pypi job so that the OIDC token and the write
153+
# access to the repository never share a job
154+
permissions:
155+
contents: write
156+
env:
157+
GH_TOKEN: ${{ github.token }}
158+
GH_REPO: ${{ github.repository }}
159+
PRERELEASE: ${{ needs.validate.outputs.prerelease }}
160+
steps:
161+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
162+
with:
163+
name: dist
164+
path: dist/
165+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
166+
with:
167+
name: notes
168+
path: .
169+
- name: Create the draft release
170+
run: |
171+
# gh release view cannot see drafts, so list releases instead
172+
existing=$(gh api "repos/$GH_REPO/releases" --paginate --jq ".[] | select(.tag_name == \"$TAG\") | .html_url")
173+
if [ -n "$existing" ]; then
174+
echo "::error::A release for $TAG exists already: $existing. Delete it and re-run."
175+
exit 1
176+
fi
177+
flags="--draft"
178+
if [ "$PRERELEASE" = "true" ]; then
179+
flags="$flags --prerelease"
180+
fi
181+
gh release create "$TAG" dist/* \
182+
--title "$TAG" \
183+
--notes-file notes.md \
184+
--verify-tag \
185+
$flags
186+
- name: Assert the pre-release flag matches the version
187+
run: |
188+
actual=$(gh api "repos/$GH_REPO/releases" --paginate --jq ".[] | select(.tag_name == \"$TAG\") | .prerelease")
189+
if [ "$actual" != "$PRERELEASE" ]; then
190+
echo "::error::Release $TAG has prerelease=$actual, but the version requires $PRERELEASE"
191+
exit 1
192+
fi
193+
echo "Draft release $TAG has prerelease=$actual, as the version requires"

‎.github/workflows/test-release.yml‎

Lines changed: 0 additions & 76 deletions
This file was deleted.

‎RELEASE.md‎

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This is the release process for releasing plotly.py version `X.Y.Z`, including c
1010
Ensure you have the following installed:
1111

1212
* `uv>=0.12.15`
13-
* `twine>=7.0.0`
1413
* A virtual environment: `uv venv`
14+
* Membership of the `libraries_admin` GitHub team, which approves publishing to PyPI
1515

1616
### Prepare local environment
1717

@@ -63,26 +63,35 @@ a link to the plotly.js CHANGELOG.
6363
$ git commit -m "version changes for vX.Y.Z"
6464
$ git push
6565
```
66-
- Create a GitHub pull request from `release-X.Y.Z` to `main` and wait for CI to be green
67-
- On the release branch, create and push a tag for the release:
66+
- Create a GitHub pull request from `release-X.Y.Z` to `main`, wait for CI to be green, and merge it
67+
- Create and push the tag on the merge commit on `main`. The publish workflow rejects tags that are not reachable from `main`:
6868
```sh
69+
$ git switch main && git pull
6970
$ git tag vX.Y.Z
7071
$ git push origin vX.Y.Z
7172
```
7273

74+
### Start the publish workflow
75+
76+
1. Go to the [Publish to PyPI workflow](https://github.com/plotly/plotly.py/actions/workflows/publish-pypi.yml) and choose "Run workflow".
77+
2. Leave the branch as `main` (the `pypi` environment only deploys from `main`) and enter the tag, for example `v7.1.0` or `v7.1.0rc0`.
78+
3. The workflow validates the tag, builds the wheel and sdist from it once, and checks that the wheel and sdist (which are named after the version in `pyproject.toml`) carry the tag's version. It then waits for approval of the `pypi` environment.
79+
80+
The accepted tag shapes are `vX.Y.Z`, `vX.Y.ZaN`, `vX.Y.ZbN` and `vX.Y.ZrcN`. Anything else (`.dev`, `.post`, `-rc.1`) is rejected before anything is built.
81+
7382
### Manual QA in Jupyter
7483
75-
We don't currently have automated tests for Jupyter, so we do this QA step manually.
84+
We don't currently have automated tests for Jupyter, so we do this QA step manually, while the workflow waits for approval.
7685

77-
The `full_build` job in the `release_build` workflow in CircleCI produces a tarball of artifacts `output.tgz`
78-
which you should download and decompress, which will give you a directory called `output`. The filenames within
79-
will contain version numbers; make sure the version numbers are correct.
86+
When the `build` job finishes, open the workflow run and download the `dist` artifact. The filenames
87+
will contain version numbers; make sure the version numbers are correct. These are the exact files
88+
that will be uploaded to PyPI and attached to the GitHub release.
8089

8190
Set up an environment with Jupyter, AnyWidget, and Pandas installed (`pip install jupyter anywidget pandas`). Then:
8291

83-
- unzip downloaded `output.tgz`
92+
- unzip the downloaded `dist` artifact
8493
- `pip uninstall plotly`
85-
- `pip install path/to/output/dist/plotly-X.Y.Z-py3-none-any.whl`
94+
- `pip install path/to/dist/plotly-X.Y.Z-py3-none-any.whl`
8695

8796
You'll want to check, in both JupyterLab (launch with `jupyter lab`) and Jupyter Notebook (launch with `jupyter notebook`),
8897
that `go.Figure()` and `go.FigureWidget()` work as expected.
@@ -115,29 +124,16 @@ figure_widget # Figure should render in notebook
115124
116125
Once these are verified working, you can move on to publishing the release.
117126
118-
### Merge the release PR and make a GitHub release
127+
### Publish the GitHub release
119128
120-
- Merge the pull request you created above into `main`
121-
- Go to https://github.com/plotly/plotly.py/releases and "Draft a new release"
122-
- Enter the `vX.Y.Z` tag you created already above and make "Release title" the same string as the tag.
123-
- Copy the changelog section for this version into "Describe this release"
124-
- Upload the build artifacts downloaded in the previous step (`.tar` and `.whl`)
129+
**Only members of the `libraries_admin` team can approve publishing to PyPI.**
125130
126-
### Publishing to PyPI
131+
1. In the workflow run, choose "Review deployments", select `pypi`, and approve. To abandon the release, reject instead; nothing is uploaded and no GitHub release is created.
132+
2. The workflow uploads the QA'd files to PyPI with trusted publishing (no API token needed).
133+
3. Only after PyPI accepts the files, the workflow creates a draft GitHub release for the tag with the same files attached and the notes copied from the version's section of `CHANGELOG.md`. The pre-release flag is set from the version (`a`, `b` and `rc` versions are pre-releases), and the workflow fails if the flag and the version disagree.
134+
4. Review the notes on the draft release, then publish it. Publishing the draft triggers nothing further.
127135
128-
The final step is to publish the release to PyPI. **You will need special permissions from Plotly leadership to do this.**
129-
130-
You must first install [Twine](https://pypi.org/project/twine/) (`pip install twine`) if not already installed.
131-
132-
Publishing to PyPI:
133-
```bash
134-
(plotly_dev) $ cd path/to/output
135-
(plotly_dev) $ twine check plotly-X.Y.Z*
136-
(plotly_dev) $ twine upload plotly-X.Y.Z*
137-
```
138-
139-
You will be prompted to enter an API token; this can be generated in your PyPI account settings.
140-
Your account must have permissions to publish to the `plotly` project on PyPI.
136+
If the upload step fails, no GitHub release is created. Fix the cause and re-run the workflow for the same tag; files already on PyPI are skipped. If the GitHub release step fails after PyPI succeeded, delete any partial draft release and re-run.
141137
142138
### Update documentation site
143139
@@ -177,13 +173,9 @@ to features in the release.
177173

178174
(rough notes for a rough/ad hoc process!)
179175

180-
It's the same process as above except that the `X.Y.Z` version has a suffix and there are special instructions below for publishing an RC: note that the `npm` suffix is `-rc.1` and the PyPI suffix is `rc1`. We also don't update the docs with RC information and we inform a limited number of stakeholders.
176+
It's the same process as above except that the version has an `a`, `b` or `rc` suffix, for example `python commands.py bumpversion 7.1.0rc0` and tag `v7.1.0rc0`. Alpha and beta versions work the same way. The `npm` form of the version uses a dash (`7.1.0-rc0`) and `npm version` normalizes it automatically during `bumpversion`. We also don't update the docs with RC information and we inform a limited number of stakeholders.
181177

182-
PyPI RC (no special flags, just the `rc1` suffix):
183-
184-
```bash
185-
(plotly_dev) $ twine upload dist/plotly-X.Y.Zrc1*
186-
```
178+
The publish workflow derives "pre-release" from the version, so no extra flags are needed. A pre-release is uploaded to PyPI as a pre-release and its GitHub release is flagged as a pre-release. A stable version is never flagged as a pre-release, and the other way round.
187179

188-
The `rc1` suffix ensures that users won't install this version by default —
189-
they must explicitly request it (e.g., `pip install plotly==X.Y.Zrc1`).
180+
Pre-release versions are not installed by default;
181+
users must explicitly request them (e.g., `pip install plotly==X.Y.ZrcN`).

0 commit comments

Comments
 (0)