Get the last commit message and store it to an env variable in actions/checkout@v2

As title.

How can I get the last commit message and store it to an env variable in actions/checkout@v2

1 Like

Hi @sblantipodi ,

The last commit message can be got from Github context :  ${{ github.event.head_commit.message }} , and set it as env as below:

- name: checkout
        uses: actions/checkout@v2
        env:
          message: ${{ github.event.head_commit.message }}

commitmessage.png

BTW, actions/checkout doesn’t need env setting since not related code are defined, the env won’t be invoked for the action, unless you rewrite the code to invoke the env variable.

Thanks.

1 Like

thanks for the answer but this code prints an empty message.

- uses: actions/checkout@v2
      env:
        commitmsg: ${{ github.event.head_commit.message }}
    - name: get commit message
      run: |
          echo Commit MSG = $commitmsg

any help is much appreciated. :slight_smile: thanks.

Hi @sblantipodi ,

Thanks for your quick reply! This is a step level env which only works in checkout step.

If you’d like to use the env variable in the other steps, please set it as job level env, code sample as below:

jobs:
  job1:
    env:
      commitmsg: ${{ github.event.head_commit.message }}
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: get commit message
        run: |
            echo Commit MSG = ${{ env.commitmsg }}

Or you can use ‘set-env’ to set/update the env variables. Code sample as below:

- name: set the head commit message as env
        run: |
          echo '::set-env name=commitmsg::${{ github.event.head_commit.message }}'
      - name: checkout
        uses: actions/checkout@v2
      - name: get commit message
        run: |
            echo Commit MSG = $commitmsg

Thanks.

2 Likes

Hey, @weide-zhou
Just wondering if github.event.head_commit.message is documented anywhere?
I looked it up here: https://help.github.com/en/actions/reference/events-that-trigger-workflows and here https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context without luck :slight_smile:

Hi @zanona,

The value comes from github context, please check the doc here: https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context

You can export the github context with below code for your workflow:

      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

It will output github context as json format, then you can follow the structure to get the content you required.

Thanks.

1 Like