如何在工作流程中使用 Github action 的输出?

20

让我们来看一下 Github 文档中发现的这个 复合 操作的示例:

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "::set-output name=random-id::$(echo $RANDOM)"
      shell: bash
    - run: ${{ github.action_path }}/goodbye.sh
      shell: bash

我们该如何在调用此操作的外部工作流中使用特定输出random-number? 我尝试了以下代码片段,但目前工作流似乎无法从操作中读取输出变量,因为它为空 - 'Output - '


jobs:
  test-job:
    runs-on: self-hosted
    steps:
      - name: Call Hello World 
        id: hello-world
        uses: actions/hello-world-action@v1
      - name: Comment
        if: ${{ github.event_name == 'pull_request' }}
        uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Output - ${{ steps.hello-world.outputs.random-number.value }}'
            })

矩阵输出变量 https://github.com/orgs/community/discussions/25634 - Richard Tyler Miles
1个回答

33

看起来我的尝试是正确的,除了一个细节:

而不是:

${{ steps.hello-world.outputs.random-number.value }}

应该在不使用.value的情况下进行引用:

${{ steps.hello-world.outputs.random-number}}

现在它工作了。


1
看起来 set-output 现在已经被弃用了......而 ${{ steps.random-number-generator.outputs.random-id }} 似乎不再起作用 :( - Fuseteam
2
“set-outputs” 正在因安全考虑而被弃用,建议使用 $GITHUB_OUTPUT。请查看 GitHub 博客 获取官方说明。我找到了一个操作仓库 this action repo ,其中提供了一个良好的示例来处理这种变更,该示例函数位于第 53 行,调用位于第 123-124 行。 - Tom Willis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接