GitLab Runner Setup Guide: Creating a Basic CI/CD Workflow(En)
In the previous article, we successfully installed GitLab. However, to facilitate future CI/CD operations, the next step is to set up GitLab Runner. Through the runner, we will execute and deploy tasks.
The official definition of GitLab Runner is as follows:
GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline
This article will cover the following sections:
- Deploying GitLab Runner
- Registering GitLab Runner with GitLab
- Conducting simple CI/CD tests
- Conclusion
1. Deploying GitLab Runner
To align with the previous article, this one will also deploy GitLab Runner using Docker-compose.
When installing on the same host, it’s important to note:
- If using Docker Executor for Docker runner, you need to utilize a specified network.
- If GitLab Hostname is set to localhost, during runner registration, you must specify the clone_url.
- If not using port 80, it’s advisable to follow the official method.
- Pay attention to Docker network configuration to avoid connectivity issues with GitLab.
#--------------------------------------------------------
# S1-1. Modify previous docker-compose file, add runner section
#--------------------------------------------------------
[root]# vim docker-compose.yaml
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
hostname: 'gitlab.test.example.poc'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab.test.example.poc:8929'
gitlab_rails['gitlab_shell_ssh_port'] = 2424
ports:
- '8929:8929'
- '8443:8443'
- '2424:2424'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
- '/var/run/docker.sock:/var/run/docker.sock'
shm_size: '256m'
gitlab-runner:
image: gitlab/gitlab-runner:latest
container_name: gitlab-runner
restart: always
networks:
- devops-net
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
- '$GITLAB_HOME/gitlab-runner:/etc/gitlab-runner'
[root]# docker-compose up -d
[root]# docker ps
[root]# docker logs -f gitlab-runner
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml
=> if you see this errors, this because we haven't regiter the runner. skip
#---------------------------------------------------
# S1-2. Login Web UI
#---------------------------------------------------
http://gitlab.test.example.poc:8929
=> root/P@ssw0rd
#---------------------------------------------------
# S1-3. Create Group
#---------------------------------------------------
Home => "+" (Create group) => Name: testgroup
2. Registering GitLab Runner with GitLab
#---------------------------------------------------
# S2-1. obtain runner token
#---------------------------------------------------
Build => Runners => token
TOKEN: GR1348941ZRdhocL1zyrdBaL1WUyC
#---------------------------------------------------
# S2-5. Register runner
#---------------------------------------------------
[root]# docker exec -it gitlab-runner gitlab-runner register
Enter the GitLab instance URL (for example, https://gitlab.com/): http://gitlab.test.example.poc:8929
Enter the registration token: GR1348941ZRdhocL1zyrdBaL1WUyC
Enter a description for the runner: test
Enter tags for the runner (comma-separated): test
Enter optional maintenance note for the runner: test
Enter an executor: custom: docker
Enter the default Docker image: python:3.9.13
#---------------------------------------------------
# S2-6. Verify
#---------------------------------------------------
Home => Group => Build => Runners => Registered runner information
3. Conducting simple CI/CD tests
#---------------------------------------------------
# S3-1. Create project
#---------------------------------------------------
Project => Create blank project => name: test-cicd => create
#---------------------------------------------------
# S3-2. Create new file
#---------------------------------------------------
"+" => New File => add contents => commit change
stages:
- Welcome_test_cicd
Job_Hello_world:
tags:
- test
stage: Welcome_test_cicd
image: python:3.9.13
script:
- python3 -V
※ Notes:
tags:Defining labels for the GitLab Runner。
stage:Defining the stage at which this task will execute。
image:Defining the Docker image for task execution. (For Docker Executor only.)
script:Defining the command to execute。
#---------------------------------------------------
# S3-3. Verify pipeline
#---------------------------------------------------
Build => Pipelines => (click)Passed
4. Conclusion
With the concepts based on GitLab, we’ve completed the simplest Pipeline test and also performed Pipeline configuration and execution through the GitLab UI.
In practical scenarios, many Kubernetes engineers may be involved in Pipeline maintenance within their job scope. Therefore, it’s recommended to learn at least one more Pipeline tool to comprehensively meet practical demands, rather than focusing solely on infrastructure maintenance.
※ References: