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

Mas*_*lor 20 github-actions building-github-actions

让我们以 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
Run Code Online (Sandbox Code Playgroud)

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


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 }}'
            })
Run Code Online (Sandbox Code Playgroud)

Mas*_*lor 33

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

代替:

${{ steps.hello-world.outputs.random-number.value }}
Run Code Online (Sandbox Code Playgroud)

引用时应不带.value

${{ steps.hello-world.outputs.random-number}}
Run Code Online (Sandbox Code Playgroud)

现在可以了。

  • 出于某些安全考虑,“set-outputs”已被弃用,取而代之的是“$GITHUB_OUTPUT”。请参阅 [GitHub 博客](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) 了解官方说法。我发现 [this action repo](https://github.com/anothrNick/github-tag-action/blob/master/entrypoint.sh) 有一个很好的例子来处理这个变化,第 53 行有一个函数并调用第 123-124 行。 (2认同)