|
| 1 | +name: Python Semantic Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +jobs: |
| 8 | + run_tests: |
| 9 | + uses: ./.github/workflows/ci.yml |
| 10 | + |
| 11 | + release: |
| 12 | + needs: run_tests |
| 13 | + runs-on: ubuntu-latest |
| 14 | + if: github.ref_name == 'main' |
| 15 | + concurrency: |
| 16 | + group: ${{ github.workflow }}-release-${{ github.ref_name }} |
| 17 | + cancel-in-progress: false |
| 18 | + |
| 19 | + permissions: |
| 20 | + contents: write |
| 21 | + |
| 22 | + steps: |
| 23 | + # Note: We checkout the repository at the branch that triggered the workflow. |
| 24 | + # Python Semantic Release will automatically convert shallow clones to full clones |
| 25 | + # if needed to ensure proper history evaluation. However, we forcefully reset the |
| 26 | + # branch to the workflow sha because it is possible that the branch was updated |
| 27 | + # while the workflow was running, which prevents accidentally releasing un-evaluated |
| 28 | + # changes. |
| 29 | + - name: Setup | Checkout Repository on Release Branch |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + ref: ${{ github.ref_name }} |
| 33 | + |
| 34 | + - name: Setup | Force release branch to be at workflow sha |
| 35 | + run: | |
| 36 | + git reset --hard ${{ github.sha }} |
| 37 | +
|
| 38 | + - name: Action | Semantic Version Release |
| 39 | + id: release |
| 40 | + # Adjust tag with desired version if applicable. |
| 41 | + uses: python-semantic-release/[email protected] |
| 42 | + with: |
| 43 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 44 | + git_committer_name: "github-actions" |
| 45 | + git_committer_email: "[email protected]" |
| 46 | + working-directory: './backend' |
| 47 | + |
| 48 | + - name: Publish | Upload to GitHub Release Assets |
| 49 | + uses: python-semantic-release/[email protected] |
| 50 | + if: steps.release.outputs.released == 'true' |
| 51 | + with: |
| 52 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 53 | + tag: ${{ steps.release.outputs.tag }} |
| 54 | + working-directory: './backend' |
| 55 | + |
| 56 | + - name: Upload | Distribution Artifacts |
| 57 | + uses: actions/upload-artifact@v4 |
| 58 | + with: |
| 59 | + name: distribution-artifacts |
| 60 | + path: dist |
| 61 | + if-no-files-found: error |
| 62 | + working-directory: './backend' |
| 63 | + |
| 64 | + outputs: |
| 65 | + released: ${{ steps.release.outputs.released || 'false' }} |
| 66 | + |
| 67 | + deploy: |
| 68 | + # 1. Separate out the deploy step from the publish step to run each step at |
| 69 | + # the least amount of token privilege |
| 70 | + # 2. Also, deployments can fail, and its better to have a separate job if you need to retry |
| 71 | + # and it won't require reversing the release. |
| 72 | + runs-on: ubuntu-latest |
| 73 | + needs: release |
| 74 | + if: github.ref_name == 'main' && needs.release.outputs.released == 'true' |
| 75 | + |
| 76 | + permissions: |
| 77 | + contents: read |
| 78 | + id-token: write |
| 79 | + |
| 80 | + steps: |
| 81 | + - name: Setup | Download Build Artifacts |
| 82 | + uses: actions/download-artifact@v4 |
| 83 | + id: artifact-download |
| 84 | + with: |
| 85 | + name: distribution-artifacts |
| 86 | + path: dist |
| 87 | + |
| 88 | + - name: Publish to PyPi |
| 89 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 90 | + with: |
| 91 | + packages-dir: dist |
| 92 | + user: __token__ |
| 93 | + password: ${{ secrets.PYPI_UPLOAD_TOKEN }} |
0 commit comments