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