如何在GitHub中配置并读取多个依赖工作流的输出值?
GitHub工作流间传递生成编号的解决方案
针对你的工作流依赖场景,这里提供两种可靠的实现方式,分别适用于临时传递和持久化存储的需求:
方法一:使用GitHub Artifacts临时传递(推荐)
这种方式不会修改仓库内容,适合工作流间临时传递数据的场景。
修改workflow1.yml(生成并上传编号)
在生成编号后,将其写入文件并上传为GitHub Artifact,供后续工作流下载读取:
name: First workflow on: workflow_dispatch: jobs: some_job: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Generate some number run: | # 将生成的编号写入文件 echo "${{ github.run_number }}" > generated_number.txt - name: Upload generated number as artifact uses: actions/upload-artifact@v4 with: name: generated-number path: generated_number.txt
修改workflow2.yml(读取First workflow的编号)
通过actions/download-artifact下载前一个工作流的Artifact,读取其中的编号:
name: Second workflow on: workflow_run: workflows: ["First workflow"] types: - completed jobs: deploy: runs-on: ubuntu-latest steps: - name: Download generated number artifact uses: actions/download-artifact@v4 with: name: generated-number github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} - name: Read and use generated number id: get_num run: | generated_num=$(cat generated_number.txt) # 将编号输出为步骤变量,供后续步骤使用 echo "generated_num=$generated_num" >> $GITHUB_OUTPUT echo "成功读取编号: $generated_num" # 示例:在后续步骤中使用编号 - name: Use the number run: echo "当前使用的编号是 ${{ steps.get_num.outputs.generated_num }}" # 可选:重新上传Artifact,供Third workflow读取 - name: Re-upload artifact for Third workflow uses: actions/upload-artifact@v4 with: name: generated-number path: generated_number.txt
修改workflow3.yml(读取传递过来的编号)
和workflow2的逻辑一致,下载Second workflow上传的Artifact即可:
name: Third workflow on: workflow_run: workflows: ["Second workflow"] types: - completed jobs: deploy: runs-on: ubuntu-latest steps: - name: Download generated number artifact uses: actions/download-artifact@v4 with: name: generated-number github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} - name: Read and use generated number run: | generated_num=$(cat generated_number.txt) echo "成功读取编号: $generated_num"
方法二:写入GitHub仓库文件(持久化存储)
如果需要将编号持久化保存到仓库中,可以直接写入文件并提交推送,后续工作流通过拉取代码读取:
修改workflow1.yml(生成并保存到仓库)
name: First workflow on: workflow_dispatch: jobs: some_job: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Generate and save number to repo run: | # 创建存储目录(如果不存在) mkdir -p .github/workflow-data # 将编号写入文件 echo "${{ github.run_number }}" > .github/workflow-data/generated_number.txt # 配置Git用户信息 git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" # 提交并推送更改 git add .github/workflow-data/generated_number.txt git commit -m "Save generated workflow number: ${{ github.run_number }}" git push
修改workflow2.yml和workflow3.yml(读取仓库中的编号)
只需拉取最新代码后读取文件内容即可:
name: Second workflow # Third workflow同理修改name和触发条件 on: workflow_run: workflows: ["First workflow"] # Third workflow改为["Second workflow"] types: - completed jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout latest code uses: actions/checkout@v4 - name: Read generated number run: | generated_num=$(cat .github/workflow-data/generated_number.txt) echo "成功读取编号: $generated_num"
内容的提问来源于stack exchange,提问作者shaik moeed
相关产品推荐
相关产品推荐

