GitHub 组合操作的输出不能在 bash 脚本内设置。

huangapple go评论60阅读模式
英文:

Github composite action output can not be set from within a bash script

问题

对于我的一个项目,我正在从一个在复合动作中执行的bash脚本中设置一个动作输出。我发现GitHub有很好的文档,说明了如何创建一个GitHub复合动作输出。它指出可以使用以下的action.yml文件来实现。

name: 'Hello World'
description: '向某人打招呼'
inputs:
  who-to-greet:  # 输入的ID
    description: '要打招呼的人'
    required: true
    default: 'World'
outputs:
  random-number:
    description: "随机数"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - run: echo 你好 ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
    - run: echo "${{ github.action_path }}" >> $GITHUB_PATH
      shell: bash
    - run: goodbye.sh
      shell: bash

我使用以下动作工作流检查了结果,它有效。

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: 打招呼的工作
    steps:
      - uses: actions/checkout@v3
      - id: foo
        uses: actions/hello-world-composite-action@v1
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo 随机数 ${{ steps.foo.outputs.random-number }}
        shell: bash

然而,我的用例与上面的示例不同,因为我必须在goodbye.sh脚本内设置输出变量。根据文档的说法,应该使用GITHUB_OUTPUT变量来完成:

echo "{name}={value}" >> $GITHUB_OUTPUT

经过一些测试,这种方法在复合动作中不起作用。由于这也可能是一个错误或不受支持,我在https://github.com/orgs/community/discussions/47775上创建了一个错误报告。然而,我想迅速再次确认一下,看看我的语法是否有问题。

复现步骤

  1. Fork 这个仓库
  2. 在分支上启用GitHub actions。
  3. 向你的分支推送一个提交。
  4. 查看只有random-number变量被设置,而random-number-bash没有被设置(参见这个示例工作流)。
英文:

For one of my projects, I am setting an action output from within a bash script that is executed inside a composite action. I found that GitHub has excellent documentation on how to create a GitHub composite action output. It states that this can be done using the following action.yml file.

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-number }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
    - run: echo "${{ github.action_path }}" >> $GITHUB_PATH
      shell: bash
    - run: goodbye.sh
      shell: bash

I checked the results using the following action workflow, and it works.

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v3
      - id: foo
        uses: actions/hello-world-composite-action@v1
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number ${{ steps.foo.outputs.random-number }}
        shell: bash

My use case, however, differs from the example above in that I have to set the output variable inside the goodbye.sh script. According to the documentation, this should be done using the GITHUB_OUTPUT variable:

echo "{name}={value}" >> $GITHUB_OUTPUT

After some testing, this method is not working for composite actions. As this could also be a bug or not supported, I created a bug report at https://github.com/orgs/community/discussions/47775. However, I quickly wanted to double-check if there may be something wrong with my syntax.

Steps to reproduce

  1. Fork this repository.
  2. Enable GitHub actions on the fork.
  3. Push a commit to your fork.
  4. See that only the random-number variable is set while the random-number-bash` is set (See this example workflow).

答案1

得分: 4

I found my issue using @benjamin-w's comment. The problem was that the goodbye.sh step should contain an id key for the created output to be referenced correctly. The correct syntax should be:

action.yml

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-number }}
  random-number-bash:
    description: "Random number bash"
    value: ${{ steps.random-number-generator-bash.outputs.random-number-bash }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
    - run: echo "${{ github.action_path }}" >> $GITHUB_PATH
      shell: bash
    - run: goodbye.sh
      id: random-number-generator-bash
      shell: bash

And the correct syntax for creating the output in the goodbye.sh script should be:

Goodbye.sh

echo "Goodbye"
echo "random-number-bash=$(echo 123)" >> $GITHUB_OUTPUT

Which then can be tested using the following workflow file:

Test workflow

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v3
      - id: foo
        uses: rickstaa/hello-world-composite-action-output-bug@main
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number ${{ steps.foo.outputs.random-number }}
        shell: bash
      - run: echo random-number ${{ steps.foo.outputs.random-number-bash }}
        shell: bash
英文:

I found my issue using @benjamin-w's comment. The problem was that the goodbye.sh step should contain an id key for the created output to be referenced correctly. The correct syntax should be:

action.yml

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-number }}
  random-number-bash:
    description: "Random number bash"
    value: ${{ steps.random-number-generator-bash.outputs.random-number-bash }}
runs:
  using: "composite"
  steps:
    - run: echo Hello ${{ inputs.who-to-greet }}.
      shell: bash
    - id: random-number-generator
      run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
      shell: bash
    - run: echo "${{ github.action_path }}" >> $GITHUB_PATH
      shell: bash
    - run: goodbye.sh
      id: random-number-generator-bash
      shell: bash

And the correct syntax for creating the output in the goodbye.sh script should be:

Goodbye.sh

echo "Goodbye"
echo "random-number-bash=$(echo 123)" >> $GITHUB_OUTPUT

Which then can be tested using the following workflow file:

Test workflow

on: [push]

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to say hello
    steps:
      - uses: actions/checkout@v3
      - id: foo
        uses: rickstaa/hello-world-composite-action-output-bug@main
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number ${{ steps.foo.outputs.random-number }}
        shell: bash
      - run: echo random-number ${{ steps.foo.outputs.random-number-bash }}
        shell: bash

huangapple
  • 本文由 发表于 2023年2月18日 04:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定