Start learning free

LESSON 155 OF 173 · BACKEND: SHIPPING IT

Run the Tests on Every Push

Practice

Continuous integration (CI) means your tests run by themselves every time you push. On GitHub you ask for it with a workflow: a YAML file in the folder .github/workflows/, made of key: value lines nested by their indentation. The key on says which event starts it, and each job runs its steps from top to bottom on a fresh machine, the runner.

Here is a small workflow of another kind. It starts on every pull request and runs one command:

name: Hello
on: pull_request
jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - run: echo Hello from the runner

A step either uses a ready-made action, such as actions/checkout, which copies your repository onto the runner, or runs a command, as you would type it in a terminal. The runner starts with its own Java 17 and nothing of yours, while your pom.xml asks for Java 21. So the order matters: copy the code, set up Java 21 with actions/setup-java, download the packages pom.xml pins with mvn dependency:go-offline, then run mvn test. The job stops at the first step that fails.

You write the workflow as a Java array of strings named workflow, one string per line. The check reads it the way GitHub would, runs the job and must print:

Workflow: Tests
Starts on: push
Job test, on a fresh ubuntu-latest machine
Step 1: uses actions/checkout@v6
  copied the repository: pom.xml, src/main/java/shop/ShopApplication.java, src/test/java/shop/ShopApplicationTests.java
Step 2: uses actions/setup-java@v5 with java-version 21
  Java 21 (temurin) is ready
Step 3: run mvn dependency:go-offline
  downloaded Spring Boot 3.3.5 and sqlite-jdbc 3.46.1.3, the versions pom.xml pins, into ~/.m2
Step 4: run mvn test
  Tests run: 3, Failures: 0, Errors: 0, Skipped: 0: BUILD SUCCESS
The job passed: every push now runs the tests on Java 21.

Your task: the starting workflow already has its name and the job test on ubuntu-latest. Add the trigger on: push and four steps: check out the code with actions/checkout@v6, set up Java '21' from the temurin distribution with actions/setup-java@v5, download the packages with mvn dependency:go-offline, and run the tests with mvn test.


Assignment
Follow the Coding Tutorial to write a GitHub Actions workflow that runs the shop's tests on Java 21 on every push.


Hint
Indentation is the structure, and it must be spaces: the step items start 6 spaces in with a dash, with: sits 8 spaces in under its uses, and distribution and java-version 10. Write the version in quotes, '21', so YAML keeps it as text.


STEP 1 OF 8
    Ctrl + Enter to check