Thursday, January 06, 2022

Gitlab Tutorial Part-1

 First create your account in gitlab site i.e. https://gitlab.com/

Login to your account and create group and project. It is best practise to create a group before creating a specific project as creating group help us in assigning the user role.

Now Lets create a simple spring boot project in STS and upload the code in our this project.
Now create a file called as .gitlab-ci.yaml and add it into the root folder of the project as shown below.

Add following lines in our .gitlab-ci.yaml

1- .gitlab-ci.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
stages:
  - build
  - test
  - deploy
 
build-code-job:
  stage: build
  script:
    - echo "Building Script execution"
     
test-code-job1:
  stage: test
  script:
    - echo "Testing Script-1 execution"   
 
test-code-job2:
  stage: test
  script:
    - echo "Testing Scrip-2  execution "   
     
deploy-code-job1:
  stage: deploy
  script:
    - echo "Deploy Script-1 execution"   
 
deploy-code-job2:
  stage: deploy
  script:
    - echo "Deploy Scrip-2  execution " 

Now as soon as you uplaod this file on the gitlab repository or change any file in sts and upload it on gitlab this .gitlab-ci file will be executed and we will be able to see the job running as shown in below screen.

Click on any job and see the details

Note:- Make sure that all the jobs in one stage are run parallel i.e. in our above .gitlab-ci files we have two job for test and deploy. So in this case both the jobs of test i.e. test-code-job1 and test-code-job2 and jobs of deploy i.e. deploy-code-job1 and deploy-code-job2 run parallel in their stages.

now lets try to run the maven build of our project on gitlab. For that add following thing in our .gitlab-ci files.

build:
script: “mvn install -B”

So our new .gitlab-ci.yaml will be

1- .gitlab-ci.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
stages:
  - build
  - test
  - deploy
 
image: maven:3-jdk-11
 
build-code-job:
  stage: build
  script:
    - echo "Building Script execution"
    - "mvn install -B"
     
test-code-job1:
  stage: test
  script:
    - echo "Testing Script-1 execution"   
 
test-code-job2:
  stage: test
  script:
    - echo "Testing Scrip-2  execution "   
     
deploy-code-job1:
  stage: deploy
  script:
    - echo "Deploy Script-1 execution"   
 
deploy-code-job2:
  stage: deploy
  script:
    - echo "Deploy Scrip-2  execution " 

upload the file on the gitlab and you will see an new build is fired and our mvn install command is run successfully.

Now lets try to run one of the test job using belwo command

“mvn clean test”

So our new .gitlab-ci.yaml will be

1- .gitlab-ci.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
stages:
  - build
  - test
  - deploy
 
image: maven:3-jdk-11
 
build-code-job:
  stage: build
  script:
    - echo "Building Script execution"
    - "mvn install -B"
     
test-code-job1:
  stage: test
  script:
    - echo "Testing Script-1 execution"  
    - "mvn clean test"
 
test-code-job2:
  stage: test
  script:
    - echo "Testing Scrip-2  execution "   
     
deploy-code-job1:
  stage: deploy
  script:
    - echo "Deploy Script-1 execution"   
 
deploy-code-job2:
  stage: deploy
  script:
    - echo "Deploy Scrip-2  execution "    

Analytics left menu will give you graphical represention of CI/CD, Repository and Value stream as shown below

There is also an option available on gitlab ui to directly update the code of .gitlab-ci files

In next article we will try to learn how to create an runner which is application that use the .gitlab-ci files and perform operation that is required for CI-CD pipeline. We are going to deploy our war file created above to install in our local tomcat in windows machine.

No comments: