如何在GitHub Actions的uses中参数化传递inputs.custom参数?
GitHub Actions参数化调用子工作流的问题及限制
需求场景
用户尝试通过inputs.custom参数动态指定uses字段引用的子工作流文件,期望实现的代码逻辑如下:
jobs: test-custom: name: Test Custom uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
完整示例文件
1. 触发工作流 work1-build.yml
name: Start workflow fail on: [push] jobs: build-fail: name: Build with other workflow uses: ./.github/workflows/work3-build-fail.yml with: custom: custom-name1
2. 中转工作流 work3-build-fail.yml
name: Build fail with input test on: workflow_call: inputs: custom: description: Some custom string required: true type: string jobs: test-custom: name: Test Custom uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
3. 目标子工作流 work4-custom-name1.yml
name: Custom 1 on: workflow_call jobs: Explore-GitHub-Actions: runs-on: ubuntu-latest steps: - run: echo "IN CUSTOM 1"
运行报错信息
执行上述工作流后,GitHub返回如下错误:
Invalid workflow file error parsing called workflow ".github/workflows/work1-build.yml" -> "./.github/workflows/work3-build-fail.yml" (source branch with sha:720087c8794e76f52277f9b1229b44ea65ab89d5) --> "./.github/workflows/work4-${{ inputs.custom }}.yml" : failed to fetch workflow: workflow was not found.
已知可行的参数使用方式
用户已验证inputs.custom可在步骤的run命令中正常使用,示例代码如下:
test-print: runs-on: ubuntu-latest name: Print input steps: - name: Step print input run: echo ${{ inputs.custom }}
核心限制说明
GitHub官方文档中无uses参数化的相关示例,本质原因是:GitHub Actions在工作流解析阶段会提前校验uses引用的文件路径,不会等到运行时再解析变量。也就是说,uses字段的路径必须是固定的静态值,无法通过动态变量拼接生成。
内容的提问来源于stack exchange,提问作者pbaranski
相关产品推荐
相关产品推荐

