Reputation: 2011
I'm using Nx for monorepo support on a new project. One of the benefits of Nx is that it can determine which apps in the monorepo are affected by a range of changes (start commit, end commit). So if you have a bunch of apps, you only have to build, test, and deploy the apps that are actually affected by the changes instead of the entire monorepo.
I'd like to setup a GitHub Action workflow to deploy only the affected apps on push or merge to master. However, I'm having trouble figuring out how to get the "start commit" for the range of changes. In other words, how do I get the commit hash of the last deploy?
GitHub provides an env variable GITHUB_SHA
but that's the commit that triggered the workflow (i.e. the "end commit"). It also provides GITHUB_BASE_REF
but that only works on workflows running from a forked repo comparing to the head repo.
CircleCI has pipeline.git.base_revision
for this purpose. Do GitHub Actions have something similar?
Upvotes: 24
Views: 21296
Reputation: 41940
For pull request events the ref and sha for the base can be found in the github context as follows.
${{ github.event.pull_request.base.ref }}
${{ github.event.pull_request.base.sha }}
For push events there are base_ref
and before
parameters.
${{ github.event.base_ref }}
${{ github.event.before }}
before
is the last git sha pushed to origin on branch base_ref
. Note that if this is the first commit on a new branch, base_ref
and before
will have null/default values as shown below.
##[debug] "event": {
##[debug] "after": "727f7aec97c394083d769029e5f619e9b094a235",
##[debug] "base_ref": null,
##[debug] "before": "0000000000000000000000000000000000000000",
...
By the way, you can dump the github context and check the available parameters by adding this step to your workflow:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
Upvotes: 51
Reputation: 76469
GitHub provides GITHUB_BASE_REF
and the github.base_ref
context that contain the base branch.
If you're interested in the latest revision of that branch, you can run git rev-parse $GITHUB_BASE_REF
to find it. If you're interested in the point at which the branches forked, you can run git merge-base $GITHUB_BASE_REF $GITHUB_SHA
to find it.
Do note that it's possible to break other projects with incompatible API changes and such without making any code changes to them, so while it will be faster to test only the apps that have changed, you may find that doing so will result in unexpected breakage.
Upvotes: -1