Search code examples
github-actions

How to generate a new string by combine github action workflow variables


I need to generate environment variable from ${{ github.event.head_commit.message }}, ${{ github.sha }} and here is what I tried so far

- name: Setup env var
  run: |
    echo MESSAGE_SHA=${{ github.event.head_commit.message }}-${{ github.sha }}) >> $GITHUB_ENV #only the dash and the SHA is print out 
    echo ${{ env.MESSAGE_SHA }} #Nothing prints out

The concatenation for the commit message and commit SHA with the dash is not working at all. How can I solve this issue?


Solution

  • I made it work here using this workflow implementation.

    For what I observed, there are 2 issues in your implementation:

    First issue

    According to the documentation, when setting an environment variable, you can't access the variable in the same step.

    You can make an environment variable available to any subsequent steps in a workflow job by defining or updating the environment variable and writing this to the GITHUB_ENV environment file. The step that creates or updates the environment variable does not have access to the new value, but all subsequent steps in a job will have access. The names of environment variables are case-sensitive, and you can include punctuation. For more information, see "Environment variables."

    Therefore, your implementation should instead looks like this:

    - name: Setup env var
      run: |
        echo MESSAGE_SHA='${{ github.event.head_commit.message }}'-'${{ github.sha }}' >> $GITHUB_ENV
    
    - name: Check env var
      run: |
        echo ${{ env.MESSAGE_SHA }}
    

    However, that wouldn't be enough depending the context, due to the second point.

    Second issue

    The ${{ github.event.head_commit.message }} variable can be a multiline variable, in that case, you would need to create the variable with another syntax before adding it to the $GITHUB_ENV.

    The solution I came with was using this implementation:

          - name: Setup env var
            run: |
              MESSAGE=$(cat << EOF
              '${{ github.event.head_commit.message }}'
              -'${{ github.sha }}'
              EOF
              )
              echo MESSAGE_SHA=$MESSAGE >> $GITHUB_ENV
    

    Therefore, combining both concepts above, your workflow implementation should look like this to achieve what you want:

          - name: Setup env var
            run: |
              MESSAGE=$(cat << EOF
              '${{ github.event.head_commit.message }}'
              -'${{ github.sha }}'
              EOF
              )
              echo MESSAGE_SHA=$MESSAGE >> $GITHUB_ENV
    
          - name: Check env var
            run: |
              echo ${{ env.MESSAGE_SHA }}
    

    Note that using echo MESSAGE_SHA='${{ github.event.head_commit.message }}'-'${{ github.sha }}' >> $GITHUB_ENV could work if you can always guaranty the commit message is a single line.

    EDIT:

    This syntax also works to set multilines outputs (and looks easier to use):

          run: |
             echo "TEST=first line \
              second line \
              third line" >> $GITHUB_OUTPUT