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 }}
Currently, this is my workaround:
As mentioned in the question, an additional step is needed before using
actions/checkoutto calculate thefetch-depth. Here, we first check thegithub.event_namecontext; if it'spush, we then output the length of thegithub.event.commitsarray plus 1, otherwise we output 1, and then use this output in thecheckoutstep as thefetch-depthargument.It's not pretty, but currently it's the most straightforward approach I could find. I have opened an issue in
actions/checkoutto add native support: https://github.com/actions/checkout/issues/1444