How to set up Gitlab CI/CD pipeline + Gitlab Runner

Photo by Pankaj Patel on Unsplash

Here I will be explaining steps to set up your own Gitlab Runner on your on-premise Gitlab. For demo I have created one docker based application which I will build and deployed using Gitlab CI/CD pipeline.

Pre-requisite

  • Basic knowledge of Docker , Docker-compose and maven project
  • Should know about Gitlab CI/CD and Gitlab Runner’s purpose.

Refer sample code from https://gitlab.com/ritresh.girdhar/docker-image-test

Let’s understand gitlab pipeline — .gitlab-ci.yml

image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
SPRING_PROFILES_ACTIVE: gitlab-ci
stages:
- build-microservice1
- build-microservice2
- package-microservice1
- package-microservice2

build-microservice1:
image: maven:3-jdk-8
stage: build-microservice1
script: "mvn clean install -f microservice1/pom.xml"
artifacts:
paths:
- microservice1/target/*.jar
build-microservice2:
image: maven:3-jdk-8
stage: build-microservice2
script: "mvn clean install -f microservice2/pom.xml"
artifacts:
paths:
- microservice2/target/*.jar

package-microservice1:
stage: package-microservice1
script:
- docker build -t ${CI_REGISTRY}/${CI_PROJECT_PATH}/gitlab/microservice1:latest microservice1/
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/gitlab/microservice1:latest
package-microservice2:
stage: package-microservice2
script:
- docker build -t ${CI_REGISTRY}/${CI_PROJECT_PATH}/gitlab/microservice2:latest microservice2/
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}/gitlab/microservice2:latest

In the above pipeline, You could see, we have two defined micro services i.e package-microservice1 and package-microservice2.

Firstly, Pipeline will build both the applications and then create a docker artifact of the both microservices and then it will push the images to the registry.

Lets run the pipeline

Due of Continuous Integration, as soon as you commit any change you would see the pipelines and the defined stages with status in pipelines section.

In case, build get failed due to any reason then user will receive notification they have opted (or admin defined) like here I am receiving an email from Cloud Gitlab runner.

Lets Check the Package/Docker Image

You could check the images under Packages -> Container Registry. You will find here two docker images as described in above Gitlab Pipeline.

How to run CI/CD pipeline on On-Premise GitLab

No worries , there is concept of Gitlab Runner . To set up Gitlab Runner On-premise Gitlab. Refer https://docs.gitlab.com/runner/

***Note: While setting gitlab runner you could face some issues that Gitlab Runner running fine first time but for second time it showing below error.

Running with gitlab-runner 12.10.2 (c5874a4b)
on <host> qz2DQqzP

Preparing the “shell” executor
Using Shell executor…

Preparing environment
Running on <host>…

Getting source from Git repository
Fetching changes with git depth set to 50…
Reinitialized existing Git repository in /home/gitlab-runner/builds/qz2DQqzP/0/<git_project_group_name_repo_name>/.git/
fatal: git fetch-pack: expected shallow list

Uploading artifacts for failed job

I fixed this error by upgrading my VM git version. Try it out, you would not face this issue. To install or upgrade Git refer this git on fedora/centos

Father || Coder || Engineer || Learner || Reader || Writer