将历史workflow_dispatch输入值复用至新GitHub工作流
问题与解决方案
问题描述
我维护一个接收workflow_dispatch输入的GitHub工作流,当原运行日志超过30天保留期后,需要重新运行该工作流并复用原运行的输入值。我希望通过读取历史运行生成的文件中的键值对,动态设置当前workflow_dispatch的输入参数。
用户提供历史运行ID(previousrunid)后,工作流读取C:\Temp\inputs.json中的内容,是否能将其中的键值对赋值给当前运行的${{ github.event.inputs.<key> }}?
附带原工作流代码:
name: Redirect Inputs to File on: workflow_dispatch: inputs: input1: description: 'Input 1' required: true default: 'Default Value 1' input2: description: 'Input 2' required: true default: 'Default Value 2' previousrunid: description: 'Enter previous runid' required: true default: '4356678' jobs: redirect-inputs: runs-on: windows-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Run PowerShell script to dump inputs to file STACK shell: pwsh run: | $Inputs = @" ${{ toJson(github.event.inputs) }} "@ | ConvertFrom-Json $InputData = $Inputs | ConvertTo-Json Write-Host $InputData $InputData | Out-File -FilePath "C:\Temp\inputs.json" Get-Content -Path "C:\Temp\inputs.json"
解决方案
核心结论
无法直接修改${{ github.event.inputs.<key> }},这个变量是工作流触发时就固定的只读上下文变量,在工作流执行过程中不能被动态赋值或修改。
可行实现思路
要复用历史输入值,需通过「保存历史输入为可持久化资源 → 读取历史输入 → 传递给后续步骤使用」的流程实现:
- 持久化历史输入:原工作流将输入保存到
C:\Temp的方式不可行(GitHub托管Runner运行结束后环境销毁,文件丢失),需将输入文件上传为GitHub Artifact(可设置更长保留期)或提交到仓库。 - 读取历史输入:通过提供的
previousrunid,下载对应历史运行的Artifact,读取其中的输入值。 - 传递并使用历史输入:将读取到的历史输入值设置为环境变量或作业输出,后续步骤直接使用这些值,而非依赖
github.event.inputs。
修改后的工作流代码
name: Save & Reuse Workflow Inputs on: workflow_dispatch: inputs: input1: description: 'Input 1' required: true default: 'Default Value 1' input2: description: 'Input 2' required: true default: 'Default Value 2' previousrunid: description: '输入历史运行ID以复用输入(新运行留空)' required: false default: '' jobs: save-or-reuse-inputs: runs-on: windows-latest steps: - name: 拉取代码仓库 uses: actions/checkout@v4 - name: 下载历史输入文件(若提供了previousrunid) if: ${{ github.event.inputs.previousrunid != '' }} uses: actions/download-artifact@v4 with: name: workflow-inputs run-id: ${{ github.event.inputs.previousrunid }} path: ./historical-inputs - name: 加载历史输入并设置为环境变量 if: ${{ github.event.inputs.previousrunid != '' }} shell: pwsh run: | $historicalInputs = Get-Content ./historical-inputs/inputs.json | ConvertFrom-Json # 将历史输入写入环境变量,后续步骤可通过$env:INPUT_*访问 echo "INPUT1=$($historicalInputs.input1)" >> $env:GITHUB_ENV echo "INPUT2=$($historicalInputs.input2)" >> $env:GITHUB_ENV - name: 使用输入(新输入或历史输入) shell: pwsh run: | # 优先使用历史输入的环境变量,无则使用当前触发的输入 $usedInput1 = if ($env:INPUT1) { $env:INPUT1 } else { "${{ github.event.inputs.input1 }}" } $usedInput2 = if ($env:INPUT2) { $env:INPUT2 } else { "${{ github.event.inputs.input2 }}" } Write-Host "当前使用的Input1: $usedInput1" Write-Host "当前使用的Input2: $usedInput2" # 此处替换为你的实际业务逻辑 - name: 保存当前输入到文件(仅新运行时执行) if: ${{ github.event.inputs.previousrunid == '' }} shell: pwsh run: | $Inputs = @" ${{ toJson(github.event.inputs) }} "@ | ConvertFrom-Json # 移除previousrunid字段,仅保留业务相关输入 $cleanInputs = $Inputs | Select-Object -Property input1, input2 $InputData = $cleanInputs | ConvertTo-Json New-Item -Path ./current-inputs -ItemType Directory -Force $InputData | Out-File -FilePath ./current-inputs/inputs.json Get-Content -Path ./current-inputs/inputs.json - name: 上传输入文件为Artifact(仅新运行时执行) if: ${{ github.event.inputs.previousrunid == '' }} uses: actions/upload-artifact@v4 with: name: workflow-inputs path: ./current-inputs/inputs.json retention-days: 90 # 设置比30天更长的保留期,避免输入文件丢失
关键说明
- Artifact持久化:通过
actions/upload-artifact将输入文件保存为Artifact,设置retention-days为90天(可按需调整),确保超过30天日志保留期后仍能获取输入。 - 环境变量传递:历史输入值通过
GITHUB_ENV写入环境变量,后续步骤可直接读取,实现复用。 - 分支逻辑:通过
if条件判断,区分新运行(保存输入)和复用运行(加载历史输入)的流程。
内容的提问来源于stack exchange,提问作者Ashar
相关产品推荐
相关产品推荐

