cfpb / hmda-platform

The HMDA Submission backend applications.
Creative Commons Zero v1.0 Universal
103 stars 94 forks source link

Add unit test github actions pipeline #4760

Open jaredb96 opened 6 months ago

jaredb96 commented 6 months ago

Github Actions PR Status Check Instructions

These instructions are to add the ability to block the merge to master of any branch on an open PR that causes a unit test to fail. We do this through Github Actions.

  1. Create a branch for pushing the action to the remote repo
  2. Create a folder .github/workflows in the hmda-platform root directory
  3. Add a yaml file to that workflows folder. It will tell Github to run unit tests when you make a PR. You can call it whatever you want, we'll call ours unit-tests.yml
  4. Add the following lines to this yaml file that tells GH to set up sbt and run the unit tests:
    
    name: HMDA Unit Tests

on: pull_request: branches:

jobs: tests: runs-on: ubuntu-latest

steps:
  - name: Checkout
    uses: actions/checkout@v4

  - name: Setup JDK
    uses: actions/setup-java@v3
    with:
      java-version: '17'
      distribution: 'adopt'

  - name: Run Sbt Tests
    run: |
      touch log-file
      sbt test > log-file
    continue-on-error: true

  - name: Check Test Results
    run: |
      if [ $(cat log-file | grep -E "TEST FAILED|TESTS FAILED|Failed tests" | wc -l) -gt 0 ]; then
        echo "Unit tests failed."
        exit 1
      else
        echo "Unit tests passed."
      fi

5. Add the yaml file, commit the change and push to the remote repo. Now the workflow should be on Github
6. On Github, go to Settings -> Branches, then click Add Branch Rule. Type in "master" as the branch name and click the box that says "Require status checks to pass before merging". Search up the status check "tests" that you just added to github and add that. 

That's it! Now when you make a PR, if it breaks any of the unit tests it will not allow you to merge the PR to master, as seen below

When you fix the code that breaks the test on your branch and push your changes, Github will automatically run the tests action so that you don't have to worry about creating a new PR

Branch Rule 
<img width="1512" alt="branch_rule" src="https://github.com/cfpb/hmda-platform/assets/41237993/9cbbdc71-1d4e-4657-a14c-6c016b128398">

Failing test:
<img width="1512" alt="failing_test" src="https://github.com/cfpb/hmda-platform/assets/41237993/27ab4157-f39f-4ae2-a63a-abbfabd7e0ea">

Succeeding test:
<img width="1509" alt="succeeding_test" src="https://github.com/cfpb/hmda-platform/assets/41237993/bf0f4326-c7ba-4a5b-bb08-c50919983d08">