2

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?

2
  • It depends on the event payload; does it have a top-level field head_commit? You can dump the event with jq . "$GITHUB_EVENT_PATH". Commented Oct 18, 2022 at 20:53
  • @BenjaminW. I tried to print out the github.event.head_commit.message it is Merge <SHA1> into <SHA2> and the echo of thegithub.sha printed a different SHA with SHA1 and SHA2. More background is I have a PR from feature branch to main, I want to fetch the commit message and the SHA that I pushed to the feature branch Commented Oct 18, 2022 at 21:03

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.