|
1 | | -name: PyPI Publish |
| 1 | +name: Publish Python distribution |
2 | 2 |
|
3 | 3 | on: |
4 | | - release: |
5 | | - types: [created] |
6 | | - workflow_dispatch: |
7 | | - inputs: |
8 | | - test_release: |
9 | | - description: If true, publish to test.pypi.org |
10 | | - required: true |
11 | | - default: true |
12 | | - type: boolean |
| 4 | + push: |
| 5 | + branches: [master] |
| 6 | + tags: ["*"] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + |
| 11 | +# Publishing runs must not be cancelled: every default-branch push gets a |
| 12 | +# distinct TestPyPI version. |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.run_id }} |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +env: |
| 18 | + PACKAGE_NAME: onekey_client |
13 | 19 |
|
14 | 20 | jobs: |
| 21 | + quality: |
| 22 | + name: Quality checks |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v6 |
| 26 | + with: |
| 27 | + persist-credentials: false |
| 28 | + - uses: astral-sh/setup-uv@v7 |
| 29 | + with: |
| 30 | + enable-cache: true |
| 31 | + cache-dependency-glob: uv.lock |
| 32 | + - run: uv run ruff format --diff --exit-non-zero-on-format |
| 33 | + - run: uv run ruff check |
| 34 | + |
15 | 35 | build: |
16 | 36 | name: Build distribution |
| 37 | + needs: quality |
17 | 38 | runs-on: ubuntu-latest |
| 39 | + outputs: |
| 40 | + version: ${{ steps.package-version.outputs.version }} |
| 41 | + steps: |
| 42 | + - uses: actions/checkout@v6 |
| 43 | + with: |
| 44 | + persist-credentials: false |
| 45 | + - uses: actions/setup-python@v6 |
| 46 | + with: |
| 47 | + python-version: "3.10" |
| 48 | + - id: package-version |
| 49 | + name: Set TestPyPI development version |
| 50 | + if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 51 | + run: python .github/scripts/package_version.py --test-run-id "${{ github.run_id }}" |
| 52 | + - name: Read production version |
| 53 | + if: github.ref != format('refs/heads/{0}', github.event.repository.default_branch) |
| 54 | + run: python .github/scripts/package_version.py |
| 55 | + - name: Install build tools |
| 56 | + run: python -m pip install --user build twine |
| 57 | + - name: Build distribution packages |
| 58 | + run: python -m build |
| 59 | + - name: Check distribution metadata |
| 60 | + run: python -m twine check dist/* |
| 61 | + - uses: actions/upload-artifact@v5 |
| 62 | + with: |
| 63 | + name: python-package-distributions |
| 64 | + path: dist/ |
18 | 65 |
|
| 66 | + validate-release-tag: |
| 67 | + name: Validate release tag |
| 68 | + if: startsWith(github.ref, 'refs/tags/') |
| 69 | + runs-on: ubuntu-latest |
| 70 | + outputs: |
| 71 | + version: ${{ steps.version.outputs.version }} |
19 | 72 | steps: |
20 | 73 | - uses: actions/checkout@v6 |
21 | 74 | with: |
| 75 | + fetch-depth: 0 |
22 | 76 | persist-credentials: false |
23 | | - - name: Install uv |
24 | | - uses: astral-sh/setup-uv@v7 |
| 77 | + - id: version |
| 78 | + env: |
| 79 | + RELEASE_TAG: ${{ github.ref_name }} |
| 80 | + run: python .github/scripts/package_version.py --validate-release-tag "$RELEASE_TAG" |
| 81 | + - name: Require tag commit on the default branch |
| 82 | + env: |
| 83 | + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} |
| 84 | + run: | |
| 85 | + git fetch origin "$DEFAULT_BRANCH" |
| 86 | + git merge-base --is-ancestor "$GITHUB_SHA" "origin/$DEFAULT_BRANCH" |
| 87 | +
|
| 88 | + authorize-release-pusher: |
| 89 | + name: Authorize release pusher |
| 90 | + if: startsWith(github.ref, 'refs/tags/') |
| 91 | + runs-on: ubuntu-latest |
| 92 | + steps: |
| 93 | + - uses: actions/github-script@v8 |
25 | 94 | with: |
26 | | - enable-cache: true |
27 | | - cache-dependency-glob: "uv.lock" |
28 | | - - name: Build distribution |
29 | | - run: uv build |
30 | | - - name: Store distribution packages |
31 | | - uses: actions/upload-artifact@v5 |
| 95 | + script: | |
| 96 | + const username = context.payload.sender?.login; |
| 97 | + if (!username) core.setFailed("The tag push has no GitHub user identity."); |
| 98 | + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ |
| 99 | + owner: context.repo.owner, repo: context.repo.repo, username, |
| 100 | + }); |
| 101 | + if (!["maintain", "admin"].includes(data.role_name)) { |
| 102 | + core.setFailed(`${username} has ${data.role_name}, not maintain/admin.`); |
| 103 | + } |
| 104 | +
|
| 105 | + publish-to-testpypi: |
| 106 | + name: Publish distribution to TestPyPI |
| 107 | + if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) |
| 108 | + needs: build |
| 109 | + runs-on: ubuntu-latest |
| 110 | + environment: |
| 111 | + name: testpypi |
| 112 | + url: https://test.pypi.org/p/onekey_client |
| 113 | + permissions: |
| 114 | + id-token: write |
| 115 | + steps: |
| 116 | + - uses: actions/download-artifact@v6 |
32 | 117 | with: |
33 | 118 | name: python-package-distributions |
34 | 119 | path: dist/ |
| 120 | + - uses: pypa/gh-action-pypi-publish@release/v1 |
| 121 | + with: |
| 122 | + repository-url: https://test.pypi.org/legacy/ |
| 123 | + |
| 124 | + smoke-test-testpypi: |
| 125 | + name: Smoke test TestPyPI distribution |
| 126 | + needs: [build, publish-to-testpypi] |
| 127 | + runs-on: ubuntu-latest |
| 128 | + steps: |
| 129 | + - uses: actions/setup-python@v6 |
| 130 | + with: |
| 131 | + python-version: "3.10" |
| 132 | + - run: python -m venv .venv |
| 133 | + - name: Install the uploaded distribution |
| 134 | + env: |
| 135 | + VERSION: ${{ needs.build.outputs.version }} |
| 136 | + run: | |
| 137 | + for attempt in 1 2 3 4 5; do |
| 138 | + .venv/bin/python -m pip install --pre \ |
| 139 | + --index-url https://test.pypi.org/simple \ |
| 140 | + --extra-index-url https://pypi.org/simple \ |
| 141 | + "$PACKAGE_NAME==$VERSION" && exit 0 |
| 142 | + sleep 15 |
| 143 | + done |
| 144 | + exit 1 |
| 145 | + - run: .venv/bin/onekey --version |
35 | 146 |
|
36 | 147 | publish-to-pypi: |
37 | 148 | name: Publish distribution to PyPI |
38 | | - if: github.event_name == 'release' |
39 | | - needs: build |
| 149 | + if: startsWith(github.ref, 'refs/tags/') |
| 150 | + needs: [build, validate-release-tag, authorize-release-pusher] |
40 | 151 | runs-on: ubuntu-latest |
41 | 152 | environment: |
42 | 153 | name: pypi |
43 | 154 | url: https://pypi.org/p/onekey_client |
44 | 155 | permissions: |
45 | 156 | id-token: write |
46 | | - |
47 | 157 | steps: |
48 | | - - name: Download distribution packages |
49 | | - uses: actions/download-artifact@v6 |
| 158 | + - uses: actions/download-artifact@v6 |
50 | 159 | with: |
51 | 160 | name: python-package-distributions |
52 | 161 | path: dist/ |
53 | | - - name: Publish distribution to PyPI |
54 | | - uses: pypa/gh-action-pypi-publish@release/v1 |
| 162 | + - uses: pypa/gh-action-pypi-publish@release/v1 |
55 | 163 |
|
56 | | - publish-to-testpypi: |
57 | | - name: Publish distribution to TestPyPI |
58 | | - if: github.event_name == 'workflow_dispatch' && inputs.test_release |
59 | | - needs: build |
| 164 | + release: |
| 165 | + name: Create GitHub Release |
| 166 | + if: startsWith(github.ref, 'refs/tags/') |
| 167 | + needs: [build, validate-release-tag, authorize-release-pusher, publish-to-pypi] |
60 | 168 | runs-on: ubuntu-latest |
61 | | - environment: |
62 | | - name: testpypi |
63 | | - url: https://test.pypi.org/p/onekey_client |
64 | 169 | permissions: |
| 170 | + attestations: write |
| 171 | + contents: write |
65 | 172 | id-token: write |
66 | | - |
67 | 173 | steps: |
68 | | - - name: Download distribution packages |
69 | | - uses: actions/download-artifact@v6 |
| 174 | + - uses: actions/download-artifact@v6 |
70 | 175 | with: |
71 | 176 | name: python-package-distributions |
72 | 177 | path: dist/ |
73 | | - - name: Publish distribution to TestPyPI |
74 | | - uses: pypa/gh-action-pypi-publish@release/v1 |
| 178 | + - uses: actions/attest-build-provenance@v3 |
75 | 179 | with: |
76 | | - repository-url: https://test.pypi.org/legacy/ |
| 180 | + subject-path: dist/* |
| 181 | + - env: |
| 182 | + GH_TOKEN: ${{ github.token }} |
| 183 | + run: gh release create "$GITHUB_REF_NAME" --title "$GITHUB_REF_NAME" --generate-notes dist/* |
0 commit comments