0% found this document useful (0 votes)
22 views

Automate Your CI - CD Pipeline With GitLab Runner (SSH) On Ubuntu

Uploaded by

v711
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Automate Your CI - CD Pipeline With GitLab Runner (SSH) On Ubuntu

Uploaded by

v711
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Automate Your CI/CD Pipeline with GitLab

Runner(SSH) on Ubuntu
Buildbot Technologies Private Limited + Follow

We build your idea, We operate your product and…


Published Feb 29, 2024

Introduction:

GitLab Runner is an open-source tool that allows you to run your GitLab CI/CD jobs. It
can be configured to run on various platforms, such as Linux, macOS, and Windows,
and it supports different executors like Docker, Kubernetes, and Shell.

To register a GitLab Runner, you can use the GitLab-runner register command, which
will
4
prompt you for the necessary information, such as the GitLab instance URL, a
registration token, and a description. You can also specify these values as command-
line arguments orLike
environment variables. Comment Share
Once registered, you can manage your GitLab Runners using various commands, such
as GitLab-runner list to list all registered runners, Gitlab-runner unregisters to
unregister a specific runner, and Gitlab-runner reset-token to reset a runner's token.

To run a job using GitLab Runner, you can use the GitLab-Runner run command, which
will start the GitLab Runner service and execute all registered runners. Alternatively,
you can use the GitLab-runner run-single command to run a single job using a specific
runner.

GitLab Runner also supports debug mode, which can be useful when troubleshooting
issues. To run a command in debug mode, you can use the --debug flag.

Finally, GitLab Runner can be configured to use different executors, such as Docker or
Shell, and you can specify the Docker image to use for the Docker executor. Here is an
example of running a job using the Docker executor with the GitLab-runner run-single
command

Image reference from Google

Prerequisites:

1. Gitlab account with the project and a repository

2.4 An Ubuntu instance with git installed

Steps install setup:Like Comment


Step 1: Update the Ubuntu

Command: apt update

Step 2. Install the curl

Command: apt install curl -y

Step 3 Install GitLab-runner using binary installation


Join now Sign in
We are installing the GitLab-runner using

https://docs.gitlab.com/runner/install/linux-manually.html#using-binary-file

Below
4 are instructions to install Gitlab-runner

1. Simply download
Likeone of the binaries for your system:
Comment
Command: sudo curl -L --output /usr/local/bin/gitlab-runner "https://

1. Permission to execute:

Command: sudo chmod +x /usr/local/bin/gitlab-runner

1. Create a GitLab CI user:

Command: sudo useradd --comment 'GitLab Runner' --create-home gitlab-

1. Install and run as a service:

Recommended by LinkedIn

Git, Github, and Git Commands.


Sunil Kumar Cheruku · 4 years ago

Like Comment
Automating Application Start on Ubuntu with systemd
Praveen Kumar · 6 months ago

linux for devOps part-3


Sahil Bhoyar · 5 months ago

Command: sudo gitlab-runner install --user=gitlab-runner --working-di

Command:sudo gitlab-runner start

Step 4. Copying ssh keys

1. Generate ssh-keys using root and copy the id_rsa.pub key to the GitLab account
4

Step 5. Clone the code in the GitLab project locally using SSH
Like Comment
We need to clone the repository using SSH

Example: git clone [email protected]:abc/ssh-test.git

Note: we are cloning the repo using SSH, not https

Step 6. We need to configure the GitLab-runner

Navigate to Repo>Project>Settings>CI/CD>Runner

We are choosing the operating system Linux we need to specify the tag and then
create the runner

Register runner:

GitLab Runner must be installed before you can register a runner.

Step 1 Copy and paste the following command into your command line to register the runner.

4
$ gitlab-runner register
--url https://gitlab.com
Like Comment
--token glrt-CWQLfY4a4YeBofk-uoci

The runner authentication token glrt-CWQLfY4a4YeBofk-uoci displays here for a short


time only. After you register the runner, this token is stored in the config.toml and
cannot be reaccessed from the UI.

Step 2: Choose an executor when prompted by the command line. Executors run builds in
different environments. Which one to select?
Step 3 (optional)

Manually verify that the runner is available to pick up jobs.

$ gitlab-runner run

we can use other options like start, stop, restart, status

After creating the runner we can verify the runner status is online

Like Comment
Note: We are using ssh instead of https so we need to configure env variables in the
/home/gitlab-runner/.gitlab-runner/config.toml

Env variables:

Note: delete the .bash_logut file in /home/gitlab-runner

concurrent = 1

check_interval = 0

connection_max_age = "15m0s"

shutdown_timeout = 0

[session_server]

session_timeout = 1800

[[runners]]

name = "ssh"

url = "https://gitlab.com"
4
id = 33217256
Like Comment
token = "glrt-QpdzCxasfbyxpq3Vnsc2"
token_obtained_at = 2024-02-28T11:37:24Z

token_expires_at = 0001-01-01T00:00:00Z

executor = "ssh"

environment = ["GIT_STRATEGY=none"]

pre_build_script = '''

# Fetching using ssh (via pre_build_script in config.toml)

if [ -d "${CI_PROJECT_DIR}" ]; then rm -rf "${CI_PROJECT_DIR}"; fi

mkdir -p "${CI_PROJECT_DIR}"

cd "${CI_PROJECT_DIR}"

git init

git remote add origin "ssh://git@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"

git fetch origin "${CI_COMMIT_SHA}"

git reset --hard FETCH_HEAD

'''

[runners.cache]

MaxUploadedArchiveSize = 0

[runners.ssh]

user = "root"

password = "Password@123"

host = "ip address"

port = "22"
4
identity_file = "/root/.ssh/id_rsa"Step 7: Then after doing the changes restart the
GitLab-runner
Like Comment
Command: gitlab-runner restart

After restarting make sure Gitlab-runner is active in GitLab

Step 8: Creating Gitlab pipeline Script

In the .gitlab-ci.yml file create a pipeline as follows, after committing the changes we
can verify the job is successfully installed (java, maven, docker, and docker-compose)

stages:
- Orchestrate
job1:
stage: Orchestrate
script:
- cd /opt
- wget http://<ip address>/path of tar.gz
- tar -zxvf test.tar.gz --directory=/opt
- cd /opt/scripts
- ./install_all.sh
tags:
- ssh-test

Note: we need to specify the runner name in the tags

Step 9: Result

I hope
4 this article has provided you with valuable insights into the topic at hand. Whether you're a
beginner or an experienced professional, there's always something new to learn and discover in
the world. By sharing my knowledge and experience with you, I hope to have sparked your
Like Comment
curiosity and inspired you to explore further. Thank you for taking the time to read this article,
and I look forward to hearing your thoughts and feedback.

Written By,

DevOps Team

To view or add a comment, sign in

More articles by this author

A Beginner's Guide to Using Setting up GitLab Runner on TypeORM Migration G


Playwright for MQTT Topi… Kubernetes using MicroK8… Feb 21, 2024
Mar 4, 2024 Mar 1, 2024

Insights from the community


Operating Systems

What are the best tools for writing cross-platform code?

DevOps

How do you troubleshoot docker alpine vs ubuntu compatibility issues with other tools or
frameworks?

Operating Systems

How can you write Bash scripts that are compatible across different Operating Systems?

Software Development
4
How do you automate repetitive tasks using Linux command line scripts?

Like Comment
System Administration

How do you keep track of your PowerShell Scripts?

Software Development

What are the key differences between Linux command line and GUI usage?

Show more

Others also viewed

Shell Scripting: Automate Linux Updates, Upgrades and Clean-up Operations


Tosin Adewale · 2y

Linux for devOps part-2


Sahil Bhoyar · 5mo

"Future of Linux and Open Source" webinar Thu 3/18...


Fred Stluka · 3y

Mastering Git and GitHub: A Comprehensive Cheat Sheet for Linux Users
Sanket Bhalke · 1y

Linux for DevOps


Vasu Dev · 11mo

Basic Linux commands- Day 2


Sudesha T R · 11mo

Show more

Explore topics
Sales

4
Marketing

Like Comment
Business Administration

HR Management

Content Management

Engineering

Soft Skills

See All

© 2024 About

Accessibility User Agreement

Privacy Policy Cookie Policy

Copyright Policy Brand Policy

Guest Controls Community Guidelines

Language

Like Comment

You might also like