Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git history in a Github Action

I would like to log the git history in a Github Action. But it seems that the Action's environment has a different one:

echo $(git log -5 --oneline)

shell: /bin/bash -e {0}

7c0faa6 Merge c245982a87ef5538d42ab905706faa08f4d67ce9 into 8a939ef1f71eaecac0ae52d625dad3e3c9fa4c16

This is not a git log and none of these hashes matches what I have in my repo.

Why is that?

How to access the commit history from the environment of a Github Action?

like image 631
Mouloud88 Avatar asked Jul 23 '26 09:07

Mouloud88


1 Answers

You are experiencing this because your workflow is running on pull_request events. During those events, GITHUB_REF is a merge commit from the head branch to the base. The intention of this is to run CI against the merge commit to check it passes before it's actually merged.

If you don't want the merge commit and want to checkout the head commit instead, you have to pass the HEAD's sha as ref to the checkout. You can change the checkout as follows (taken from this example).

Also, that checkout is shallow by default, meaning it only has the last commit. To read more than the last commit, pass a non zero number to fetch-depth, or zero as well, which will fetch the entire history (default is 1):

- uses: actions/checkout@v2
  with:
    ref: ${{ github.event.pull_request.head.sha }}
    fetch-depth: 0

See the documentation here for the pull_request event.

like image 134
peterevans Avatar answered Jul 24 '26 23:07

peterevans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!