How to test and push to the registry a Spring Boot app on Gitlab CI

# .gitlab-ci.yml
stages:
- test
- build
- deploy

cache:
key: "java-repo"
paths:
- .m2/repository

include:
- api/.gitlab-ci.yml

Project specific .gitlab.ci.yaml to test the Spring boot app and push images to the registry explained

  • Variables: Gitlab CI provides variables with everything you might need for the build. In this case I’m creating CONTAINER_TEST_IMAGE (the image name) composing the registry image and tag it with prefix api-and the commit hash (so I have a new tag at every commit). I also create CONTAINER_RELEASE_IMAGE similarly, but with api-latest as tag.
  • mvn-test-api is just a name I chose for a job that will run at the teststage. This stage will be executed as I’ve specified so in the stages:in the root .gitlab-ci-.yaml file. What it does should be clear from the script section: dependency resolve (suggested to run by gitlab docs) and the maven verify . Note that I’m running tests using testcontainers, so I need to also add the docker-in-docker service running , disable TLS and specify the host (I had some errors without it)
  • build-tag-and-push-image acts at the next build stage, and what is doing is logging to the gitlab docker registry using the credentials (all given as a variables) and then build, tag and push the image with the name chosen earlier and the two tags (CONTAINER_TEST_IMAGE and CONTAINER_RELEASE_IMAGE). It’s a good practice to have the latest tag for release, but also all of them in case a rollback is needed.
---
variables:
MAVEN_OPTS: "-Dmaven.repo.local=maven.repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:api-$CI_COMMIT_SHORT_SHA
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:api-latest


mvn-test-api:
stage: test
# when: manual # enable this if you want to temporarily skip it
image: eclipse-temurin:17
script: |
cd api
pwd
./mvnw clean dependency:resolve -B --no-transfer-progress -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
./mvnw -B --no-transfer-progress verify -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
cd -
services:
- name: 'docker:20.10.12-dind' # testcontainers
command: [ '--tls=false' ]
#
artifacts:
# expire_in: 1 hour
variables:
DOCKER_TLS_CERTDIR: ''
DOCKER_HOST: tcp://docker:2375


build-tag-and-push-image:
stage: build
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
image: 'docker:20.10.12'
services:
- name: 'docker:20.10.12-dind'
script: |
docker build --pull -t $CONTAINER_TEST_IMAGE flow-api
docker push $CONTAINER_TEST_IMAGE
docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
docker push $CONTAINER_RELEASE_IMAGE

--

--

--

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Linux Automate System task with cron

2021 — Rockstar Data Engineer Roadmap

My First Spring+Docker+Kubernetes project

What Happens When You Type ‘gcc main.c’?

Spring Boot Profiles

Starting the Journey as a Software Developer: An Interview with untapt’s Jeff Fenster.

If you can’t beat the market at least you can protect from it using Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elvis Ciotti

Elvis Ciotti

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

More from Medium

Authorisation with Spring Security — Part 1

REST API using Quarkus and Panache

Quarkus REST API

Experience with Spring batch and docker

Dynamically route to read-only and read-write database instances in spring-boot