Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 2

please reconsider setting GITHUB_COMMIT_MESSAGE or equivalent

It is suprisingly difficult to obtain the commit message in it's full glory due to various limitations.  I understand that setting GITHUB_COMMIT_MESSAGE may not make sense for all different types of events, but I believe that setting this value for push and pull_request events should be done for us.

 

https://github.community/t5/GitHub-Actions/Accessing-commit-message-in-pull-request-event/m-p/40925/... by @Yanjingzhu seems to have worked in the past, but for me this will result in a fatal: bad object <sha>.  I believe what happens for pull_request is that a bare repo is initialized with exactly one commit, which is the refs/pull/XYZ/merge commit.  So the original commit is not in the history anymore.

 

Here is what I was able to achieve as a linux-only solution (note: I was working on a Python project, you could omit that and just install / use jq or something to parse the json):

 
jobs:
  dist:
    name: Distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Python
        uses: actions/setup-python@v1
        with:
          python-version: 3.x
      - name: Extract Commit Message (Pull Request)
        if: github.event_name == 'pull_request'
        run: |
          # For pull requests, github.ref will be e.g., refs/pull/XYZ/merge.  We
          # want to query the /head commit, so replace /merge with /head.
          ref="$(echo '${{ github.ref }}' | sed 's@^\(refs/pull.*\)/merge$@\1/head@g')"
          curl https://api.github.com/repos/${{ github.repository }}/commits/$ref > commit.json
          msg="$(python -c 'import json; print(json.load(open("commit.json"))["commit"]["message"])')"
          # ::set-env and friends choke on multiline values, replace newlines.
          # https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/37870
          msg="${msg//'%'/'%25'}"
          msg="${msg//$'\n'/'%0A'}"
          msg="${msg//$'\r'/'%0D'}"
          echo "::set-env name=COMMIT_MESSAGE::$msg"
      - name: Extract Commit Message (Push)
        if: github.event_name == 'push'
        run: |
          # ::set-env and friends choke on multiline values, replace newlines.
          # https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/37870
          msg="${{ github.event.head_commit.message }}"
          msg="${msg//'%'/'%25'}"
          msg="${msg//$'\n'/'%0A'}"
          msg="${msg//$'\r'/'%0D'}"
          echo "::set-env name=COMMIT_MESSAGE::$msg"
      - name: DEBUG COMMIT MESSAGE
        run: |
          echo "Commit Message"
          echo "$COMMIT_MESSAGE"

I have opened a new issue because the ones I have found either have been marked as resolved (but have outdated answers), or the answer is incomplete (e.g., only gets you the first line of the commit message).  Please consider making an official GITHUB_COMMIT_MESSAGE environment variable and populating it for us, the above solution is overly complicated, and doesn't work on every platform.

 

I'm really hoping I've overlooked something, and please correct me if there is a more direct solution!  But it seems that something changed relatively recently that prevents the old solutions for pull_request working.  Thank you for your consideration, many people want to perform or skip actions via phrases in their commit messages but it is currently very difficult to achieve this.

1 Reply
Highlighted
GitHub Partner
Message 2 of 2

Re: please reconsider setting GITHUB_COMMIT_MESSAGE or equivalent

Thank you for your suggestion. I would like direct you to Feedback form for GitHub Actions to share your idea. 

As you said the previous workaround there https://github.community/t5/GitHub-Actions/Accessing-commit-message-in-pull-request-event/m-p/40925/... could not work in your side. What's the GITHUB context like? Can you share the repo url and the workflow runs url here?