Gitlab CI Yml Example Docker

Gitlab ci yml Example Docker:

In gitlab, CI CD pipelines are configured with Gitlab ci yml file. this file will be located at your project root directory. whenever developer push the code into gitlab, Gitlab will trigger the ci yml file and it will execute the all the jobs that we defined in the gitlab ci yml. Gitlab ci yml is mainly depends on docker images. Using these docker images only all the jobs will be executed. In the following steps i will explain about gitlab ci yml example with docker.

Gitlab ci yml Example:

image: maven:latest

variables:
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

stages:
  - build
  - testing
  - code-coverage

compile:
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS compile

test:
  stage: testing
  script:
    - mvn $MAVEN_CLI_OPTS test

sonar:
  stage: code-coverage
  script:
    - mvn $MAVEN_CLI_OPTS sonar:sonar
  only:
    - master

in this gitlab ci yml example file we are having three stages and each stage has one job. stage can have multiple jobs but here i defined only one job for each stage.

Gitlab ci yml Example Docker:

understanding docker image in gitlab ci yml :

in the gitlab ci yml example file you can see in the first line we mentioned docker image maven:latest. Whenever the developer push the code into gitlab, it will trigger the pipeline and execute all the jobs. But these all jobs will be executed in docker containers in git lab runner. For that you need to configure gitlab runner. Gitlab runner is like jenkins slave machine, where the jobs will be executed. The first step that will be executed on the gitlab runner is creating docker container. Whatever docker image you mentioned in the gitlab ci yml file, Gitlab runner will create that the docker container with that image and Gitlab will send all your source code in to that container. After sending source code into the docker container the commands or scripts will be executed in the containers. After executing scripts in the containers gitlab runner will send all those logs to gitlab server.

In the above gitlab ci yml example file i mentioned docker maven image, So gitlab runner will download the docker image from docker hub. And it will create maven container. And git lab will send all your source code into this container.

Understanding stages and jobs in Gitlab CI Yml :

In git lab ci yml, Stage represents a collection of jobs. Those jobs will be executed under that particular stage. Job represents a linux command, or a maven command, or a shell script, or list of scripts.

gitlab ci yml example Docker

In this image you can see 4 stages and 6 jobs. Build stage is having one job called build job. Test stage is having 2 jobs test1 and test2. Staging stage is having one job called auto-deploy-manage. Production stage is having 1 job called deploy to production job.

In above gitlab ci yml example file you can observe there we have defined three stages and three jobs. Build stage is having compile job. Testing stage is having test job. Code-coverage stage is having sonar job.

In compile job we are executing the maven compile commands. To execute any commands in gitlab ci yml we will mention that commands in the script tags. in test job we are executing maven test commands. in sonar job we are executing maven sonar analysis commands.

  • gitlab ci yml example
  • ci yml file gitlab
  • yml file gitlab example

Leave a Reply

Your email address will not be published. Required fields are marked *