调用外部GitHub Actions工作流前,如何正确修改输入参数?
正确解决方案
核心问题是未在deploy job中显式声明outputs,导致其他job无法读取该job的步骤输出。以下是修正后的配置:
修改后的deployment.yml
on: workflow_call: inputs: runTests: description: 'Run tests' required: false default: false type: boolean testTags: description: 'Tags to run tests with' required: false type: string jobs: deploy: runs-on: company-runner # 1. 显式声明job的输出变量 outputs: testTags: ${{ steps.set-test-tags.outputs.testTags }} steps: - name: Set test tags id: set-test-tags # 2. 给步骤添加ID,用于引用其输出 if: ${{ inputs.runTests }} run: | if [[ <dev env condition> ]]; then echo "testTags=@dev and ${{ inputs.testTags }}" >> $GITHUB_OUTPUT elif [[ <qa env condition> ]]; then echo "testTags=@qa and ${{ inputs.testTags }}" >> $GITHUB_OUTPUT elif [[ <prod condition> ]]; then echo "testTags=@prod and ${{ inputs.testTags }}" >> $GITHUB_OUTPUT else echo "testTags=${{ inputs.testTags }}" >> $GITHUB_OUTPUT fi functional-test: needs: deploy uses: ourdomain/testrepo/path/to/tests.yml@main if: ${{ inputs.runTests }} with: # 3. 通过needs引用deploy job的输出 TAGS: ${{ needs.deploy.outputs.testTags }} secrets: inherit
关键说明
- 必须显式声明job outputs:只有在job中定义了
outputs字段,该job的输出才能被其他依赖它的job访问 - 步骤需要设置ID:通过步骤ID(
steps.set-test-tags)引用步骤的输出,再映射到job的outputs中 - 使用标准输出方式:
echo "key=value" >> $GITHUB_OUTPUT是GitHub Actions当前推荐的设置步骤输出的方式,旧的::set-output命令已被弃用 - 逻辑保持一致:确保环境判断条件(
<dev env condition>等)能正确匹配部署环境,生成符合预期的testTags值
内容的提问来源于stack exchange,提问作者Working Title
相关产品推荐
相关产品推荐

