Accessing commit message in pull_request event

tl;dr:

Feaure request: Make head_commit available with pull_request’s synchronize action.

I’ve a workflow which triggers on pull_request’s default types/actions. I want to run a job when there are new commits pushed to a PR, but I wanted to run the job only if commit message included specific keywords. So, I tried to access <font face="courier new,courier">github.event.head_commit.message</font>, as suggested on various forums.

But unfortunately, head_commit is not available during pull_request’s synchronize action. Following is a relevant bit from github context dump:

{
  "event_name": "pull_request",
  "event": {
     "action": "synchronize",
  }
}:  
3 Likes

Yes, currently you can’t see commit message in pull_request‘s synchronize event. But you can get the commit id in github.event.after.

after commit.png

Then you can run a git command to get the commit message : git log --format=%B -n 1 <commit>

Please set this commit message as the value of an environment variable using set-env command.

Then you can add if condition in all your next steps, to judge if commit message included specific keywords

Please see my example:

name: filter commit nessage
on: pull_request

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name : GITHUB CONTEXT
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

      - name: get commit message
        run: |
           echo ::set-env name=commitmsg::$(git log --format=%B -n 1 ${{ github.event.after }})
      - name: show commit message
        run : echo $commitmsg

      - name: step1
        if: contains( env.commitmsg , 'try' )
        run: echo hello

filter commit message.png

4 Likes

This workaround is available only under “steps”. Would be great if it was available in github context and we could skip a “job”.

I did a similar workaround, which is pretty repetitive:

steps:
      - uses: actions/checkout@v1
      - id: log
        run: echo "::set-output name=message::$(git log --no-merges -1 --oneline)"
      - if: "!contains(steps.log.outputs.message, 'ci skip')"
        uses: actions/setup-node@v1
        with:
          node-version: 10
      - if: "!contains(steps.log.outputs.message, 'ci skip')"
        run: npm install
6 Likes

This just bit me and I was about to post about it.

In my opinion, something like this should work for any commit-driven trigger, whether via pull_request or push:

jobs:
  build:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'ci skip') && !contains(github.event.head_commit.message, 'skip ci')"
3 Likes

Did GitHub remove

github.event.head_commit.message

I don’t see it anywhere in my context dump.

1 Like

Any new on that feature ? Would be really helpfull