Monday, February 14, 2022

Configure and Register Local Runner On Ubuntu VM to connect to GITLAB


SUMMARY:
A) INSTALL LOCAL GITLAB RUNNER ON UBUNTU
B) EXAMPLES OF .GITLAB-CI.YML

Local Gitlab Runner:- If you want to run CICD pipeline from your local shell then you should use local runner and make it run on your local machine. You can refer to the GitLab repo and execute the pipeline from your local runner. Here it is not required to validate the account because you are using your local machine for running CICD pipeline.

Once Local runner is installed then you need to register it on local machine and need to provide some details like GitLab URL, token and tags to connect to a particular project.

Install local GitLab runner on Ubuntu 18.04 

Step1: Refer below link for latest way to download and install GitLab runner on your local machine

https://docs.gitlab.com/runner/install/linux-repository.html

Step2:-

          curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

Step3:- Install GitLab runner

          sudo apt install docker.io -y

          sudo apt-get install gitlab-runner

Step4:- Verify GitLab Runner is installed

        gitlab-runner -v

Step5:- Register gitlab runner to local machine

        sudo gitlab-runner register

Enter the GitLab instance URL (for example, https://gitlab.com/):

https://gitlab.com/

Enter the registration token:

Token from your gitlab runner (CICD --> Runner)

Enter a description for the runner:

localmachine runner

Enter tags for the runner (comma-separated):

localrunner,localshell

                Registering runner... succeeded                     runner=1T_UqosD

Enter an executor: ssh, virtualbox, docker, docker-ssh, parallels, docker-ssh+machine, kubernetes, custom, shell, docker+machine:

shell


Step6:- To verify that local runner is  registered successfully, you need to go to GitLab Project ---> Select Settings---> CICD---> Runner.

When you expand Runner section you will find under "Available Specific Runner" section.

Step7:- Let's update .gitlab-ci.yml file in GITLAB to check that CICD pipeline is running or not by using the local runner on the local machine.

job-to-run:
  tags:
    - localrunner  
    - localshell
  script:
    echo "Hello world"

Step 8:- Once you execute above script then you will find in the log that this pipeline was executed on the local machine.



alternatively, you can type git log --oneline on CLI



Step 9:- When you install GitLab runner, automatically a 'user' will get created for the GitLab-runner and the user should have sudo permission to run the pipeline.

             vi /etc/sudoers

 and add below entry after root user info and save your file

          gitlab-runner ALL=(ALL) NOPASSWD: ALL


.gitlab-ci.yml Examples:

Example1:

stages:
  - build
  - test
  - deploy
build:
    stage: build
    tags:
        - localrunner  
        - localshell
    script: 
        - echo "Build Stage"
test:
    stage: test
    tags:
        - localrunner  
        - localshell
    script:
        - echo "Test Stage"        
deploy:
    stage: deploy
    tags:
        - localrunner  
        - localshell
    script:
        - echo "Deploy Stage"  

Example2: CICD pipeline with maven junit test

stages:
  - build
  - test
  - deploy

build:
    stage: build
    tags:
        - localrunner  
        - localshell
    script: 
        - cd /home/vagrant/mvnrepo
        - sudo mvn clean package

maven-build:
    stage: build
    tags:
        - localrunner  
        - localshell
    script: 
        - cd /home/vagrant/mvnrepo
        - sudo mvn clean package
    artifacts:
     paths:
      - target/surefire-reports/TEST-*.xml
     reports:
      junit:
        - target/surefire-reports/TEST-*.xml
test:
    stage: test
    tags:
        - localrunner  
        - localshell
    script:
        - sudo ls /home/vagrant/mvnrepo/target/*.jar    
newtest:
    stage: test
    tags:
        - localrunner  
        - localshell
    script:
        - sudo touch /home/vagrant/mvnrepo/passtest    
deploy:
    stage: deploy
    tags:
        - localrunner  
        - localshell
    script:
        - cd /home/vagrant/mvnrepo
        - sudo java -cp target/myproj-1.0-SNAPSHOT.jar com.raman.App


No comments:

Post a Comment

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...