|
8 | 8 | workflow_dispatch: |
9 | 9 | inputs: |
10 | 10 | test_pypi: |
11 | | - description: 'Publish to TestPyPI instead of PyPI' |
| 11 | + description: "Publish to TestPyPI instead of PyPI" |
12 | 12 | required: false |
13 | 13 | default: false |
14 | 14 | type: boolean |
15 | 15 |
|
| 16 | + # GitHub does not start new workflow runs for events caused by the default |
| 17 | + # GITHUB_TOKEN (e.g. gh release create in another workflow). After |
| 18 | + # "Release on merge" creates a release, trigger publish here instead. |
| 19 | + workflow_run: |
| 20 | + workflows: [Release on merge] |
| 21 | + types: [completed] |
| 22 | + |
| 23 | +permissions: |
| 24 | + contents: read |
| 25 | + id-token: write |
| 26 | + |
16 | 27 | jobs: |
| 28 | + gate: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + outputs: |
| 31 | + publish: ${{ steps.decide.outputs.publish }} |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@v4 |
| 34 | + if: github.event_name == 'workflow_run' |
| 35 | + with: |
| 36 | + ref: main |
| 37 | + |
| 38 | + - id: decide |
| 39 | + env: |
| 40 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + run: | |
| 42 | + set -euo pipefail |
| 43 | + if [[ "${{ github.event_name }}" != "workflow_run" ]]; then |
| 44 | + echo "publish=true" >> "${GITHUB_OUTPUT}" |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | + if [[ "${{ github.event.workflow_run.conclusion }}" != "success" ]]; then |
| 48 | + echo "publish=false" >> "${GITHUB_OUTPUT}" |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | + VERSION="$(grep -m1 '^version = ' pyproject.toml | cut -d'"' -f2)" |
| 52 | + TAG="v${VERSION}" |
| 53 | + if gh release view "${TAG}" --repo "${{ github.repository }}" >/dev/null 2>&1; then |
| 54 | + echo "publish=true" >> "${GITHUB_OUTPUT}" |
| 55 | + else |
| 56 | + echo "No GitHub release ${TAG} yet (or release job was skipped); skipping publish." |
| 57 | + echo "publish=false" >> "${GITHUB_OUTPUT}" |
| 58 | + fi |
| 59 | +
|
17 | 60 | build: |
18 | 61 | name: Build distribution |
| 62 | + needs: gate |
| 63 | + if: needs.gate.outputs.publish == 'true' |
19 | 64 | runs-on: ubuntu-latest |
20 | | - |
| 65 | + |
21 | 66 | steps: |
22 | 67 | - uses: actions/checkout@v4 |
23 | | - |
| 68 | + with: |
| 69 | + ref: ${{ github.event_name == 'workflow_run' && 'main' || github.event_name == 'release' && github.ref || 'main' }} |
| 70 | + |
24 | 71 | - name: Set up Python |
25 | 72 | uses: actions/setup-python@v5 |
26 | 73 | with: |
27 | | - python-version: '3.x' |
28 | | - |
| 74 | + python-version: "3.x" |
| 75 | + |
29 | 76 | - name: Install build dependencies |
30 | 77 | run: | |
31 | 78 | python -m pip install --upgrade pip |
32 | 79 | pip install build |
33 | | - |
| 80 | +
|
34 | 81 | - name: Build package |
35 | 82 | run: python -m build |
36 | | - |
| 83 | + |
37 | 84 | - name: Store distribution packages |
38 | 85 | uses: actions/upload-artifact@v4 |
39 | 86 | with: |
@@ -42,24 +89,24 @@ jobs: |
42 | 89 |
|
43 | 90 | publish-to-pypi: |
44 | 91 | name: Publish to PyPI |
45 | | - if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && inputs.test_pypi == false) |
| 92 | + if: >- |
| 93 | + github.event_name == 'workflow_run' || |
| 94 | + github.event_name == 'release' || |
| 95 | + (github.event_name == 'workflow_dispatch' && inputs.test_pypi == false) |
46 | 96 | needs: build |
47 | 97 | runs-on: ubuntu-latest |
48 | | - |
| 98 | + |
49 | 99 | environment: |
50 | 100 | name: pypi |
51 | 101 | url: https://pypi.org/p/python-proxy-headers |
52 | | - |
53 | | - permissions: |
54 | | - id-token: write # Required for trusted publishing |
55 | | - |
| 102 | + |
56 | 103 | steps: |
57 | 104 | - name: Download distribution packages |
58 | 105 | uses: actions/download-artifact@v4 |
59 | 106 | with: |
60 | 107 | name: python-package-distributions |
61 | 108 | path: dist/ |
62 | | - |
| 109 | + |
63 | 110 | - name: Publish to PyPI |
64 | 111 | uses: pypa/gh-action-pypi-publish@release/v1 |
65 | 112 |
|
@@ -68,21 +115,18 @@ jobs: |
68 | 115 | if: github.event_name == 'workflow_dispatch' && inputs.test_pypi == true |
69 | 116 | needs: build |
70 | 117 | runs-on: ubuntu-latest |
71 | | - |
| 118 | + |
72 | 119 | environment: |
73 | 120 | name: testpypi |
74 | 121 | url: https://test.pypi.org/p/python-proxy-headers |
75 | | - |
76 | | - permissions: |
77 | | - id-token: write |
78 | | - |
| 122 | + |
79 | 123 | steps: |
80 | 124 | - name: Download distribution packages |
81 | 125 | uses: actions/download-artifact@v4 |
82 | 126 | with: |
83 | 127 | name: python-package-distributions |
84 | 128 | path: dist/ |
85 | | - |
| 129 | + |
86 | 130 | - name: Publish to TestPyPI |
87 | 131 | uses: pypa/gh-action-pypi-publish@release/v1 |
88 | 132 | with: |
|