如何在GitHub Actions中复用前序Workflow输出值且不重跑该Workflow?
GitHub Actions跨Workflow复用固定计算值的解决方案
完全支持这种场景,下面是几种可行的方案,可避开可复用工作流重复执行的问题:
方案1:通过GitHub Releases存储计算值x
Workflow1计算出x后,将其写入文件并上传为Release的资产,Workflow2触发时直接下载该资产读取x值,无需重新计算。
Workflow1示例(.github/workflows/workflow1.yaml):
name: Generate and Store X on: [push] jobs: generate-x: runs-on: ubuntu-latest steps: - name: Calculate x id: calc-x run: | # 替换为你的实际x计算逻辑 x=$((RANDOM % 100)) echo "x_value=$x" >> "$GITHUB_OUTPUT" - name: Save x to file run: echo "${{ steps.calc-x.outputs.x_value }}" > x_value.txt - name: Create Release id: create_release uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: tag_name: x-value-${{ steps.calc-x.outputs.x_value }} release_name: X Value ${{ steps.calc-x.outputs.x_value }} draft: false prerelease: false - name: Upload X Value Asset uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} asset_path: ./x_value.txt asset_name: x_value.txt asset_content_type: text/plain
Workflow2示例(.github/workflows/workflow2.yaml):
name: Use Stored X on: [workflow_dispatch] # 按需选择触发方式,比如定时、其他事件 jobs: use-x: runs-on: ubuntu-latest steps: - name: Download x from latest Release uses: dsaltares/fetch-gh-release-asset@master with: repo: ${{ github.repository }} file: x_value.txt target: ./x_value.txt env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Calculate total run: | x=$(cat x_value.txt) a=50 total=$((a + x)) echo "Total is $total"
方案2:通过专属Git分支存储x值
Workflow1计算x后,将x写入仓库内的文件,提交到专属分支,Workflow2拉取该分支读取x值。
Workflow1关键步骤:
- name: Save x to file run: echo "${{ steps.calc-x.outputs.x_value }}" > x_store.txt - name: Commit and push to value-store branch run: | git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" git checkout -b value-store || git checkout value-store git add x_store.txt git commit -m "Update x value to ${{ steps.calc-x.outputs.x_value }}" git push origin value-store --force
Workflow2关键步骤:
- name: Checkout value-store branch uses: actions/checkout@v4 with: ref: value-store - name: Calculate total run: | x=$(cat x_store.txt) a=50 total=$((a + x)) echo "Total is $total"
方案3:通过GitHub Environment Secrets临时存储x
如果x值不需要长期保留,可将其存入环境密钥,Workflow2在对应环境下读取。
Workflow1关键步骤:
- name: Get Environment Public Key id: get_public_key run: | response=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ https://api.github.com/repos/${{ github.repository }}/environments/production/secrets/public-key) echo "key_id=$(echo $response | jq -r .key_id)" >> "$GITHUB_OUTPUT" echo "key=$(echo $response | jq -r .key)" >> "$GITHUB_OUTPUT" - name: Encrypt and Update Secret run: | x=${{ steps.calc-x.outputs.x_value }} # 用环境公钥加密x值 encrypted_x=$(echo -n "$x" | openssl rsautl -encrypt -pubin -inkey <(echo "${{ steps.get_public_key.outputs.key }}") | base64 -w0) curl -X PUT \ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/${{ github.repository }}/environments/production/secrets/X_VALUE \ -d '{"encrypted_value":"'"$encrypted_x"'","key_id":"'"${{ steps.get_public_key.outputs.key_id }}"'"}'
Workflow2读取示例:
jobs: use-x: runs-on: ubuntu-latest environment: production # 指定对应环境 steps: - name: Calculate total run: | x=${{ secrets.X_VALUE }} a=50 total=$((a + x)) echo "Total is $total"
为什么可复用工作流不适用?
可复用工作流是将被调用的工作流嵌入当前Workflow的执行流程,每次调用都会重新执行所有步骤,因此会重新计算x值,无法复用之前生成的结果,不符合你的需求。
内容的提问来源于stack exchange,提问作者protossforaiur
相关产品推荐
相关产品推荐

