You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dev Repos推送时触发Test Repo工作流并获取产物?

解决方案

一、核心目标实现思路

要完成需求,需要分两步打通Dev Repos和Test Repo的工作流联动:触发+状态跟踪、产物跨仓库同步,全程使用GitHub原生API和Actions能力实现。

二、第一步:修正触发逻辑并实现状态跟踪

你的原触发命令存在语法和逻辑漏洞,导致Test Repo未被触发但Dev Repos无报错。以下是修正后的完整实现:

1. Dev Repos的触发工作流

该工作流会在推送时触发Test Repo的工作流,并持续轮询其状态,直到完成,确保Dev Repos能显示实时处理状态:

# Dev Repo/.github/workflows/trigger-test-workflow.yml
name: Trigger & Track Test Repo Workflow

on:
  push:
    branches: [main]

jobs:
  trigger-and-monitor:
    runs-on: ubuntu-latest
    steps:
      - name: Trigger Test Repo workflow
        id: trigger
        env:
          ORG_NAME: 你的组织名称 # 替换为实际组织名
          TEST_REPO_NAME: Test Repo # 替换为Test Repo的实际名称
          TEST_WORKFLOW_FILE: build-html.yml # Test Repo的工作流文件名
          GH_TOKEN: ${{ secrets.ORG_LEVEL_PAT }} # 组织级PAT,需提前配置为仓库机密
        run: |
          # 调用API触发Test Repo的workflow_dispatch,传递当前Dev Repo名称
          api_response=$(curl -s -w "%{http_code}" -H "Accept: application/vnd.github+json" \
            -H "Authorization: token $GH_TOKEN" \
            -X POST \
            -d '{
              "ref": "main",
              "inputs": {
                "triggering_repo": "'"${GITHUB_REPOSITORY#*/}"'"
              }
            }' \
            https://api.github.com/repos/${ORG_NAME}/${TEST_REPO_NAME}/actions/workflows/${TEST_WORKFLOW_FILE}/dispatches)

          # 检查HTTP响应码,确认触发是否成功
          http_code=$(echo "$api_response" | tail -n1)
          if [ "$http_code" -ne 204 ]; then
            echo "触发Test Repo工作流失败,响应:$(echo "$api_response" | head -n-1)"
            exit 1
          fi

          # 等待GitHub生成运行记录,然后获取最新的工作流运行ID
          sleep 5
          run_id=$(curl -s -H "Accept: application/vnd.github+json" \
            -H "Authorization: token $GH_TOKEN" \
            https://api.github.com/repos/${ORG_NAME}/${TEST_REPO_NAME}/actions/runs \
            | jq -r '.workflow_runs[] | select(.event == "workflow_dispatch" and .head_branch == "main") | .id' | head -n1)
          echo "run_id=$run_id" >> "$GITHUB_OUTPUT"

      - name: Track Test Repo workflow status
        env:
          ORG_NAME: 你的组织名称
          TEST_REPO_NAME: Test Repo
          RUN_ID: ${{ steps.trigger.outputs.run_id }}
          GH_TOKEN: ${{ secrets.ORG_LEVEL_PAT }}
        run: |
          while true; do
            # 获取工作流状态和结果
            run_data=$(curl -s -H "Accept: application/vnd.github+json" \
              -H "Authorization: token $GH_TOKEN" \
              https://api.github.com/repos/${ORG_NAME}/${TEST_REPO_NAME}/actions/runs/${RUN_ID})
            
            status=$(echo "$run_data" | jq -r '.status')
            conclusion=$(echo "$run_data" | jq -r '.conclusion')

            echo "Test Repo工作流当前状态:$status"

            if [ "$status" == "completed" ]; then
              if [ "$conclusion" == "success" ]; then
                echo "Test Repo工作流执行成功"
                exit 0
              else
                echo "Test Repo工作流执行失败,结果:$conclusion"
                exit 1
              fi
            fi

            sleep 30 # 每30秒轮询一次状态
          done

2. 权限配置要求

需要创建一个组织级个人访问令牌(PAT),权限至少包含:

  • repo:访问私有仓库
  • workflows:触发工作流和读取工作流状态
    将该PAT作为机密(Secret)添加到所有Dev Repos和Test Repo中,命名为ORG_LEVEL_PAT。

三、第二步:Test Repo工作流产物同步到触发的Dev Repo

修改Test Repo的工作流,在生成产物后,将其推送到触发它的Dev Repo中:

# Test Repo/.github/workflows/build-html.yml
name: Build & Sync HTML Artifact

on:
  push:
    branches: [main]
  workflow_dispatch:
    inputs:
      triggering_repo:
        description: "触发当前工作流的Dev Repo名称"
        required: true
        type: string

jobs:
  build-and-sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Test Repo code
        uses: actions/checkout@v4

      # 你的原有构建步骤,生成HTML产物(示例)
      - name: Build HTML files
        run: |
          mkdir -p dist
          echo "<h1>Generated from Test Repo</h1>" > dist/index.html
          # 替换为你的实际构建命令,比如npm run build

      - name: Sync artifact to triggering Dev Repo
        env:
          ORG_NAME: 你的组织名称
          DEV_REPO_NAME: ${{ github.event.inputs.triggering_repo }}
          GH_TOKEN: ${{ secrets.ORG_LEVEL_PAT }}
        run: |
          # 克隆目标Dev Repo
          git clone https://${GH_TOKEN}@github.com/${ORG_NAME}/${DEV_REPO_NAME}.git temp-repo
          cd temp-repo
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"

          # 清空旧产物目录(可选)
          rm -rf ./html-artifact
          mkdir -p ./html-artifact

          # 复制新产物到Dev Repo
          cp -r ../dist/* ./html-artifact/

          # 提交并推送
          git add ./html-artifact
          git commit -m "Update HTML artifact from Test Repo"
          git push origin main

四、原触发命令的问题说明

你之前的curl命令存在以下错误:

  1. 变量赋值有空格:response = $(...) 应改为 response=$(...)
  2. 未传递触发的Dev Repo名称参数,导致Test Repo无法确定同步目标
  3. 未检查API响应状态码,所以Dev Repos显示“成功”但实际触发失败

内容的提问来源于stack exchange,提问作者Sahil Khanna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 13:11:37