Run tests using GitHub actions in SpringBoot project

Show a related board with postit notes

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:

name: Maven Run Tests when push to Main branch and newly created PRs.

on:
    # Trigger the workflow on push for the main branch
    # and pull request for main and card-* prefix branches
    push:
        branches:
            - main
        pull_request:
            branches:
                - main
                - # Push events to branches matching refs/heads/card-*
                - 'card-/**'
jobs:
    build:
        runs-on: windows-latest
        steps:
            - uses: actions/checkout@v2
            - name: Set up JDK 1.11
                uses: actions/setup-java@v1
                with:
                    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
    

Explanations:

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

  2. on: push: branches:
    • main Run this script when a Push is identified directly on main branch
  3. on: pull_request: branches:
    • main
    • Push events to branches matching refs/heads/card-*
    • ‘card-/**’

Run this script when a new PR is created on main 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.

  1. jobs: build: 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]

  1. steps:
    • uses: actions/checkout@v2
    • name: Set up JDK 1.11 uses: actions/setup-java@v1 with: 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 Git Actions Result

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.

Notes mentioning this note

There are no notes linking to this note.