Draft GitHub Release #168
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: Draft GitHub Release | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version_level: | |
| description: Semantic version level increase | |
| required: true | |
| type: choice | |
| default: patch | |
| options: | |
| - patch | |
| - minor | |
| - major | |
| permissions: | |
| contents: write | |
| packages: read | |
| statuses: read | |
| checks: read | |
| pull-requests: write | |
| actions: read | |
| repository-projects: read | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GH_PAGER: cat | |
| jobs: | |
| checks: | |
| name: Check requirements for PR to be merged | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Fail if main branch was selected | |
| if: ${{ github.ref_name == 'main' }} | |
| run: | | |
| echo "Cannot release from main branch, please select valid release branch." | |
| exit 1 | |
| - name: Check GitHub token validity | |
| run: | | |
| echo "Validating GitHub Token" | |
| status_code=$(curl -o /dev/null -s -w "%{http_code}" -H "Authorization: token ${{ secrets.GH_PAT }}" https://api.github.com/user) | |
| if [ "$status_code" -ne 200 ]; then | |
| echo "Error: GitHub token is invalid or expired. Please update your token in secrets." | |
| echo "Instructions can be found at: https://github.com/EIT-ALIVE/eitprocessing/blob/main/README.dev.md#updating-the-token" | |
| exit 1 | |
| else | |
| echo "GitHub token is valid." | |
| fi | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Check if PR exists | |
| run: gh pr view ${{ github.ref_name }} | |
| - name: Check if PR base is main | |
| run: gh pr view ${{ github.ref_name }} --json baseRefName -q 'if .baseRefName == "main" then "PR base is main" else error("PR base is not main") end' | |
| - name: Check if PR is mergeable | |
| run: gh pr view ${{ github.ref_name }} --json mergeable -q 'if .mergeable == "MERGEABLE" then "PR is mergeable" else error("PR is not mergeable") end' | |
| - name: Check whether PR checks pass(ed) | |
| # note that this assumed there are checks to be passed; this fails if there are no checks, which is no | |
| # relevant to this repo | |
| run: | | |
| gh pr checks ${{ github.ref_name }} --watch --fail-fast | |
| gh pr checks ${{ github.ref_name }} --json state -q 'if . | length == 0 then "No checks found." elif map(.state == "SUCCESS") | all then "All checks passed" else error("Not all checks passed") end' | |
| test_python_releases: | |
| needs: checks | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| os: ["ubuntu-latest"] | |
| python-version: ["3.10", "3.11", "3.12", "3.13"] | |
| name: Build for ${{ matrix.python-version }}, ${{ matrix.os }} | |
| env: | |
| EIT_PROCESSING_TEST_DATA: ${{ github.workspace }}/../eitprocessing_data/ | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Install eitprocessing | |
| uses: ./.github/actions/install_eitprocessing | |
| with: | |
| dependencies: testing | |
| extract-data: true | |
| python-version: ${{ matrix.python-version }} | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| data-directory: ${{ env.EIT_PROCESSING_TEST_DATA }} | |
| - name: Run pytest | |
| run: pytest -v --runslow | |
| - name: Install additional dependencies for building | |
| run: python3 -m pip install --upgrade ".[publishing]" | |
| - name: Build eitprocessing | |
| run: python3 -m build | |
| merge_and_bump: | |
| name: Test and apply merging and bumping | |
| needs: test_python_releases | |
| runs-on: ubuntu-latest | |
| outputs: | |
| new-version: ${{ steps.bump.outputs.current-version }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| fetch-depth: 0 | |
| token: ${{ secrets.GH_PAT }} | |
| - name: Configure git | |
| run: | | |
| git config user.email "${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com" | |
| git config user.name "GitHub Actions" | |
| git config -l | |
| - name: Install Python | |
| uses: actions/setup-python@v5.1.1 | |
| with: | |
| python-version: "3.12" | |
| - name: Install bump-my-version | |
| shell: bash | |
| run: | | |
| python3 -m pip install pyproject-deplister | |
| pyproject-deplister --extra dev --path pyproject.toml | grep bump-my-version | sed 's/ //g' | xargs -I{} python3 -m pip install "{}" | |
| - name: Test merging PR into main | |
| run: | | |
| git fetch origin main | |
| git checkout -B temp_main_${{github.ref_name}}_${{github.run_id}} origin/main | |
| git branch -f ${{ github.ref_name }} origin/${{ github.ref_name }} | |
| git merge ${{ github.ref_name }} --no-ff --no-edit | |
| - name: Test bumping version | |
| id: temp_bump | |
| shell: bash | |
| run: | | |
| bump-my-version bump ${{ inputs.version_level }} --commit --no-tag | |
| ([[ $? -gt 0 ]] && echo "bumped=false" || echo "bumped=true") >> $GITHUB_OUTPUT | |
| - name: Fail if bumping failes | |
| if: steps.temp_bump.outputs.bumped == 'false' | |
| run: | | |
| echo "Bumping failed on temp branch." | |
| git reset --hard HEAD^ | |
| exit 1 | |
| - name: Succeed if version is bumped | |
| if: steps.temp_bump.outputs.bumped == 'true' | |
| run: | | |
| echo "Version would be bumped to $(bump-my-version show current_version)!" | |
| - name: Test merging into develop | |
| run: | | |
| git fetch origin develop | |
| git checkout -B temp_develop_${{github.ref_name}}_${{github.run_id}} origin/develop | |
| git merge temp_main_${{github.ref_name}}_${{github.run_id}} | |
| - name: Get current main commit hash | |
| id: premerge | |
| run: | | |
| git fetch origin main | |
| echo "hash=$(git rev-parse origin/main)" >> $GITHUB_OUTPUT | |
| - name: Merge PR | |
| run: | | |
| gh pr merge ${{ github.ref_name }} --merge --delete-branch --squash=false --admin | |
| - name: Wait for main to update | |
| id: waitmain | |
| run: | | |
| for i in {1..10}; do | |
| git fetch origin main | |
| NEW_HASH=$(git rev-parse origin/main) | |
| if [ "$NEW_HASH" != "${{ steps.premerge.outputs.hash }}" ]; then | |
| echo "Main branch updated." | |
| exit 0 | |
| fi | |
| echo "Waiting for main branch to update..." | |
| sleep 6 | |
| done | |
| echo "Timeout: main branch did not update in time." | |
| exit 1 | |
| - name: Checkout main | |
| run: | | |
| git switch main | |
| git pull origin main | |
| - name: Bump version | |
| id: bump | |
| shell: bash | |
| run: | | |
| echo "previous-version=$(bump-my-version show current_version)" >> $GITHUB_OUTPUT | |
| bump-my-version bump ${{ inputs.version_level }} --commit --tag | |
| ([[ $? -gt 0 ]] && echo "bumped=false" || echo "bumped=true") >> $GITHUB_OUTPUT | |
| echo "current-version=$(bump-my-version show current_version)" >> $GITHUB_OUTPUT | |
| - name: Fail if bumping failes | |
| if: steps.bump.outputs.bumped == 'false' | |
| run: | | |
| echo "Bumping failed." | |
| git reset --hard HEAD^ | |
| exit 1 | |
| - name: Check new version number | |
| if: steps.bump.outputs.bumped == 'true' | |
| run: | | |
| echo "Version was bumped from ${{ steps.bump.outputs.previous-version }} to ${{ steps.bump.outputs.current-version }}!" | |
| - name: Merge main into develop | |
| run: | | |
| git checkout develop | |
| git merge main --no-ff --no-edit || { echo "Can't merge changes to develop. Manually merge PR and create GitHub release."; exit 1; } | |
| - name: Push changes to develop | |
| uses: ad-m/github-push-action@master | |
| with: | |
| github_token: ${{ secrets.GH_PAT }} | |
| branch: develop | |
| force_with_lease: true | |
| - name: Checkout main | |
| run: | | |
| git checkout main | |
| - name: Push changes to main | |
| uses: ad-m/github-push-action@master | |
| with: | |
| github_token: ${{ secrets.GH_PAT }} | |
| branch: main | |
| force_with_lease: true | |
| github_release: | |
| name: Create a draft GitHub release | |
| needs: merge_and_bump | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| - name: Create GitHub Release | |
| id: create_release | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| gh release create v${{ needs.merge_and_bump.outputs.new-version }} \ | |
| --title="Release v${{ needs.merge_and_bump.outputs.new-version }}" \ | |
| --generate-notes \ | |
| --draft |