Commit 3292996f authored by Perwaiz Ali's avatar Perwaiz Ali

Update .gitlab-ci.yml

parent ed43cc73
Pipeline #2176 failed
Skip to content
DEV Community
Search...
Log in
Create account
6
Like
2
Unicorn
10
Save
sanjay shivanna
sanjay shivanna
Posted on 12 Feb 2021 • Updated on 10 Apr 2021
Spring boot +Docker+ Gitlab - Continuous Integration(CI)
#
java
#
git
#
spring
#
devops
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"]
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: stages:
- test
- build - build
- assemble - test
default: - docker
image: adoptopenjdk:14.0.2_12-jdk-hotspot-bionic
before_script: maven-build:
- echo `pwd` # debug image: maven:3-jdk-8
- export GRADLE_USER_HOME=`pwd`/.gradle stage: build
- export JIB_CACHE_HOME=`pwd`/.jib script: "mvn clean package -B"
# installs git artifacts:
# - apt update && apt install -y git
# - git status
cache:
paths: paths:
- .gradle/caches - target/*.jar
- .gradle/wrapper
- .jib 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
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)
Subscribe
pic
Add to the discussion
Code of Conduct • Report abuse
Read next
benjyw profile image
Better CI/CD caching with new-gen build systems
Benjy Weinberger - Jul 26
rcls profile image
You think you’re working in an agile way? Probably not.
Ossi P. - Aug 2
ankit01oss profile image
Kubernetes Audit Logs - Best Practices And Configuration ✨
Ankit Anand ✨ - Aug 5
dylanmorley profile image
Azure Service Bus - Replay Messages CLI
Dylan Morley - Aug 5
sanjay shivanna
Follow
Keep learning
LOCATION
Brussels
JOINED
28 Jun 2020
More from sanjay shivanna
Springboot+Docker+Minikube+Kubectl - Continuous Integration(CI) Local
#java #springboot #minikube #k8s
Springboot Project Structure
#java #springboot #maven
Timeouts and how to handline in Java
#java #spring
image: gitlab/dind
services:
- docker:dind
variables: variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
APP_VERSION: 1.0.0
DOCKER_DRIVER: overlay DOCKER_DRIVER: overlay
PROJECT_NAME: "SPRING_BOOT_APP" SPRING_PROFILES_ACTIVE: gitlab-ci
IMAGE_NAME: "app-svc" USER_GITLAB: sanjaybsm
APP_NAME: springbootgitlab
REPO: springbootlabtest
test: stages:
stage: test - build
script:
- ./gradlew -S -g /cache/.gradle -Dorg.gradle.java.home=${JAVA_HOME} clean test
only:
- merge_requests
tags:
- test - test
except: - docker
- master
- release-app-1.0.0
build: maven-build:
image: maven:3-jdk-8
stage: build stage: build
script: script: "mvn clean package -B"
- ./gradlew printVersion build artifacts:
tags: paths:
- build - target/*.jar
maven-test:
image: maven:3-jdk-8
stage: test
script: "mvn test"
artifacts: artifacts:
paths: paths:
- build/libs/*.jar - target/*.jar
- build/reports/tests
- build/reports/jacoco docker-build:
- build/docs/asciidoc stage: docker
- build/version.id
expire_in: 5 day
docker_publish_app:
stage: assemble
when: manual
tags:
- assemble
image: docker:latest
before_script:
- apk add --update --no-cache curl jq py3-configobj py3-pip py3-setuptools python3 python3-dev
- docker login -u "$CONTAINER_REGISTRY_USER" -p "$CONTAINER_REGISTRY_PW" $CONTAINER_REGISTRY_NAME
services:
#- docker:dind
- name: docker:dind
script: script:
- cd /builds/Application-Repo/spring-boot-app - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker --version - docker build -t registry.gitlab.com/yournameingitlab/springbootlabtest .
- docker build . -t "mousumi08/mourepo:${APP_VERSION}.${CI_BUILD_ID}" -f Dockerfile - docker push registry.gitlab.com/yournameingitlab/springbootlabtest
- docker tag mousumi08/mourepo:${APP_VERSION}.${CI_BUILD_ID} mousumi08/mourepo:${APP_VERSION}.latest DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey.
- docker tag mousumi08/mourepo:${APP_VERSION}.${CI_BUILD_ID} mousumi08/mourepo:${APP_VERSION}.${CI_BUILD_ID}
after_script: Built on Forem — the open source software that powers DEV and other inclusive communities.
- echo "End CI"
\ No newline at end of file Made with love and Ruby on Rails. DEV Community © 2016 - 2022.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment