GitHub 组合操作的输入是否可以使用自身输入作为默认值?

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

can github composite action inputs use self inputs as default values?

问题

I want if the value of a is not set the value of b be set as the value of a.

如果a的值未设置,则将b的值设置为a的值。

Please note that the code section remains unchanged in Chinese as you requested.

英文:

does this work?

name: "test"
description: "test"
inputs:
  a:
    description: "Description of a"
    required: false
    default: ${{ inputs.b }}
  b:
    description: "Description of b"

runs:
  using: "composite"
  steps:
    - name: docker-login-build-push
      run: |
        echo {{ inputs.a }}
        echo {{ inputs.b }}

      shell: bash

I want if the value of a is not set the value of b be set as the value of a.

答案1

得分: 1

inputs.<input_id>.default 没有提到在 default 中使用 inputs

但是,您可以自行测试和验证。

我运行了这个简单的测试,结果出现了这个 错误

> 无法识别的命名值:'inputs'。位于表达式内的位置 1:inputs.a

这意味着它不受支持


这是我用来验证的操作和工作流:

.github/actions/test/action.yml

name: Test
description: Test

inputs:
  a:
    description: a
  b:
    description: b
    default: '${{ inputs.a }}';

runs:
  using: composite

  steps:
    - name: Test
      shell: bash
      run: |
        echo 'a: ${{ inputs.a }}'
        echo 'b: ${{ inputs.b }}'        

.github/workflows/ci-test-action.yml

name: test_action

on: workflow_dispatch

jobs:
  ci:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Test
      uses: ./.github/actions/test

这是完整的错误日志

Error: /home/runner/work/test/test/./.github/actions/test/action.yml (Line: 11, Col: 14):
Error: /home/runner/work/test/test/./.github/actions/test/action.yml (Line: 11, Col: 14): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.a
Error: GitHub.DistributedTask.ObjectTemplating.TemplateValidationException: The template is not valid. /home/runner/work/test/test/./.github/actions/test/action.yml (Line: 11, Col: 14): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.a
   at GitHub.DistributedTask.ObjectTemplating.TemplateValidationErrors.Check()
   at GitHub.Runner.Worker.ActionManifestManager.ConvertRuns(IExecutionContext executionContext, TemplateContext templateContext, TemplateToken inputsToken, String fileRelativePath, MappingToken outputs)
   at GitHub.Runner.Worker.ActionManifestManager.Load(IExecutionContext executionContext, String manifestFile)
Error: Fail to load /home/runner/work/test/test/./.github/actions/test/action.yml

关于:

> 如果 a 的值未设置,b 的值应设置为 a 的值。

这应该可以工作:

${{ inputs.a && inputs.a || inputs.b }}

即:

echo '${{ inputs.a && inputs.a || inputs.b }}'

您可以将 inputs 设置为环境变量,应用这些验证,并在 shell/Bash 中像这样使用:

.github/actions/test/action.yml

name: Test
description: Test

inputs:
  a:
    description: a
  b:
    description: b

runs:
  using: composite

  steps:
    - name: Test
      env:
        A: '${{ inputs.a && inputs.a || inputs.b }}'
        B: '${{ inputs.b }}'
      shell: bash
      run: |
        echo "A: $A"
        echo "B: $B"        
英文:

inputs.<input_id>.default doesn't say anything about the use of inputs in default.

But, you can test and verify it yourself.

I ran this simple test resulting in this error:

> Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.a

That means it's not supported.


Here's the action and workflow that I used to verify this:

.github/actions/test/action.yml

name: Test
description: Test

inputs:
  a:
    description: a
  b:
    description: b
    default: '${{ inputs.a }}'

runs:
  using: composite

  steps:
    - name: Test
      shell: bash
      run: |
        echo 'a: ${{ inputs.a }}'
        echo 'b: ${{ inputs.b }}'        

.github/workflows/ci-test-action.yml

name: test_action

on: workflow_dispatch

jobs:
  ci:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - name: Test
      uses: ./.github/actions/test

Here's the complete error logs:

Error: /home/runner/work/test/test/./.github/actions/test/action.yml (Line: 11, Col: 14):
Error: /home/runner/work/test/test/./.github/actions/test/action.yml (Line: 11, Col: 14): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.a
Error: GitHub.DistributedTask.ObjectTemplating.TemplateValidationException: The template is not valid. /home/runner/work/test/test/./.github/actions/test/action.yml (Line: 11, Col: 14): Unrecognized named-value: 'inputs'. Located at position 1 within expression: inputs.a
   at GitHub.DistributedTask.ObjectTemplating.TemplateValidationErrors.Check()
   at GitHub.Runner.Worker.ActionManifestManager.ConvertRuns(IExecutionContext executionContext, TemplateContext templateContext, TemplateToken inputsToken, String fileRelativePath, MappingToken outputs)
   at GitHub.Runner.Worker.ActionManifestManager.Load(IExecutionContext executionContext, String manifestFile)
Error: Fail to load /home/runner/work/test/test/./.github/actions/test/action.yml

Regarding:

> I want if the value of a is not set the value of b be set as the value of a.

this should work:

${{ inputs.a && inputs.a || inputs.b }}

i.e.

echo '${{ inputs.a && inputs.a || inputs.b }}'

You may set inputs as env vars, apply these validations, and use those inside shell/Bash like this:

.github/actions/test/action.yml

name: Test
description: Test

inputs:
  a:
    description: a
  b:
    description: b

runs:
  using: composite

  steps:
    - name: Test
      env:
        A: '${{ inputs.a && inputs.a || inputs.b }}'
        B: '${{ inputs.b }}'
      shell: bash
      run: |
        echo "A: $A"
        echo "B: $B"        

答案2

得分: 0

这是我选择的解决方案:

name: "composite-test"
description: "test"
inputs:
  a:
    description: "如果a不适用,请将其值设置为空字符串 - 将使用b的值"
  b:
    description: "对b的一些描述"

runs:
  using: "composite"
  steps:
    - name: echo
      run: |
        A=${{ inputs.a }}
        B=${{ inputs.b }}

        if [ "$A" = "" ]; then
          echo "$B"
        else
          echo "$A"
        fi

        echo "$B"        
      shell: bash

请注意,我已将代码部分排除在外,只翻译了您提供的YAML配置部分。

英文:

here is a solution I am going with

name: "composite-test"
description: "test"
inputs:
  a:
    description: "If a is not applicable, set its value to an empty string if - the value of b will be used for it"
  b:
    description: "Some description for b"

runs:
  using: "composite"
  steps:
    - name: echo
      run: |
        A=${{ inputs.a }}
        B=${{ inputs.b }}

        if [ "$A" = "" ]; then
          echo "$B"
        else
          echo "$A"
        fi

        echo "$B"


      shell: bash

</details>



huangapple
  • 本文由 发表于 2023年5月13日 20:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242713.html
匿名

发表评论

匿名网友

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

确定