You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions多输出仅返回一个值问题求助

解决GitHub Action跨Job输出仅能获取单个值的问题

你的问题根源在于第一个Job的输出定义全部错误引用了同一个步骤的输出。看get_changed_files的outputs部分,你把所有输出都指向了steps.changed_files.outputs,但实际上changed_python_files、changed_yaml_files这些变量是在各自独立的步骤(对应id为changed_python_files、changed_yaml_files的步骤)里设置的,并非来自changed_files步骤。

修改后的正确代码:

env:
  GH_TOKEN: ${{ github.token }}

jobs:
  get_changed_files:
    name: Get changed files
    runs-on: ubuntu-latest
    outputs:
      changed_files: ${{ steps.changed_files.outputs.changed_files }}
      # 修正每个输出对应的步骤id
      changed_python_files: ${{ steps.changed_python_files.outputs.changed_python_files }}
      changed_yaml_files: ${{ steps.changed_yaml_files.outputs.changed_yaml_files }}
      changed_json_files: ${{ steps.changed_json_files.outputs.changed_json_files }}
      test_value: ${{ steps.test_value.outputs.test_value }}
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref || github.ref }}
      - name: Get changed files
        id: changed_files
        shell: bash
        run: |
          changed_files=`gh pr diff ${{ github.event.number }} --name-only | xargs`
          echo "Changed files: $changed_files"
          echo "changed_files=$changed_files" >> $GITHUB_OUTPUT
      - name: Get changed python files
        id: changed_python_files
        shell: bash
        run: |
          cpf=`python3 ${{ github.workspace }}/deploy/_filter_files.py py ${{ steps.changed_files.outputs.changed_files }}`
          echo "Changed python files: $cpf"
          echo "changed_python_files=$cpf" >> $GITHUB_OUTPUT
      - name: Get changed yaml files
        id: changed_yaml_files
        shell: bash
        run: |
          cyf=`python3 ${{ github.workspace }}/deploy/_filter_files.py yml ${{ steps.changed_files.outputs.changed_files }}`
          echo "Changed yaml files: $cyf"
          echo "changed_yaml_files=$cyf" >> $GITHUB_OUTPUT
      - name: Get changed json files
        id: changed_json_files
        shell: bash
        run: |
          cjf=`python3 ${{ github.workspace }}/deploy/_filter_files.py json ${{ steps.changed_files.outputs.changed_files }}`
          echo "Changed json files: $cjf"
          echo "changed_json_files=$cjf" >> $GITHUB_OUTPUT
      - name: Get test value
        id: test_value
        shell: bash
        run: |
          echo "test_value=foo" >> $GITHUB_OUTPUT

  echo_changed_files:
    needs: get_changed_files
    name: Echo changed files
    runs-on: ubuntu-latest
    steps:
      - name: Echo changed files
        env:
          OUTPUTS: ${{ toJson(needs.get_changed_files.outputs) }}
        run: |
          echo "All outputs: $OUTPUTS"

关键说明:

每个步骤的id是唯一标识,步骤内通过echo "var=value" >> $GITHUB_OUTPUT设置的输出变量,只能通过steps.[步骤id].outputs.[变量名]来引用。你之前的写法错误地将所有Job输出都绑定到了changed_files步骤,而该步骤并没有输出其他几个变量,导致后续Job只能拿到存在的changed_files值,其他输出因为引用不存在的变量而被忽略。

修改后,每个Job输出都对应到正确的步骤输出,第二个Job就能获取到所有定义的输出值了。

内容的提问来源于stack exchange,提问作者Owen C. Jones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:08:27