Setup a Gitlab Runner on Your Own Server to Run Your Jobs That Gets Triggered From Gitlab CI
From our previous post, we went through the setup on setting up a Basic CI Pipeline on Gitlab, in conjunction with Gitlab CI which coordinates your jobs, where we used the Shared Runners, which runs your jobs on Gitlab’s Infrastructure.
In Gitlab, you have Shared Runners and your Own Runners, which is used to run your jobs and send the results back to GitLab.
In this tutorial we will Setup a Server with gitlab-runner and Docker on Ubuntu and then Setup a Basic Pipeline to Utilize your Gitlab Runner.
Register the Runner. The Gitlab-CI Token is available in your CI/CD Settings panel from the UI: https://gitlab.com/<account>/<repo>/settings/ci_cd
$ gitlab-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
Please enter the gitlab-ci token for this runner:
Please enter the gitlab-ci description for this runner:
Please enter the gitlab-ci tags for this runner (comma separated):
Registering runner... succeeded runner=66m_339h
Please enter the executor: docker-ssh+machine, docker, docker-ssh, parallels, shell, ssh, virtualbox, docker+machine, kubernetes:
Please enter the default Docker image (e.g. ruby:2.1):
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Verify the Status and check if Docker and Gitlab Runner is enabled on startup:
$ gitlab-runner status
Runtime platform arch=amd64 os=linux pid=30363revision=7f00c780 version=11.5.1
gitlab-runner: Service is running!
$ systemctl is-enabled gitlab-runner
$ systemctl is-enabled docker
Gitlab-CI Config for Shared Runners
If you would like to use the shared runners that Gitlab Offers, the .gitlab-ci.yml config will look like this:
stages:-build-testbuild:stage:buildscript:-echo "this is building"-hostname-mkdir builds-touch builds/data.txt-echo "true" > builds/data.txtartifacts:paths:-builds/test:stage:testscript:-echo "this is testing"-hostname-test -f builds/data.txt-grep "true" builds/data.txt
Gitlab-CI Config for your own Gitlab Runner
Gitlab utilizes the tags that was specified on registration to determine where the jobs gets executed on, for more information on this, have a look at their docs
The .gitlab-ci.yml config for using your gitlab runner:
stages:-build-testbuild:stage:buildtags:-my-runnerscript:-echo "this is building"-hostname-mkdir builds-touch builds/data.txt-echo "true" > builds/data.txtartifacts:paths:-builds/test:stage:testtags:-my-runnerscript:-echo "this is testing"-hostname-test -f builds/data.txt-grep "true" builds/data.txt
Trigger and Check Docker
Commit the config to master, let your pipeline run their jobs upon completion have a look at docker on your server for the containers that the jobs ran on:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04292a78de0b c04b8be95e1e "gitlab-runner-cache.." About a minute ago Exited (0) About a minute ago runner-xx-project-xx-concurrent-0-cache-3cxx0
49b1b3c4adf9 c04b8be95e1e "gitlab-runner-cache.." About a minute ago Exited (0) About a minute ago runner-xx-project-xx-concurrent-0-cache-6cxxa
422b23191e8c hello-world "/hello"24 minutes ago Exited (0)24 minutes ago wizardly_meninsky
As we know each job gets executed in different containers, you can see from the output above that there was 2 different containers for the 2 jobs that was specified in our pipeline.