WeChat official account: operation and development story, author: double winter
Continuous integration (CI)
CI, Continuous Integration, is a very important link in the process of software development. In the process of Internet agile development, Continuous Integration is usually used for daily compilation and automatic testing to ensure timely discovery of submitted problems and avoid affecting the project progress.
Generally, the process of continuous integration includes:
Submit (merge) code
Different projects may have different steps. Some projects of more standardized companies may add static code inspection, and many small projects may not even have the test process due to the pressure of progress and QA.
GitLab CI/CD reprinted from: https://docs.gitlab.com/ee/ci/
Continuous integration tool
There are many CI tools, and Jenkins is the most commonly used one. Jenkins usually includes a master and many slave. Master is used to configure and organize nodes and tasks, while slave is used to actually execute configured tasks. Because of the huge user base, various plug-ins on Jenkins, especially many visual plug-ins, are very rich, which can help many novices quickly configure the required tasks.
Gitlab CI is git's official continuous integration tool. There are also special CI configuration and display pages on git project management page.
Readme. Com for many excellent open source projects on Github In MD, you can see the icon "build|passing" in the figure below, which is the display of the results of referencing the current version of CI/CD through the markdown element.
As more code is managed through git, gitlab CI has become a common CI platform. As far as I understand, gitlab CI is a simple version of Jenkins. The GIT server also serves as the function of Jenkins master, and I only need to prepare a slave. Moreover, gitlab Ci's runner supports multiple environments. In particular, Docker has exclusive configuration support. The configuration process is also very simple and mindless. Compared with Jenkins' slave configuration, it can be said to be a complete victory.
I have always done CI on the company's Jenkins service platform (in fact, I haven't done a few). Due to the problem of Jenkins permission control, it's inconvenient to try and troubleshoot environmental problems on the slave (you can see my previous troubleshooting of environmental problems in oclint). Just now the company project uses gitlab Ci, so I want to learn and try.
Installation and configuration of gitlab CI runner
1. Install Docker
docker is installed to run runner later
reference resources https://docs.docker.com/install/linux/docker-ce/centos/
- On linux
curl -sSL https://get.docker.com/ | sh
- On Mac OS
For Mac OS, we can use Homebrew to install Docker.
Homebrew's Cask already supports Docker for Mac, so you can easily use Homebrew Cask for installation:
$ brew cask install docker ==> Creating Caskroom at /usr/local/Caskroom ==> We'll set permissions properly so we won't need sudo in the future Password: # Enter Mac OS password ==> Satisfying dependencies ==> Downloading https://download.docker.com/mac/stable/21090/Docker.dmg ######################################################################## 100.0% ==> Verifying checksum for Cask docker ==> Installing Cask docker ==> Moving App 'Docker.app' to '/Applications/Docker.app'. 🍺 docker was successfully installed!
After loading the Docker app, click Next. You may be asked for your Mac OS login password, which you can enter. After that, a prompt window for Docker operation will pop up, and there is also a small whale icon on the status bar().
2. Install gitlab ci runner
- On linux
Reference link: https://docs.gitlab.com/runner/install/linux-manually.html
Install using binaries
# Linux x86-64 sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 sudo chmod +x /usr/local/bin/gitlab-runner sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
Check whether it works normally
root@i-klhcs1uo:~# gitlab-runner status Runtime platform arch=amd64 os=linux pid=16705 revision=c127439c version=13.0.0 gitlab-runner: Service is running!
- On Mac OS
➜ [/Users/mac] brew install gitlab-runner ➜ [/Users/mac] brew services start gitlab-runner ➜ [/Users/mac] gitlab-runner status Runtime platform arch=amd64 os=darwin pid=61606 revision=21cb397c version=13.0.1 gitlab-runner: Service is running!
3. Set Docker permissions
In order for gitlab runner to execute the docker command correctly, you need to add the gitlab runner user to the docker group, and then restart docker and gitlab ci runner
usermod -aG docker gitlab-runner service docker restart gitlan-runner restart
4. Gitlab runner registration
The registration process is to obtain the runner token > > registration
4.1 types of gitlab runner
shared: job that runs the entire platform project (gitlab)
Group: a job (Group) that runs all items under a specific group
specific: run the specified project job (project)
locked: unable to run project job
paused: the job will not run
First of all, you need to know the types of gitlab runner. If you can obtain runner token s in different interfaces, different types of runners will be generated..
Gitlab runner supports distributed and can run in various environments, which is very convenient for development and testing. After installing gitlan runner, you need to register with gitlab for association. First, log in to gitlab to obtain url and token
Get shared type runnertoken
Enter system settings - > runners
Get runnertoken of group type
Go to group - > Settings - > CI / CD - > runners - > group runners
Get runnertoken of type specific
Enter specific project - > Settings - > CI / CD - > runners - > specific runners
- Method 1: interactive registration
➜ [/Users/mac] gitlab-runner register Runtime platform arch=amd64 os=darwin pid=61621 revision=21cb397c version=13.0.1 WARNING: Running in user-mode. WARNING: Use sudo for system-mode: WARNING: $ sudo gitlab-runner... Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): http://gitlab.example.com/ Please enter the gitlab-ci token for this runner: 6-uZ1ndZ2NRGp8_TghnL Please enter the gitlab-ci description for this runner: [Double-dong.local]: gitlab-ci Please enter the gitlab-ci tags for this runner (comma separated): python3.4 Registering runner... succeeded runner=6-uZ1ndZ Please enter the executor: docker+machine, kubernetes, ssh, virtualbox, docker-ssh, parallels, shell, docker-ssh+machine, custom, docker: docker Please enter the default Docker image (e.g. ruby:2.6): python:3.4 Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ➜ [/Users/mac] gitlab-runner verify Runtime platform arch=amd64 os=darwin pid=61635 revision=21cb397c version=13.0.1 WARNING: Running in user-mode. WARNING: Use sudo for system-mode: WARNING: $ sudo gitlab-runner... Verifying runner... is alive runner=NkYLeMbb ➜ [/Users/mac] gitlab-runner list Runtime platform arch=amd64 os=darwin pid=61640 revision=21cb397c version=13.0.1 Listing configured runners ConfigFile=/Users/mac/.gitlab-runner/config.toml gitlab-ci Executor=docker Token=NkYLeMbbpJ2NyuwxaVKG URL=http://gitlab.example.com/
url: path to private git
Token: the token of the project, which is used to associate the runner with the project
Name: the name of the runner, which is used to distinguish the runner
tags: used to match jobs and runners
executor: execution environment
The url and token can be found on the CI configuration page of the project. name is only used to distinguish two runners, and has no special role. The tags attribute, both job and runner, is used to match the task and the runner executing the task. The tags attribute of job will be mentioned in the next article, or you can refer to it yourself gitlab-ci. Syntax of YML. There can be multiple tags in the runner, which can be separated by comma s during registration. When the tag of a job is a subset of the current runner tags, the job can be assigned to the current runner for execution.
For example, the tag of chestnut runner is set to python2 7,python3.4. Set the tag of the job to python2 7 or Python 3 4. macos can execute on this runner. The tag will not be assigned to this job.
The executor is the environment for executing job s. Usually, we will choose docker. If there is any other need, we can also consult the documents by ourselves.
When we finish setting up, we can use vi ~ / gitlab-runner/config. Toml opens the runner configuration file, and the information just filled in will be recorded in it. If multiple runners are configured, the section s of two runners will appear as shown in the figure.
After successful registration, return to the activated state of gitlab.
- Method 2: direct registration
➜ [/Users/mac] gitlab-runner register \ --non-interactive \ --executor "shell" \ --url "http://gitlab.example.com/" \ --registration-token "AvpQDzBCL66sYKyURChH" \ --description "devops-runner" \ --tag-list "build,deploy" \ --run-untagged="true" \ --locked="false" \ --access-level="not_protected"
⚠️: Here, if https is enabled on the gitlab server, the gitlab runner needs to sign the certificate file when registering. The solution is as follows:
➜ [/Users/mac] gitlab-runner register \ --non-interactive \ --tls-ca-file=/etc/gitlab/ssl/gitlab.example.com.crt \ --url "https://gitlab.example.com/" \ --registration-token "AvpQDzBCL66sYKyURChH" \ --executor "docker" \ --docker-image maven:latest \ --description "runner " \ --tag-list "run" \ --run-untagged \ --locked="false" Running in system-mode. Registering runner... succeeded runner=AvpQDzBC Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
4.3 effect after registration
Official account: operation and development story
Love life, love operation and maintenance
If you think the article is good, please click on the top right corner to send it to your friends or forward it to your circle of friends. Your support and encouragement is my greatest motivation. If you like, please pay attention to me~
Scanning QR code
Pay attention to me and maintain high-quality content from time to time
If you like this article, please share it with your circle of friends. For more information, please follow me.