如何通过另一个GitHub Action触发Git标签触发的工作流?
如何通过GitHub Action自动触发另一个Action?
问题描述
手动创建并推送v开头的Git标签时,tag-trigger工作流可以正常运行;但通过version-tagging Action自动创建并推送标签时,tag-trigger工作流从未执行。相关配置如下:
tag-trigger.yml
name: Tag Triggered Workflow on: push: tags: - "v*" # Runs only when a tag starting with "v" is created jobs: say-hello: runs-on: ubuntu-latest steps: - name: Print Hello World run: | echo "Hello, World! A new tag was created: ${{ github.ref }}"
version-tagging.yml
name: Auto Version Tagging # this action creates a new tag when you change the version in Cargo.toml on: push: branches: - main # Adjust if needed permissions: contents: write # Required to push new tags jobs: tag-version: runs-on: ubuntu-latest outputs: new_version: ${{ steps.get_version.outputs.value }} # Pass version to next job tag_created: ${{ steps.create_tag.outputs.tag_created }} # Pass if tag was created steps: - name: Checkout Repository uses: actions/checkout@v4 with: fetch-depth: 0 # Fetch all history to compare versions - name: Extract Version from Cargo.toml id: get_version uses: sravinet/toml-select@v1.0.1 with: file: "Cargo.toml" field: "package.version" - name: Check if Tag Exists id: check_tag run: | VERSION=v${{ steps.get_version.outputs.value }} if git rev-parse "$VERSION" >/dev/null 2>&1; then echo "TAG_EXISTS=true" >> $GITHUB_ENV else echo "TAG_EXISTS=false" >> $GITHUB_ENV fi - name: Create and Push Git Tag id: create_tag if: env.TAG_EXISTS == 'false' run: | VERSION=v${{ steps.get_version.outputs.value }} git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" git commit --allow-empty -m "trigger workflow $VERSION" git tag "$VERSION" git push origin main --tags echo "tag_created=true" >> $GITHUB_ENV - name: Print Confirmation run: | if [ "${{ env.TAG_EXISTS }}" == "true" ]; then echo "✅ Tag already exists: v${{ steps.get_version.outputs.value }}" else echo "🚀 New tag created: v${{ steps.get_version.outputs.value }}" fi
解决方案
核心问题是:GitHub Actions默认使用的GITHUB_TOKEN推送内容时,不会触发新的工作流(防止循环触发)。要解决这个问题,需要使用**个人访问令牌(PAT)**替代默认令牌来推送标签。
步骤1:生成并添加PAT到仓库
- 生成一个具有
repo权限的PAT(在GitHub账号的Settings > Developer settings > Personal access tokens中创建)。 - 进入目标仓库的
Settings > Secrets and variables > Actions,添加一个新的仓库密钥,命名为PAT_TOKEN,值为刚才生成的PAT。
步骤2:修改version-tagging.yml配置
调整代码拉取步骤
将Checkout Repository步骤改为使用PAT拉取代码:
- name: Checkout Repository uses: actions/checkout@v4 with: fetch-depth: 0 token: ${{ secrets.PAT_TOKEN }} # 使用PAT替代默认GITHUB_TOKEN
调整标签推送命令
修改Create and Push Git Tag步骤中的推送命令,使用PAT进行认证:
- name: Create and Push Git Tag id: create_tag if: env.TAG_EXISTS == 'false' run: | VERSION=v${{ steps.get_version.outputs.value }} git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" # 可选:如果不需要空提交可以删除这行 git commit --allow-empty -m "trigger workflow $VERSION" git tag "$VERSION" # 使用PAT认证推送标签和分支 git push https://${{ secrets.PAT_TOKEN }}@github.com/snowfoxsh/actions-test.git main --tags echo "tag_created=true" >> $GITHUB_ENV
原理说明
默认的GITHUB_TOKEN存在触发限制:由该令牌触发的工作流,后续推送操作不会再触发新的工作流。而PAT作为个人身份令牌,不受此限制,因此用它推送标签后,tag-trigger工作流会正常响应push.tags事件。
其他可选方案
- 直接调用工作流:如果不需要单独触发
tag-trigger,可以在version-tagging工作流中使用workflow_call事件直接调用tag-trigger的逻辑,避免依赖标签推送触发。 - 允许循环触发(不推荐):在仓库设置中开启
Allow GitHub Actions to create and approve pull requests,但这可能导致无限循环,仅在明确无循环风险时使用。
内容的提问来源于stack exchange,提问作者Patrick Unick
相关产品推荐
相关产品推荐

