Here is a video tutorial
Introduction
You can organize and execute your tests right within GitLab with GitLab Continuous Integration/Continuous Development, which frequently eliminates the requirement for Jenkins. Robust features that address a variety of continuous integration and delivery demands are provided by GitLab Continuous Integration/Continuous Development. The following are some arguments in favor of using GitLab Continuous Integration/Continuous Development over Jenkins:
Advantages of GitLab Continuous Integration/Continuous Development
-
Integrated Platform: - GitLab offers an integrated platform that lets you handle deployment procedures, Continuous Integration/Continuous Development pipelines, and code repositories all in one location.
-
Simplified Configuration: - GitLab Continuous Integration/Continuous Development manages and version controls your Continuous Integration/Continuous Development setup along with your code with ease, using a single
.gitlab-ci.yml
file for pipeline configuration. -
Built-in Security: - GitLab’s settings allow for the safe management of environment variables and secrets.
- Sensitive variable management and masking are supported natively by GitLab.
-
GitLab CI/CD is scalable: - it can grow with your project. It is possible to manage job concurrency, employ multiple runners, and run jobs in parallel.
- GitLab Runners are a simple to set up tool for workload distribution.
-
Flexibility: - Handles a variety of tools and environments, such as cloud providers, Docker, and Kubernetes.
- Permits the usage of intricate job dependencies and unique scripts.
-
User-Friendly Interface: - Job logs, artifacts, and pipeline status are all clearly visible through the GitLab interface.
- Direct web interface monitoring and management of your pipelines is simple.
How to Move from Jenkins to GitLab Continuous Integration/Continuous Development
-
Examine your existing Jenkins pipelines
- Make a list of every Jenkins task, process, and stage that you have.
- Note the use of any plugins or particular customizations.
-
Make Jenkins Pipelines
.gitlab-ci.yml
compatible:- Align every Jenkins job with a GitLab Continuous Integration/Continuous Development stage.
- Convert Jenkinsfile logic or Groovy scripts to GitLab Continuous Integration/Continuous Development YAML syntax.
For complex operations, use dynamic child pipelines or the matrix technique.
-
Install and Configure GitLab Runners: - Install and set up GitLab Runners to carry out your tasks. GitLab has shared runners that you can utilize, or you can put up your own runners.
-
Setup Continuous Integration/Continuous Development Variables: - Save any environment variables, credentials, and secrets that are required in the GitLab Continuous Integration/Continuous Development settings.
-
Test Your Pipelines: - Use GitLab Continuous Integration/Continuous Development to run the migrated pipelines and make sure everything functions as it should.
- Examine work logs and artifacts to troubleshoot any problems.
-
Optimize and Automate: - Improve the performance and dependability of your pipeline arrangement.
- Arrange triggers and pipeline scheduling to automate the Continuous Integration/Continuous Development process.
How to set up GitLab runner
It is feasible to install Docker on a virtual machine (VM) and utilize it as a GitLab Runner. With this configuration, you can use the virtual machine (VM) to perform your Continuous Integration/Continuous Development operations as a Docker executor. Here’s how to make this happen:
Steps to Set Up a GitLab Runner on a Virtual Machine
1. Prepare the Virtual Machine
Make that an appropriate operating system, such as Ubuntu, Debian, CentOS, or any other Linux distribution supported by GitLab Runner, is installed in your virtual machine.
2. Install Docker on the Virtual Machine
Install Docker by following these steps:
Debian/Ubuntu
|
|
CentOS/RHEL
|
|
Verify that Docker is installed properly:
|
|
Verify that the installation is successful by running the hello-world image:
|
|
3. Install GitLab Runner
Download and install GitLab Runner:
Debian/Ubuntu
|
|
CentOS/RHEL
|
|
4. Register the GitLab Runner
Register the GitLab Runner with your GitLab instance. During the registration process, you will need to provide:
- Your GitLab instance URL (e.g.,
https://gitlab.example.com
). - A registration token (available from your GitLab project under Settings > Continuous Integration/Continuous Development > Runners).
- A description for the runner (e.g.,
my-vm-runner
). - Tags to identify the runner (e.g.,
docker, vm
). - The executor type (
docker
). - Docker image to use (e.g.,
docker:latest
).
Token
A unique string known as the registration token enables a GitLab Runner to register as a runner for a particular project, group, or instance and authenticate with your GitLab instance. The registration token for your GitLab project can be located as follows:
How to Locate the Registration Token
-
Log in to GitLab: - Go to `https://10.10.0.119/} in your web browser to access your GitLab instance.
- Enter your login information to log in.
-
Go to Your Project: - Locate the particular project for which you wish to sign up the runner.
- You can use the search box or go through your list of projects to find your project.
-
Select the Continuous Integration/Continuous Development Settings menu.
- Select Settings from the menu on the left sidebar of your project.
- Choose Continuous Integration/Continuous Development from Settings.
-
Expand the Runners Section: - To view additional options, scroll down to the Runners section and select the Expand button.
-
Find the Registration Token: - The Registration token token can be found under the Specific Runners section.
- To register your GitLab Runner, you must use this token.
Example of the Registration Token Location
Here’s an example of what the settings page might look like:
Settings
├── General
├── Integrations
├── CI / CD
│ ├── Pipelines
│ ├── Jobs
│ ├── Runners
│ │ ├── Shared Runners
│ │ └── Specific Runners
│ │ ├── Runner token: [your_project_specific_token]
│ │ └── Registration token: [your_registration_token]
Register the Runner with the Token
Once you have the registration token, proceed with the registration process:
|
|
When prompted, enter the GitLab instance URL and the registration token you retrieved from the Continuous Integration/Continuous Development settings:
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://10.10.0.119/
Enter the registration token:
[your_registration_token]
Follow the remaining prompts to complete the registration, specifying the description, tags, and executor type (docker
).
1. Adding Tags to the GitLab Runner
When you register the GitLab Runner, you can assign tags to it. If you have already registered the runner without tags, you can add tags by editing the configuration.
During Runner Registration
When running the gitlab-runner register
command, you will be prompted to enter tags. Enter the tags you want to use, separated by commas. For example:
|
|
Follow the prompts and add tags when prompted:
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://10.10.0.119/
Enter the registration token:
[your_registration_token]
Enter a description for the runner:
[my-vm-runner]
Enter tags for the runner (comma-separated):
docker,vm,ci
After Registration (Editing config.toml
)
If the runner is already registered, you can add tags by editing the config.toml
file, usually located at /etc/gitlab-runner/config.toml
:
|
|
2. Using Tags in .gitlab-ci.yml
Update your .gitlab-ci.yml
file to specify the tags for your jobs. This ensures that the jobs are picked up by the runners with matching tags.
Installing GitLab Runner with Docker - added additionally if you want to use Docker instead a virtual machine.
|
|
Registering the Runner
|
|
5. Configure the Runner to Use Docker
Assign a Runner to Your Project
-
Verify Runner Installation:
- Ensure that the GitLab Runner is properly installed and registered as described in the previous steps.
- Check the status of the runner:
1
sudo gitlab-runner status
-
Check Runner Configuration:
- Verify that the runner is listed in the GitLab UI and is assigned to the correct project.
-
Assign Runner to Your Project:
- Go to your GitLab project.
- Navigate to Settings > Continuous Integration/Continuous Development.
- Expand the Runners section.
- If the runner is registered but not assigned, you might see it under the Available specific runners section. Click the Enable for this project button next to your runner.
Configure the Executor
When registering your runner, you specify the executor type. If you registered the runner with docker
as the executor, you don’t need to change anything in your .gitlab-ci.yml
file regarding the executor. However, ensure that your runner’s configuration in config.toml
is correct:
By modifying the /etc/gitlab-runner/config.toml
file, make sure the GitLab Runner is set up to use Docker. Also modify the services_limit
to allow at least 1 service:
|
|
Make sure that Docker-in-Docker (DinD) capability is enabled by setting the privileged
mode to {true}.
Restart the GitLab Runner:
After saving the changes, restart the GitLab Runner to apply the new configuration:
|
|
Verify the Configuration:
Check the runner configuration to ensure the changes have been applied:
|
|
6. Add SSL certificate
1. Add the GitLab Server Certificate to the Runner’s Trust Store
If your GitLab server is using a self-signed certificate or an internal CA, you need to add the certificate to the GitLab Runner’s trusted certificates.
a. Install OpenSSL (if not already installed):
Make sure openssl
is installed on your machine. You can install it using the package manager of your distribution if it’s not already present.
|
|
b. Obtain the Certificate
Use the openssl
command to connect to your GitLab server and retrieve the certificate. Replace 10.10.0.119
with your GitLab server’s domain.
|
|
This command will create a file named gitlab.crt
in your current directory containing the server’s certificate.
c. Add the Certificate to the Runner’s Trust Store
Copy the certificate to the appropriate directory for trusted certificates. On most Linux distributions, this is /usr/local/share/ca-certificates
or /etc/ssl/certs
.
|
|
c. Restart GitLab Runner
After updating the certificates, restart the GitLab Runner service to apply the changes:
|
|
You may guarantee safe connection between the runner and the GitLab server by adding the GitLab server certificate to the runner’s trust store.
7. Generate a pair of keys to clone Git repository
The persistent exit code 128
error you’re encountering indicates an issue related to the SSH host key verification or permissions when trying to clone the Git repository. Let’s further address this problem.
Ensure SSH Keys and Known Hosts Configuration
1. Add SSH Key to Known Hosts
Ensure the SSH host key is correctly added to the known hosts on the runner:
|
|
Include this step in your GitLab CI script to make sure it works for all runners:
2. Use SSH Key for Cloning
Make sure you have a deploy key or personal access token with SSH access set up for cloning the repository. If you haven’t set up an SSH key, you can generate one and add it to your GitLab account or project:
|
|
Add the public key (~/.ssh/id_ed25519.pub
) to your GitLab project under Settings > Repository > Deploy Keys.
3. Ensure GitLab Runner Can Access SSH Key
The GitLab Runner needs to use the SSH key to authenticate. You can add the SSH private key as a secret variable in GitLab Continuous Integration/Continuous Development settings.
Add SSH Key as a Secret Variable
- Navigate to your GitLab project.
- Go to Settings > Continuous Integration/Continuous Development.
- Expand the Variables section.
- Add a new variable:
- Key:
SSH_PRIVATE_KEY
- Value: Paste the contents of your private SSH key (
~/.ssh/id_ed25519
). - Check Masked and Protected if appropriate.
- Key:
4. Create .gitlab-ci.yml
to use SSH key
Create your .gitlab-ci.yml
file to use the SSH private key for authentication and to build a Docker image from a Dockerfile and run your tests inside that Docker container:
|
|
Ensure DOCKER_TLS_CERTDIR is Set to an Empty Value:
The DOCKER_TLS_CERTDIR environment variable should be set to an empty string to ensure Docker does not try to use TLS.
Put .gitlab-ci.yml
in the root directory of your GitLab project.
8. Add logins and passwords as variables in GitLab
In GitLab, you can store sensitive information such as logins and passwords as Continuous Integration/Continuous Development environment variables. These variables are encrypted and can be accessed by your Continuous Integration/Continuous Development pipeline jobs. Here’s how to store and manage these variables:
Steps to Add Continuous Integration/Continuous Development Variables in GitLab
-
Navigate to Your Project:
- Open your GitLab project.
-
Go to Settings:
- On the left sidebar, click on Settings.
-
Access Continuous Integration/Continuous Development Settings:
- In the settings menu, select Continuous Integration/Continuous Development.
-
Expand Variables Section:
- Scroll down to the Variables section and click on the Expand button.
-
Add a Variable:
- Click on the Add variable button.
- Key: Enter the name of the variable, e.g.,
NPM_USER
,NPM_PASS
,AWX_USERNAME_
,AWX_PASSWORD
, etc. - Value: Enter the corresponding value for the variable.
- Type: Ensure the variable type is
Variable
. - Protected: Check this box if you want the variable to be available only to protected branches or tags.
- Masked: Check this box if you want the variable’s value to be masked in job logs.
- Environment scope: By default, it applies to all environments, but you can specify a particular environment if needed.
-
Save the Variable:
- Click on the Add variable button to save the variable.
Example Variables Configuration
For the variables used in your pipeline, you would add them as follows:
NPM_USER
NPM_PASS
AWX_USERNAME
AWX_PASSWORD
ARGOCD_USERNAME
ARGOCD_PASSWORD
- and so on for each service.
NPM_USER
and NPM_PASS
Update your .gitlab-ci.yml
to pass these variables to the Docker build process.
Ensure your Dockerfile
correctly uses the NPM_USER
and NPM_PASS
build arguments:
|
|
Accessing Variables in the GitLab Continuous Integration/Continuous Development Pipeline
In your .gitlab-ci.yml
file, you can access these variables using the ${VARIABLE_NAME}
syntax. GitLab Continuous Integration/Continuous Development automatically injects these variables into the pipeline’s environment.
Here is a snippet from the pipeline configuration showing how to use these variables:
|
|
9. Tests modification
argocd.js
|
|
awx.js
|
|
10. Troubleshooting
If your job is in a pending state and waiting to be picked by a runner, there are a few steps you can take to troubleshoot and resolve the issue:
1. Check Runner Registration and Status
Ensure that the runner is correctly registered and online:
|
|
This command will list all the registered runners and their status.
2. Check Runner Tags
Make sure the runner has the correct tags and that the tags match the ones specified in your .gitlab-ci.yml
file. You can add or modify tags by editing the config.toml
file and then restarting the runner.
3. Ensure Runner is Assigned to the Project
Ensure the runner is enabled for your project:
- Navigate to your GitLab project.
- Go to Settings > Continuous Integration/Continuous Development.
- Expand the Runners section.
- Ensure that your runner is listed under Available specific runners and is enabled for your project.
4. Review Runner Logs
Check the runner logs for any errors that might indicate why jobs are not being picked up:
|
|
5. Restart the Runner
Restarting the GitLab Runner can sometimes resolve issues:
|
|
6. Example .gitlab-ci.yml
with Tags
Make sure the relevant tags are specified in the .gitlab-ci.yml
file according to your runner setup.
Ensure the Runner is Enabled for the Project
- Go to your GitLab project.
- Navigate to Settings > Continuous Integration/Continuous Development.
- Expand the Runners section.
- Check if the runner is listed under “Available specific runners” and ensure it is enabled.
7. Review Runner Logs
The logs for the gitlab-runner
service are typically managed by the system’s logging service, which is usually systemd
on most modern Linux distributions. Here are the common ways to access the logs for the gitlab-runner
service:
Viewing GitLab Runner Logs
-
Using
journalctl
:The
journalctl
command can be used to view logs for services managed bysystemd
. To view the logs for thegitlab-runner
service, you can use:1
sudo journalctl -u gitlab-runner.service
This command shows all logs for the
gitlab-runner
service. You can use additional options to filter the logs, such as-f
to follow the logs in real-time or--since
to view logs since a specific time:1 2
sudo journalctl -u gitlab-runner.service -f sudo journalctl -u gitlab-runner.service --since "2024-05-28 00:00:00"
8. Configuring GitLab Runner Logging
If you want to configure where GitLab Runner writes its logs, you can adjust the service configuration. Here are the steps to change logging configurations:
-
Redirect Logs in the Service File:
If you need to redirect logs to a specific file, you can modify the
gitlab-runner
service file. This is generally located at/etc/systemd/system/gitlab-runner.service
or similar.1
sudo vim /etc/systemd/system/gitlab-runner.service
Add
StandardOutput
andStandardError
after theExecStart
line to redirect logs:1 2 3 4
[Service] ExecStart=/usr/local/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "gitlab-runner StandardOutput=file:/var/log/gitlab-runner/runner-std.log StandardError=file:/var/log/gitlab-runner/runner-err.log
Create a directory:
1
sudo mkdir -p /var/log/gitlab-runner
Create files:
1 2
sudo touch /var/log/gitlab-runner/runner-std.log sudo touch /var/log/gitlab-runner/runner-err.log
Reload the
systemd
configuration and restart the service:1 2
sudo systemctl daemon-reload sudo systemctl restart gitlab-runner