英文:
Get the head ref branch name in GitHub Actions when PR is merged to main branch
问题
我想在将格式为release/vX.Y.Z的PR合并到main分支时触发GitHub Actions工作流程。
我想从此主分支中获取版本号,即 "vX.Y.Z",以便我可以创建一个相应的发布并进行标记。
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: 提取版本号
run: |
echo "${{ github.event.pull_request.base_ref }}";
# 从发布分支格式中提取版本号:release/v1.2.3
# 例如:release/v1.2.3 | sed 's/.*\///' 将给我们 v1.2.3
echo "${{ GET_THE_RELEASE_BRANCH_NAME }}" | sed 's/.*\///' >> $RELEASE_VERSION
- name: 创建发布
id: create_release
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release create $RELEASE_VERSION --generate-notes --latest --verify-tag
我总是获得关于基本分支(即 main)的信息,但没有关于头分支的信息。
如何实现这一点?
英文:
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?
答案1
得分: 2
我想在主分支合并了 PR 的 release/vX.Y.Z 格式时触发 GitHub Actions 工作流程。
这里有一个类似的示例,使用了 release-vX.Y.Z
格式:
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:
[ ... ] # 使用 $TAG 做一些事情
此工作流将在关闭 PR 时触发。
根据 if
表达式,只有当 PR 的头分支使用 release-vX.Y.Z
格式,且 PR 合并到 main
分支时,tag
作业才会执行。使用这个实现,tag
变量和 output
将等于 vX.Y.Z
。
英文:
> 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论