In a GitHub Actions workflow triggered by a push event, I want to use the actions/checkout action to fetch the git history of all commits in the push event, plus the last commit before the push event (without having to fetch the entire history).
However, actions/checkout only provides the fetch-depth parameter, which inputs the number of commits to fetch, but that is not known. The push event payload contains the list of commits as an array, but there is no native function in GitHub Actions to count the length of an array. One could write an extra step before using checkout to count the number of commits in the payload, but that seems a bit too hacky.
Isn't there a straightforward way to fetch the history from right before a push event? That seems like a very common thing to do, so it's odd that I can't find an easy way to do it, something like:
- needs: actions/checkout@v3
with:
- from-ref: ${{ github.event.before }}
A cleaner way that I could find for pull_requests was:
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: $(( ${{ github.event_name == 'pull_request' && github.event.pull_request.commits || 0 }} + 1 ))
Using the ternary operation mentioned in the docs. So we don't need an additional step.
You could use the same format for push too, though the event name and commit count would need to be changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With