I want to trigger a GitHub Actions workflow when a release/vX.Y.Z format of PR gets merged into the main branch.
I want the version number from this head branch i.e. "vX.Y.Z" so that I can create a release and tag it accordingly.
name: Auto PR Action
on:
pull_request:
branches:
- main
types: [closed]
jobs:
build:
if: ${{ github.event.pull_request.merged }}
runs-on: ubuntu-latest
steps:
- name: Extract version number
run: |
echo "${{ github.event.pull_request.base_ref }}"
# Extract version number from the Release branch format: release/v1.2.3
# E.g: release/v1.2.3 | sed 's/.*\///' will give us v1.2.3
echo "${{ GET_THE_RELEASE_BRANCH_NAME }}" | sed 's/.*\///' >> $RELEASE_VERSION
- name: Create Release
id: create_release
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release create $RELEASE_VERSION --generate-notes --latest --verify-tag
I always get the information about the base branch, i.e. main, but no info about the head branch.
How can we achieve this?
I want to trigger a GitHub Actions workflow when a release/vX.Y.Z format of PR gets merged into the main branch.
Here is something similar using the release-vX.Y.Z
format:
on:
pull_request:
types:
- closed
jobs:
tag:
if: contains(github.head_ref, 'release-') && contains(github.base_ref, 'main') && github.event.action == 'closed' && github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.tag.outputs.tag }}
steps:
- name: Extract branch tag
id: tag
run: |
BRANCH=${{ github.head_ref }}
TAG="${BRANCH//release-/""}"
echo $TAG
echo "tag=$TAG" >> $GITHUB_OUTPUT
release:
needs:
- TAG
runs-on: ubuntu-latest
env:
TAG: ${{ needs.TAG.outputs.tag }}
steps:
[ ... ] # use $TAG to do something
This workflow will trigger when a PR is closed.
However, according to the if
expression, the tag
job will only be executed when the PR head branch use the release-vX.Y.Z
format, and if the PR gets merged into the main
branch.
Using this implementation, the tag
variable and output
will be equal to vX.Y.Z
.