Retrieve commit message in a GH Action triggered on pull request

410 Views Asked by At

I want to create a GitHub Actions workflow triggered on pull_request. The workflow should get the last commit message in the Pull Request and check it against a given regular expression.

The part I am struggling on is actually getting the last commit message.

My workflow looks like this:

---
name: CI-PR-Check

on:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
 
      - name: Check commit message
        shell: bash
        run: |
          echo "commit message validation"
          message=$(git log --pretty=%B -n 1)
          python ${{ github.action_path }}/commitcheck.py "$message"

I expected the line message=$(git log --pretty=%B -n 1) to get me the message of the last commit, but I get the message from the pull request, so something like Merge <some sha> into <other sha>.

Using GitHub webhook events, I am able to get the sha of the commit that I am interested in using sha="${{ github.event.pull_request.head.sha }}". However, it is still not possible to get the commit message with git log --format=%B -n 1 $sha. I get an error fatal: bad object <my sha>.

Is there any way to get my commit message ?

0

There are 0 best solutions below