从Jenkins迁移至GitHub Actions:调用工作流传参为空问题排查
解决GitHub Actions工作流调用时参数传递为空的问题
问题概述
从Jenkins迁移到GitHub Actions过程中,两种批量调用子工作流的场景均出现参数传递为空的情况:
- CSV批量调用:
Caller.yml读取CSV后调用called.yml,子工作流接收到的参数为空 - JSON矩阵调用:
Main.yml读取cache.json通过矩阵策略调用ASGW-Config-Cache.yml,同样存在参数空值问题
场景1:CSV调用修复(Caller.yml + called.yml)
核心问题
called.yml中错误使用${{ github.event.inputs.xxx }}获取参数,通过workflow_call触发的工作流,参数需直接从inputs对象读取,github.event.inputs仅适用于workflow_dispatch手动触发的场景。
修复后的called.yml关键代码
jobs: Create_Update-Cache: runs-on: [abc] steps: - name: Debug Inputs run: | echo "cacheName: ${{ inputs.cacheName }}" echo "expirationType: ${{ inputs.expirationType }}" echo "expirationTime: ${{ inputs.expirationTime }}" - name: Execute cURL run: | createresponse=`curl -k --write-out '%{http_code}' --header "authorization: Basic ${{ secrets.USER }}" --header "Content-Type: application/json" --request POST --url 'https://example.com/config' --data '{"distributed": true,"Settings": {"${{ inputs.expirationType }}": {"value": "${{ inputs.expirationTime }}"},"valuesNull": false},"name": "${{ inputs.cacheName }}","overflowToDisk": false,"skipCacheIfElementSizeInKBExceeds": 123}'` echo "$createresponse" # 后续判断逻辑保持不变,将所有`github.event.inputs`替换为`inputs`
Caller.yml循环调用补充
Read CSV and trigger workflows步骤需补充循环调用逻辑,确保每行CSV参数正确传递:
while IFS=, read -r cacheName expirationType expirationTime; do gh workflow run called.yml \ -f cacheName="$cacheName" \ -f expirationType="$expirationType" \ -f expirationTime="$expirationTime" done < test.csv
场景2:JSON矩阵调用修复(Main.yml + ASGW-Config-Cache.yml)
问题点及修复
JSON输出格式标准化
确保set-iterations步骤输出的JSON无换行、转义问题,使用jq工具格式化:json_array=$(jq -c . cache.json) echo "values=$json_array" >> $GITHUB_OUTPUT参数类型匹配
cache.json中expirationType值为"int",与ASGW-Config-Cache.yml的workflow_dispatch选项(timeoutInSec/timeOfDay/date)不匹配,需修正JSON中的值为子工作流支持的类型。curl命令引号优化
避免嵌套引号导致的变量替换失败,改用Here-Document构造JSON:createresponse=$(curl -k --write-out '%{http_code}' \ --header "authorization: Basic ${{ secrets.USER }}" \ --header "Content-Type: application/json" \ --request POST \ --url 'https://example.com/caches' \ --data @- <<EOF {"distributed": true,"expirySettings": {"${{ inputs.expirationType }}": {"value": "${{ inputs.expirationTime }}"},"valuesNull": false},"name": "${{ inputs.cacheName }}","overflowToDisk": false,"skipCacheIfElementSizeInKBExceeds": 1000} EOF )
通用验证步骤
- 在子工作流中添加调试步骤,确认参数接收情况
- 在调用方工作流中打印传递的参数值,验证传递逻辑正确性
内容的提问来源于stack exchange,提问作者Reddy Rohit
相关产品推荐
相关产品推荐

