Create GitHub Release #71
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: Create GitHub Release | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| branch: | |
| description: 'Branch to release from' | |
| required: true | |
| default: 'beta' | |
| workflow_run: | |
| workflows: ['Test PR'] | |
| types: [completed] | |
| branches: ['main', 'beta', 'httpx'] | |
| # The release is created with a GitHub App token. This workflow no longer | |
| # commits anything back to the branch (the changelog is rendered in the | |
| # regeneration PR), so the default token only needs read access for checkout. | |
| permissions: | |
| contents: read | |
| jobs: | |
| create-release: | |
| if: github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push') | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ inputs.branch || github.event.workflow_run.head_branch }} | |
| - name: Generate app token | |
| id: app-token | |
| uses: actions/create-github-app-token@v3 | |
| with: | |
| app-id: ${{ secrets.RELEASE_APP_ID }} | |
| private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} | |
| - name: Read version | |
| id: version | |
| run: | | |
| VERSION=$(grep '^version' pyproject.toml | sed 's/version = "\(.*\)"/\1/') | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| if [[ "$VERSION" == *"b"* || "$VERSION" == *"rc"* || "$VERSION" == *"a"* ]]; then | |
| echo "prerelease=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "prerelease=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Check if release already exists | |
| id: check-release | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| VERSION="${{ steps.version.outputs.version }}" | |
| if gh release view "$VERSION" > /dev/null 2>&1; then | |
| echo "Release $VERSION already exists, skipping." | |
| echo "exists=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "exists=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Validate version matches branch | |
| if: steps.check-release.outputs.exists == 'false' | |
| run: | | |
| BRANCH="${{ inputs.branch || github.event.workflow_run.head_branch }}" | |
| VERSION="${{ steps.version.outputs.version }}" | |
| PRERELEASE="${{ steps.version.outputs.prerelease }}" | |
| if [[ "$BRANCH" == "beta" && "$PRERELEASE" == "false" ]]; then | |
| echo "::error::Beta branch must have a prerelease version (e.g. 3.1.0b0), got '$VERSION'" | |
| exit 1 | |
| fi | |
| if [[ "$BRANCH" == "httpx" && "$PRERELEASE" == "false" ]]; then | |
| echo "::error::httpx branch must have a prerelease version (e.g. 4.0.0b1), got '$VERSION'" | |
| exit 1 | |
| fi | |
| if [[ "$BRANCH" == "main" && "$PRERELEASE" == "true" ]]; then | |
| echo "::error::Main branch must not have a prerelease version, got '$VERSION'" | |
| exit 1 | |
| fi | |
| # CHANGELOG.md is rendered from changelog.d/ fragments inside the | |
| # regeneration PR (see regenerate-library.yml), so it is already on the | |
| # branch by the time the release is cut. Committing/pushing here would | |
| # bypass the branch's required status checks and be rejected by the | |
| # ruleset, so no git write happens in this workflow. --generate-notes | |
| # below produces the GitHub release body. | |
| - name: Create release | |
| if: steps.check-release.outputs.exists == 'false' | |
| env: | |
| GH_TOKEN: ${{ steps.app-token.outputs.token }} | |
| run: | | |
| FLAGS="" | |
| if [ "${{ steps.version.outputs.prerelease }}" = "true" ]; then | |
| FLAGS="--prerelease" | |
| fi | |
| gh release create "${{ steps.version.outputs.version }}" \ | |
| --target "${{ inputs.branch || github.event.workflow_run.head_branch }}" \ | |
| --title "${{ steps.version.outputs.version }}" \ | |
| --generate-notes \ | |
| $FLAGS |