skip to Main Content

I am trying to understand where is my mistake:
I want to set an output var in a composite file but I keep getting an empty result:

'Output - ${{ steps.hello-world.outputs.random-number }}' returns 'Output - '
Like '${{ steps.hello-world.outputs.random-number }}' is never set.

main-workflow.yml:

name: My action 

on:
  workflow_dispatch:
    
jobs:
  test-job:
    runs-on: ubuntu-22.04
    steps:
      - name: Call Hello World 
        id: hello-world
        uses: actions/hello-world-action@v1
      - name: Comment
        run: echo 'Output - ${{ steps.hello-world.outputs.random-number }}'

The file in which I want to set the variable:

name: 'Hello World'
description: 'Greet someone'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: false
    default: 'World'
outputs:
  random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
  using: "composite"
  steps:
    - name: Random number generator
      id: random-number-generator
      run: "random-number=$(echo 123)" >> $GITHUB_OUTPUT
      shell: bash

2

Answers


  1. Chosen as BEST ANSWER

    I am sure that more people will come across the same issue as I did.

    This is my final code:

    main-workflow.yml:

    name: My action 
    
    on:
      workflow_dispatch:
        
    jobs:
      test-job:
        runs-on: ubuntu-22.04
        steps:
          - name: Checkout
            uses: actions/checkout@v1
          - name: Call Hello World 
            id: hello-world
            uses: ./.github/workflows/hello-world-action
          - name: Comment
            run: echo 'Output - ${{ steps.hello-world.outputs.random-number }}'
          - name: Composite Check
            uses: ./.github/workflows/read-output
            with: 
              rand: ${{ steps.hello-world.outputs.random-number }}
    

    hello-world-action.yml:

    Let's take this example composite action found on Github's documentation:

    name: 'Hello World'
    description: 'Greet someone'
    inputs:
      who-to-greet:  # id of input
        description: 'Who to greet'
        required: false
        default: 'World'
    outputs:
      random-number:
        description: "Random number"
        value: ${{ steps.random-number-generator.outputs.random-number }}
    runs:
      using: "composite"
      steps:
        - name: Random number generator
          id: random-number-generator
          run: |
            test=1234
            echo "random-number=$test" >> $GITHUB_OUTPUT
          shell: bash
    

  2. In the provided code, it seems there are a couple of issues that are causing the empty result. I’ll address them below and provide a corrected version of the composite action file.

    Incorrect Output Value Assignment:
    The output value "random-number" in the composite action file is not being assigned correctly. The value field should be a reference to the output of the step named "random-number-generator." Instead, it is referencing itself, which causes the output to be empty. To fix this, you need to use the correct step reference for the value assignment.

    Incorrect Shell Command Format:
    The shell command in the composite action file seems to have an incorrect format. The run field should contain the actual command that generates the random number. The provided command "random-number=$(echo 123)" is not correctly formatted.

    Here’s the corrected version of your composite action file:

    yaml
    Copy code
    name: ‘Hello World’
    description: ‘Greet someone’
    inputs:
    who-to-greet:
    description: ‘Who to greet’
    required: false
    default: ‘World’
    outputs:
    random-number:
    description: "Random number"
    value: ${{ steps.random-number-generator.outputs.random-number }}
    runs:
    using: "composite"
    steps:
    – name: Random number generator
    id: random-number-generator
    run: |
    echo "::set-output name=random-number::123"
    Explanation:

    In the random-number-generator step, we are using the echo command to generate the random number "123."
    We are using the ::set-output syntax to set the output variable "random-number" to the value "123." This makes the random number available as an output for the composite action.
    With this corrected composite action file, the output variable should be set correctly, and you should see the expected result in the main workflow when you access the output using ${{ steps.hello-world.outputs.random-number }}.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search