I am using Renovate to generate PRs with a git trailer. These PRs always only contain a single commit. I have configured Renovate to embed a list of the updates in the commit body, and now I want to fetch that list in a Github Actions step.
Currently, I set GITHUB_SHA={{github.event.pull_request.head.sha}}
and then I use git log ${GITHUB_SHA}^1..${GITHUB_SHA}^2
with the right formatting argument, which extracts the trailer.
It works, but it seems fragile. It requires the merge commit parents to be ordered. It requires the merge commit to be present. I want the step to be reusable, and to work even if the checkout action is used with an alternative ref. Currently, it would break given that it operates on the merge commit.
How can I more succinctly extract a git trailer from a Renovate generated PR?
My current step, which seems too inelegant (from source):
- name: Extract New-Versions git trailer from Renovate
if: ${{ github.event_name == 'pull_request' }}
env:
GITHUB_SHA: "{{ github.event.pull_request.head.sha }}"
run: |
echo 'packages: .' > cabal.project
for constraint in $(git log "--format=%(trailers:key=New-Versions,valueonly=true)" ${GITHUB_SHA}^1..${GITHUB_SHA}^2)
do echo "constraints: $constraint" >> cabal.project
done
You can't overwrite an environment variable already set by Github.
The env
section has no effect, and that's why GITHUB_SHA doesn't refer to the pull request head as intended.
If you use git log ${{ github.event.pull_request.head.sha }} -1
instead of using environment variables, you get the same result without worrying about the merge commit. And of course it is independent of which commit is checked out, since it specifies the exact commit to operate on.
But remember that you need to set fetch-depth
, for example to 0
. Otherwise it won't know the PRs head commit SHA.