By testing GitLab CI/CD pipeline on my free SaaS GitLab account, I’ve explored the setup of specific GitLab Runner in containers on my local machine. For those who don’t want to use the shared runners of GitLab (it’s free but you still have to share your credit card details with GitLab!) or just want to do some tests and keep the full control of the Runners, here is a quick setup that should cover several test scenarios for your needs.

GitLab gives all the details for creating a Runner into one container here. If you are in a hurry (who isn’t!) and want a quick procedure to follow or if you want to create a second one in order to test jobs ran by two different Runner, follow through.

First Runner in a local container

Let’s hit our terminal straight away to run our first gitlab-runner container:

enb@DBI-LT-ENB project1 % docker run -d --name gitlab-runner --restart always \
  -v /Users/Shared/gitlab-runner/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest

As I’m on MacOS, I’m using /Users/Shared instead of /srv as per GitLab documentation. This volume mounted will keep the Runner configuration persistent after a container restart. I didn’t use the option -v /var/run/docker.sock:/var/run/docker.sock as I’m going to use Shell Executor.

Now let’s register our Runner:

enb@DBI-LT-ENB project1 % docker run --rm -it -v /Users/Shared/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register

Provide all the required register information (you could alternatively have passed all those parameters as options of the register command above):

Runtime platform                                    arch=arm64 os=linux pid=7 revision=32fc1585 version=15.2.1
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.com/
Enter the registration token:
GZ2358921LfDn-pbKiaL6exvSk0B2
Enter a description for the runner:
[21e5bba348f9]: gitlab-runner1
Enter tags for the runner (comma-separated):
r1
Enter optional maintenance note for the runner:

Registering runner... succeeded                     runner=GZ2358921LfDn-pb
Enter an executor: custom, docker-ssh, parallels, docker-ssh+machine, kubernetes, docker, shell, ssh, virtualbox, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

The GitLab instance and registration token are given in your GitLab project (Settings -> CD/CD -> Runners). Enter a unique tag (or tags) for this runner as this will be used to identify it in your CI/CD pipeline (by default untagged Jobs are not run). It is here I define I’ll use Shell Executor for this runner. Note that this configuration is saved in the volume mounted in the previous step.

Second Runner in another local container

Repeat these two commands above for the second Runner:

enb@DBI-LT-ENB project1 % docker run -d --name gitlab-runner2 --restart always \
  -v /Users/Shared/gitlab-runner/config2:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest

Just change the name used for this Runner as well as the local folder for the volume mount.

enb@DBI-LT-ENB project1 % docker run --rm -it -v /Users/Shared/gitlab-runner/config2:/etc/gitlab-runner gitlab/gitlab-runner register
Runtime platform                                    arch=arm64 os=linux pid=7 revision=32fc1585 version=15.2.1
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.com/
Enter the registration token:
GZ2358921LfDn-pbKiaL6exvSk0B2
Enter a description for the runner:
[c29d03644a73]: gitlab-runner2
Enter tags for the runner (comma-separated):
r2
Enter optional maintenance note for the runner:

Registering runner... succeeded                     runner=GZ2358921LfDn-pb
Enter an executor: custom, shell, ssh, docker+machine, docker-ssh+machine, kubernetes, docker, docker-ssh, parallels, virtualbox:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

We provide another description for the runner (this will be the name used in GitLab to identify your Runner) as well as another tag for this Runner (to be used in our Pipeline).

Checking of our setup

Both our Runner containers are up and running on my local machine:

enb@DBI-LT-ENB project1 % docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS         PORTS     NAMES
3ce44058ff1c   gitlab/gitlab-runner:latest   "/usr/bin/dumb-init …"   2 minutes ago   Up 2 minutes             gitlab-runner2
5d44c65d768e   gitlab/gitlab-runner:latest   "/usr/bin/dumb-init …"   4 minutes ago   Up 4 minutes             gitlab-runner

They have also been successfully registered in our GitLab project:

GitLab CI/CD Pipeline

The last step is to use both of those Runner in our CI/CD Pipeline by using the simple script below (Use the Editor of SaaS GitLab to edit the pipeline file .gitlab-ci.yml):

build1:
  tags:
    - r1
  stage: build
  script:
    - echo "Do your build here"

test:
  tags:
    - r2
  stage: test
  script:
    - echo "Do a test here"

“build1” Job is using the Runner with the tag r1 and “test” the one with the tag r2. Let’s run it and see the results:

We can see that each Job is using each of our Runner using the Shell Executor as configured. You can now perform more advanced tests with your Runners and your GitLab project.

If you want to bring your Docker skills to the next level, check out our Training course given by our Docker Guru!


Thumbnail [60x60]
by
DevOps