A simple gitlab-ci pipeline, for a java project built with gradle, dockerized and pushed to a Google Container Registry

Share on:

I have been working on a small personal project during these days (a silly slack bot). Usually I try to mix and match technologies that I am using at work, with stuff that I really enjoy, and I have a very strong opinion. So I get to improve on stuff that I don't necessarily enjoy using day to day :P with stuff that I really like.

In this particular small post I want just to share a dead simple GitlabCI pipeline for a small service based on Java, built with gradle, dockerized and then pushed the image to a private registry on Google. You may ask why GCR, it's part of my small project.

So we have

  • Java web api service, based on JDK 11
  • Built with Gradle 6.3
  • Has to be Dockerized and pushed to a private registry on Google Cloud.
  • It is hosted on gitlab, and I am using Gitlab-CI (free).

Project Structure

You can assume the following standard structure. Java sources, gradle build script, a Dockerfile on the root of the folder + gitlab-ci.yml definition. I am not going to elaborate on the build script, code etc.

Pre-req

  • You need a Google Cloud account, and a new project configured (in this example the name of the project is called javapapo)
  • You need to enable the Docker Registry on Google Cloud for your project ( Enable the Container Registry API.) see here.
  • In order to be able to push to your private docker registry from a service like Gitlab, you need to a way to authenticate. There several options out there, but it seems that most of the people do prefer to create a specific _SERVICE_ACCOUNT', and then get a JSON token. See here

Pipeline

The source of the pipeline can be found on this Gitlab Snippet (it's public. I have added inline comments. Adding the service account JSON as a Base64 variable on the CI/CD settings of the project

References: