Friday, January 07, 2022

Project deployment in Local Tomcat using GitLab Runner

 Please refer to the previous article Gitlab Tutorial Part-1 to understand the use and benefit of GitLab.


http://siddharathadhumale.blogspot.com/2022/01/gitlab-tutorial-part-1.html


In this below article we will try to do following things.

1- As we are using window machine we will install gitlab-runner for windows.
2- We will register our gitlab-runner to our gitlab site.
3- we will create a job inside a .gitlab-ci file to build and Junit test our maven project and finally deploy the same in our local tomcat using our runner.

Step 1: Install GitLab Runner

Lets first install gitlab runner in windows. Please follow the below steps religiously.

Refer to the belwo url for more information https://docs.gitlab.com/runner/
You can download windows version from belwo site
https://docs.gitlab.com/runner/install/index.html
https://docs.gitlab.com/runner/install/windows.html

Now as stated above lets create folder C:\GitLab-Runner
now copy this file gitlab-runner-windows-amd64.exe inside our C:\GitLab-Runner

rename it to gitlab-runner.exe

Now execute the below command and install the gitlab runner

gitlab-runner.exe install

1
2
PS C:\GitLab-Runner> .\gitlab-runner.exe install
Runtime platform                                    arch=amd64 os=windows pid=11156 revision=5316d4ac version=14.6.0

Now lets check gitlab-runner is installed properly using below command.

1
2
3
4
5
6
7
8
9
10
11
gitlab-runner --version
 
 
PS C:\GitLab-Runner> .\gitlab-runner --version
Version:      14.6.0
Git revision: 5316d4ac
Git branch:   14-6-stable
GO version:   go1.13.8
Built:        2021-12-17T17:35:49+0000
OS/Arch:      windows/amd64
PS C:\GitLab-Runner>

Note:- if you want to uninstall gitlab-runner use belwo command.

PS C:\GitLab-Runner> .\gitlab-runner.exe uninstall
Runtime platform arch=amd64 os=windows pid=1744 revision=5316d4ac version=14.6.0

Now lets do the step2 of registering the runner

Step 2: Register GitLab Runner
This step is required to bind runner with gitlab instance.we are using cloud instance for our Gitlab.

Please refer to the below url for more information.

https://docs.gitlab.com/runner/register/index.html

gitlab-runner.exe register

You will be asked for certain question please fill the data as shown in the belwo figure.

1
2
3
4
5
PS C:\GitLab-Runner> .\gitlab-runner.exe register
Runtime platform arch=amd64 os=windows pid=4268 revision=5316d4ac version=14.6.0
Enter the GitLab instance URL (for example, https://gitlab.com/):
Enter the registration token:

When it ask for token take it from your gitlab site as follow belows

now add the description for the token

1
2
3
4
5
6
7
8
PS C:\GitLab-Runner> .\gitlab-runner.exe register
Runtime platform                                    arch=amd64 os=windows pid=4268 revision=5316d4ac version=14.6.0
Enter the GitLab instance URL (for example, https://gitlab.com/):
Enter the registration token:
CqKa7ozrNmht2feecgsJ
Enter a description for the runner:
[DESKTOP-7TJ9MSN]: siddhu-runner
1
2
3
4
5
6
7
8
9
10
11
12
PS C:\GitLab-Runner> .\gitlab-runner.exe register
Runtime platform                                    arch=amd64 os=windows pid=4268 revision=5316d4ac version=14.6.0
Enter the GitLab instance URL (for example, https://gitlab.com/):
Enter the registration token:
CqKa7ozrNmht2feecgsJ
Enter a description for the runner:
[DESKTOP-7TJ9MSN]: siddhu-runner
Enter tags for the runner (comma-separated):
ssh,shell,ci
Registering runner... succeeded                     runner=CqKa7ozr
Enter an executor: virtualbox, docker+machine, custom, docker, docker-ssh, parallels, shell, ssh, kubernetes, docker-windows, docker-ssh+machine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PS C:\GitLab-Runner> .\gitlab-runner.exe register
Runtime platform                                    arch=amd64 os=windows pid=4268 revision=5316d4ac version=14.6.0
Enter the GitLab instance URL (for example, https://gitlab.com/):
Enter the registration token:
CqKa7ozrNmht2feecgsJ
Enter a description for the runner:
[DESKTOP-7TJ9MSN]: siddhu-runner
Enter tags for the runner (comma-separated):
ssh,shell,ci
Registering runner... succeeded                     runner=CqKa7ozr
Enter an executor: virtualbox, docker+machine, custom, docker, docker-ssh, parallels, shell, ssh, kubernetes, docker-windows, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
PS C:\GitLab-Runner>

Now once this is done we wil check our 3rd step

Step 3: Start GitLab Runner
use belwo command

.\gitlab-runner.exe start

1
2
3
PS C:\GitLab-Runner> .\gitlab-runner.exe start
Runtime platform                                    arch=amd64 os=windows pid=6392 revision=5316d4ac version=14.6.0
PS C:\GitLab-Runner>

Now check the runner is started in the project from the ui.

now lets create one simple job in our .gitlab-ci file to check if the our runner is working properly or not.

Add following lines in our .gitlab-ci files as shown below

1
2
3
4
5
6
demo_job_1:
  tags:
   - ssh #this is the same tag we had used while creating the runner Enter tags for the runner (comma-separated):ssh,shell,ci
  script:
   - echo Hello World  
   

As soon as we commit the code our .gitlab-ci CI/CD pipeline start

You can see our build fails at the our demo_job_1 and stop their only i.e. no further job are run in the flows.

to solve this issues refer to the below site

https://stackoverflow.com/questions/68109273/exec-pwsh-executable-file-not-found-in-path

When choosing the shell option, the gitlab runner installer uses pwsh as the executor. It generates a config.toml that looks like

1
2
3
4
5
6
[[runners]]
  name = "some name"
  token = "some-token"
  executor = "shell"
  shell = "pwsh"

The problem is that pwsh isn’t a valid windows command (on my installs). Changing pwsh to powershell and restarting gitlab-runner service fixed the problem for me.

1- config.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
concurrent = 1
check_interval = 0
 
[session_server]
  session_timeout = 1800
 
[[runners]]
  name = "siddhu-runner"
  token = "5GoezppvccNtJjekSsP6"
  executor = "shell"
  shell = "powershell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

Now stop and start the runner again

1
2
3
4
5
PS C:\GitLab-Runner> .\gitlab-runner.exe stop
Runtime platform                                    arch=amd64 os=windows pid=11624 revision=5316d4ac version=14.6.0
PS C:\GitLab-Runner> .\gitlab-runner.exe start
Runtime platform                                    arch=amd64 os=windows pid=11352 revision=5316d4ac version=14.6.0
PS C:\GitLab-Runner>

Once again update any file of the project and do checkin the file to gitlab and one more time our build will be started.

You will be able to see our job for runner is executed properly this time

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
34
35
36
37
38
39
40
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 "      
     
demo_job_1:
  stage: test
  tags:
   - ssh #this is the same tag we had used while creating the runner Enter tags for the runner (comma-separated):ssh,shell,ci
  script:
   - echo Hello World changed.

Now lets try to modify our runner to execute the build and copy the created jar file on perticular local machine using gitlab url.

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
34
35
36
37
38
39
40
41
42
43
stages:
  - build
  - test
  - deploy
 
#image: maven:latest
build-code-job:
  stage: build
  tags:
   - ssh #this is the same tag we had used while creating the runner Enter tags for the runner (comma-separated):ssh,shell,ci
  script:
    - echo "Building Script execution"   
    - "C:/apache-maven-3.6.3/bin/mvn clean install -B"
    - "sleep 60"
    - "cp C:/GitLab-Runner/builds/5Goezppv/0/siddhugitlabgroup/siddhusimplehelloworldspringboot/target/SiddhuSimpleHelloWorldSpringboot-0.0.1-SNAPSHOT.jar C:/to_delete/copy_war_files/SiddhuSimpleHelloWorldSpringboot-0.0.1-SNAPSHOT.jar"
 
 
#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 "      
#   
#demo_job_1:
#  stage: test
#  tags:
#   - ssh #this is the same tag we had used while creating the runner Enter tags for the runner (comma-separated):ssh,shell,ci
#  script:
#   - echo Hello World changed.

You will able to see that our .jar created files is copied at our desired folder.

No comments: