June 1st, 2020

Run tests using GitHub actions on push in SpringBoot project

When you use GitHub and you really want to run all tests when a new PR is created, or just when code is pushed, then you can use Github Actions.

In the root of your project create a directory named ".github" and inside it another directory named "workflows". Here we will add our .yml script that can be modified to fit your needs and extended. Bear with me, I am still learning this as I go and this is a basic script.

Yaml script to run testes on push in github


1. name: Maven Run Tests when push to Master and PRs.
This is what it says. It's the name I choose for my script

2. on:

- master
Run this script when a Push is identified directly on `master` branch

3. on:

- master
- # Push events to branches matching refs/heads/card-*
- 'card-/**'

Run this script when a new PR is created on `master` or `card-**` branches. The latter is specific to the way I want to name my feature branches. You would delete or change this to your own pattern naming.

4. jobs:
runs-on: windows-latest

I am running this script on windows. You can use an array here or just another OS. Example: [ubuntu-latest, windows-latest]

5. steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.11
uses: actions/setup-java@v1
java-version: 1.11
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Test with Maven
run: mvn -B test --file pom.xml

I am using Maven with Java 11 (and I want to update to Java 14) and running the `mvn test` command.

Result after I created a new PR:

Yaml steps description

This is everything that is needed for a basic .yml script to run tests. I tested this a few times and a gotcha that you need to take care of is to USE SPACE `key: value` in this pattern.

Happy coding!