Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the previous commit before a push or merge in GitHub Action workflow?

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?

like image 935
Rich McCluskey Avatar asked May 18 '20 00:05

Rich McCluskey


People also ask

How do I rollback an action in GitHub?

Go to Actions at the top of your Github repository. Click on Manual Undo Push Action (or other name you have given) under All workflows. You will see Run workflow , click on it. Fill in the branch to undo the most recent push ( ⚠️ make sure it is correct)

What is Workflow_dispatch in GitHub Actions?

This action triggers another GitHub Actions workflow, using the workflow_dispatch event. The workflow must be configured for this event type e.g. on: [workflow_dispatch] This allows you to chain workflows, the classic use case is have a CI build workflow, trigger a CD release/deploy workflow when it completes.

How do I run jobs sequentially in GitHub Actions?

To run jobs sequentially, you can define dependencies on other jobs using the jobs. <job_id>. needs keyword. Each job runs in a runner environment specified by runs-on .


1 Answers

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"
like image 137
peterevans Avatar answered Oct 19 '22 11:10

peterevans