Search code examples
github-actions

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


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.


Solution

  • 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"