|
| 1 | +name: Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + |
| 7 | +jobs: |
| 8 | + validate: |
| 9 | + uses: "./.github/workflows/validate.yml" |
| 10 | + |
| 11 | + # GitHub Actions does not have a halt job option to stop from deploying if no functional changes were found. |
| 12 | + # We thus execute a separate deployment job depending on the output of this job. |
| 13 | + check-for-functional-changes: |
| 14 | + runs-on: ubuntu-22.04 |
| 15 | + outputs: |
| 16 | + status: ${{ steps.stop-early.outputs.status }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 # Fetch all the tags |
| 21 | + - name: Set up Python |
| 22 | + uses: actions/setup-python@v5 |
| 23 | + with: |
| 24 | + python-version: 3.9.12 |
| 25 | + - id: stop-early |
| 26 | + run: | |
| 27 | + if "${GITHUB_WORKSPACE}/.github/has-functional-changes.sh" |
| 28 | + then |
| 29 | + echo "status=success" >> $GITHUB_OUTPUT |
| 30 | + fi |
| 31 | +
|
| 32 | + check-pypi-token: # Use intermediary job as secrets cannot be directly referenced in `if:` conditionals; see https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#using-secrets-in-a-workflow |
| 33 | + runs-on: ubuntu-22.04 |
| 34 | + outputs: |
| 35 | + pypi_token_present: ${{ steps.check_token.outputs.pypi_token_present }} |
| 36 | + steps: |
| 37 | + - name: Check PyPI token is defined |
| 38 | + id: check_token |
| 39 | + run: | |
| 40 | + if [[ -n "${{ secrets.PYPI_TOKEN }}" ]] |
| 41 | + then |
| 42 | + echo "pypi_token_present=true" >> $GITHUB_OUTPUT |
| 43 | + else |
| 44 | + echo "pypi_token_present=false" >> $GITHUB_OUTPUT |
| 45 | + fi |
| 46 | + |
| 47 | + deploy: |
| 48 | + runs-on: ubuntu-22.04 |
| 49 | + needs: [ validate, check-for-functional-changes, check-pypi-token ] |
| 50 | + if: needs.check-for-functional-changes.outputs.status == 'success' && needs.check-pypi-token.outputs.pypi_token_present == 'true' |
| 51 | + steps: |
| 52 | + - uses: actions/checkout@v4 |
| 53 | + - name: Set up Python |
| 54 | + uses: actions/setup-python@v5 |
| 55 | + with: |
| 56 | + python-version: 3.9.12 |
| 57 | + - name: Restore build |
| 58 | + uses: actions/cache@v4 |
| 59 | + with: |
| 60 | + path: ${{ env.pythonLocation }} |
| 61 | + key: build-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-${{ github.sha }} |
| 62 | + - name: Restore built package |
| 63 | + uses: actions/cache@v4 |
| 64 | + with: |
| 65 | + path: dist |
| 66 | + key: release-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-${{ github.sha }} |
| 67 | + - name: Upload a Python package to PyPi |
| 68 | + run: twine upload dist/* --username __token__ --password ${{ secrets.PYPI_TOKEN }} |
| 69 | + - name: Publish a git tag |
| 70 | + run: "${GITHUB_WORKSPACE}/.github/publish-git-tag.sh" |
0 commit comments