Changes from gocardless/client-library-templates #482
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: build & publish | |
| on: | |
| push: | |
| branches: [master] | |
| pull_request: | |
| types: [opened, reopened, synchronize] | |
| permissions: | |
| contents: write # Required for creating releases and tags | |
| jobs: | |
| test: | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| python_version: ["3.10", "3.11", "3.12", "3.13", "3.14"] | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python_version }} | |
| cache-dependency-path: "**/requirements-dev.txt" | |
| - name: Test | |
| run: |- | |
| pip install -r requirements-dev.txt | |
| pytest | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: | |
| - test | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.13" | |
| - name: Install build dependencies | |
| run: python -m pip install --upgrade build | |
| - name: Build package | |
| run: python -m build | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: dist | |
| path: dist/ | |
| all-tests-passed: | |
| name: all-tests-passed | |
| runs-on: ubuntu-latest | |
| needs: [test, build] | |
| if: always() | |
| steps: | |
| - name: Check test/build job status | |
| run: | | |
| if [ "${{ needs.test.result }}" != "success" ] || [ "${{ needs.build.result }}" != "success" ]; then | |
| echo "test or build job did not succeed" | |
| exit 1 | |
| fi | |
| check_version: | |
| if: github.ref == 'refs/heads/master' | |
| runs-on: ubuntu-latest | |
| outputs: | |
| should_publish: ${{ steps.check.outputs.should_publish }} | |
| version: ${{ steps.check.outputs.version }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check if version tag exists | |
| id: check | |
| run: | | |
| VERSION=$(python -c "import re; print(re.search(r\"version\s*=\s*'([^']+)'\", open('setup.py').read()).group(1))") | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| if git rev-parse "v${VERSION}" >/dev/null 2>&1; then | |
| echo "Tag v${VERSION} already exists, skipping publish" | |
| echo "should_publish=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "Tag v${VERSION} does not exist, will publish" | |
| echo "should_publish=true" >> $GITHUB_OUTPUT | |
| fi | |
| publish_to_pypi: | |
| if: github.ref == 'refs/heads/master' && needs.check_version.outputs.should_publish == 'true' | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build | |
| - check_version | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.13" | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install setuptools wheel twine | |
| - uses: actions/download-artifact@v4 | |
| with: | |
| name: dist | |
| path: dist/ | |
| - name: Publish to PyPI | |
| env: | |
| TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} | |
| TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} | |
| run: twine upload dist/* | |
| create_release: | |
| if: github.ref == 'refs/heads/master' && needs.check_version.outputs.should_publish == 'true' | |
| runs-on: ubuntu-latest | |
| needs: | |
| - publish_to_pypi | |
| - check_version | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Extract changelog notes for this version | |
| id: notes | |
| if: needs.check_version.outputs.should_publish == 'true' | |
| run: | | |
| VERSION="${{ needs.check_version.outputs.version }}" | |
| NOTES="" | |
| if [ -f CHANGELOG.md ]; then | |
| NOTES=$(awk -v ver="$VERSION" ' | |
| $0 ~ "^## " ver {p=1; next} | |
| p && /^## / {exit} | |
| p {print} | |
| ' CHANGELOG.md) | |
| fi | |
| { | |
| echo "notes<<EOF" | |
| echo "$NOTES" | |
| echo "EOF" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Create tag and release | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| TAG="v${{ needs.check_version.outputs.version }}" | |
| git tag "$TAG" | |
| git push origin "$TAG" | |
| if [ -n "$(echo "$NOTES" | tr -d '[:space:]')" ]; then | |
| gh release create "$TAG" --title "$TAG" --notes "$NOTES" | |
| else | |
| gh release create "$TAG" --title "$TAG" --generate-notes | |
| fi | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| NOTES: ${{ steps.notes.outputs.notes }} |