DEV Community

loading...

Spring boot +Docker+ Gitlab - Continuous Integration(CI)

sanjaybsm profile image sanjay shivanna Updated on ・2 min read

A simple example to demonstrate, how we can use spring boot, docker and gitlab for build and test using maven, create an image and push it to gitlab repository.

Step 1 - Create a Gitlab account
If you already familiar with gitlab then feel free to skip this section, otherwise create a gitlab account by following this link Link

Step 2 - Create a New Project in Gitlab
Once you successfully created Gitlab account then create a new project

Alt Text

Step 3 - Create a New Spring boot project
Simple and easiest way to create spring boot project is to visit start.spring.io

Alt Text

Once you have the project created, use the choice of your editor to open the project.

Step 4 - Add Dockerfile and Update gitlab.ci.yml file
Docker file

FROM openjdk:13-alpine
Volume /tmp
ADD /target/*.jar springbootgitlab-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/springbootgitlab-0.0.1-SNAPSHOT.jar"]
Enter fullscreen mode Exit fullscreen mode

gitlab-ci yml file

image: gitlab/dind
services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay
  SPRING_PROFILES_ACTIVE: gitlab-ci
  USER_GITLAB: sanjaybsm
  APP_NAME: springbootgitlab
  REPO: springbootlabtest

stages:
  - build
  - test
  - docker

maven-build:
  image: maven:3-jdk-8
  stage: build
  script: "mvn clean package -B"
  artifacts:
    paths:
      - target/*.jar

maven-test:
  image: maven:3-jdk-8
  stage: test
  script: "mvn test"
  artifacts:
    paths:
      - target/*.jar

docker-build:
  stage: docker
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build -t registry.gitlab.com/yournameingitlab/springbootlabtest .
    - docker push registry.gitlab.com/yournameingitlab/springbootlabtest
Enter fullscreen mode Exit fullscreen mode

please note test mvn test is added just as an example

For above project I used packaging as jar since my dockerfile and gitlab yml files configured like that.

Step 5 - Commit and Push to gitlab!
git add .
git commit -m "initial commit"
git push origin master

After above step, you should see your gitlab pipeline kicking in!!
Alt Text

and also you should see your image available in container registry!
Alt Text

Voila!! you have successfully used gitlab ci/cd pipeline to build, test, create image and push image to gitlab container registry!

In the next blog I will try to explain how we can download docker image from gitlab and run it from local machine!

Discussion (0)

pic
Editor guide