|
2 | 2 | name: Release |
3 | 3 |
|
4 | 4 | on: |
5 | | - push: |
6 | | - tags: |
7 | | - - v*.*.* |
| 5 | + workflow_run: |
| 6 | + workflows: |
| 7 | + - "CI" |
| 8 | + types: |
| 9 | + - completed |
8 | 10 |
|
9 | 11 | jobs: |
| 12 | + validate-tag: |
| 13 | + name: Check tag |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + valid_tag: ${{ steps.validation.outputs.valid_tag }} |
| 17 | + if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push' && startsWith(github.event.workflow_run.head_branch, 'v') }} |
| 18 | + steps: |
| 19 | + - name: Check out the repository including tags |
| 20 | + uses: actions/checkout@v5 |
| 21 | + with: |
| 22 | + ref: ${{ github.event.workflow_run.head_sha }} |
| 23 | + fetch-depth: 0 |
| 24 | + fetch-tags: true |
| 25 | + - name: Validate tag |
| 26 | + id: validation |
| 27 | + run: | |
| 28 | + # Validation is necessary in the unlikely case that a branch matching the tag naming pattern is pushed |
| 29 | + # and the CI workflow in that branch is modified to run upon a push to that branch |
| 30 | + REF='${{ github.event.workflow_run.head_branch }}' # This can be a branch or tag name |
| 31 | + if [[ "$REF" != v*.*.* ]]; then |
| 32 | + echo "valid_tag=false" >> "$GITHUB_OUTPUT"; exit 0 |
| 33 | + fi |
| 34 | + # Validate that the tag exists |
| 35 | + if ! git rev-parse -q --verify "refs/tags/$REF" >/dev/null; then |
| 36 | + echo "There is no tag matching $REF - $REF is a branch" |
| 37 | + echo "valid_tag=false" >> "$GITHUB_OUTPUT"; exit 0 |
| 38 | + fi |
| 39 | + # Validate that the tag is for the same commit that was pushed |
| 40 | + TAG_SHA="$(git rev-parse "$REF^{commit}")" |
| 41 | + COMMIT_SHA="${{ github.event.workflow_run.head_sha }}" |
| 42 | + if [ "$TAG_SHA" != "$COMMIT_SHA" ]; then |
| 43 | + echo "Tag SHA $TAG_SHA does not match pushed commit SHA $COMMIT_SHA" |
| 44 | + echo "valid_tag=false" >> "$GITHUB_OUTPUT"; exit 0 |
| 45 | + fi |
| 46 | + echo "Tag $REF exists and is valid. Tag $TAG_SHA matches the pushed commit $COMMIT_SHA." |
| 47 | + echo "valid_tag=true" >> "$GITHUB_OUTPUT" |
10 | 48 | publish: |
11 | 49 | name: Publish to PyPI |
| 50 | + needs: validate-tag |
12 | 51 | runs-on: ubuntu-latest |
| 52 | + if: ${{ needs.validate-tag.outputs.valid_tag == 'true' && github.event.workflow_run.conclusion == 'success' }} |
13 | 53 | steps: |
14 | | - - name: Wait for tests to succeed |
15 | | - |
16 | | - timeout-minutes: 15 |
17 | | - with: |
18 | | - ref: 'refs/heads/main' |
19 | | - running-workflow-name: 'Publish to PyPI' |
20 | | - repo-token: ${{ secrets.GITHUB_TOKEN }} |
21 | | - wait-interval: 10 |
22 | | - allowed-conclusions: success |
23 | | - |
24 | 54 | - uses: actions/checkout@v5 |
25 | 55 |
|
26 | 56 | - name: Set up Python |
|
0 commit comments