GitLab Runner

The current installation environment is centOS 7.7. You are advised to perform the following tutorials in a range of versions 7.6 to 7.8. If the vM version is centOS 6.x, you are advised to abandon the preceding version and reinstall the VM. I have done it, too many holes. Attachment: Common commands:

# Check the current system
cat /etc/redhat-release
Copy the code

Before installing GitLab Runner, we need to install the basic front-end tools NVM, NodeJS and Docker well

NVM

Automatic installation

The curl - o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash# orWget - qO - https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bashCopy the code

Restart the bash

Choose according to your system

# bash: centOS system, restart bashrc
source ~/.bashrc

# zsh: 
source ~/.zshrc

# ksh: 
. ~/.profile
Copy the code

Install Nodejs

LTS /erbium NVM install v12.18.3# Install version 12
Copy the code

Git

Install Git

# install Git
yum install git

# check version
git --version # Possible version 1.8.3.1 needs to be upgraded
Copy the code

Manually Upgrading Git

If the Git version is earlier than 2.0, manually upgrade the Git version

1. Install dependencies
# install dependencies
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install gcc perl-ExtUtils-MakeMaker
Copy the code
2. Compile git source code
Access the SRC directory
cd /usr/src/
Download git filesWget HTTP: / / https://github.com/git/git/archive/v2.25.1.zipUnzip the file and delete the previous fileUnzip v2.25.1.zip && rm v2.25.1.zipAccess the unzipped file
cdGit - 2.25.1# create ();
make prefix=/usr/local/git all

If the git installation is successful, uninstall the old version and then install the new version:
rpm -e --nodeps git
make prefix=/usr/local/git install
Copy the code
3. Create a soft link
ln -s  /usr/local/git/bin/git /usr/bin/git
# Check version:
git --version
Copy the code

Docker

1. Automatic installation

sudo curl -sSL https://get.docker.com/ | sh  
Copy the code

GitLab Runner

Linux official website installation tutorial portal

1. Install

Automatic installation is recommended to avoid many permission problems

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
yum install gitlab-runner
Copy the code

Install using binary files

# 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"
Copy the code

If manual build is used, you need to add permissions manually, for example, /usr/local/bin to secure_path

2. Grant execution permission to:

sudo chmod +x /usr/local/bin/gitlab-runner
Copy the code

Create a GitLab CI user:

sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
Copy the code

4. Install and run as a service:

# installation
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# --user specifies the user that will be used to execute the build. Gitlab-runner uses root to avoid many permissions
# '--working-directory specifies the root directory in which all data will be stored when the **Shell** executor runs the build

# start service
sudo gitlab-runner start
Copy the code

5.Registered Gitlab Runner

# 1. Registration
sudo gitlab-runner register
# 2. Enter the URL of the Gitlab instance:
https://gitlab.example.com/
# 3. Enter the token used to register Runner:
TokenExample
# 4. Enter the description of Runner, which can be modified later in the GitLab interface
fedrunner

# 5. Enter the tag attached to Runner.Centos7.7 nodejs12, shell# 6. Choose Runner's execution mode: Shell is recommended
Please enter the executor: docker, parallels, shell, kubernetes, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine:
shell

# 7. If the execution mode is docker, you will be asked to fill in the default image, which can also be defined in.gitlab-ci.yml:Please enter the Docker image (eg. Ruby :2.1): alpine:latestCopy the code

6. Update GitLab Runner


# stop
sudo gitlab-runner stop

# update file
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"

Grant execute permission to:
sudo chmod +x /usr/local/bin/gitlab-runner

# start service:
sudo gitlab-runner start

# restart
sudo gitlab-runner restart

# check status
sudo gitlab-runner status
Copy the code

7. Other Grammar

# Stop running and uninstall GitLab Runner.
gitlab-runner uninstall 
Copy the code

Common permissions:

1. Add the secure_PATH permission (manually installed)

vim /etc/sudoers
Copy the code

The edited content is as follows:

#Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
# change as follows:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

# Then force save
:qw!
Copy the code
# set permissions
echo "gitlab-runner ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
Copy the code

2. Switch the root permission

The gitlab-ci runner uses the gitlab-ci user by default to perform operations. Through the instruction ps aux | grep gitlab – runner can see:

/usr/local/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner
Copy the code

Among them:

  • –working-directory: set the working directory, default is /home/{execute user}
  • –config: set the configuration file directory. The default directory is /etc/gitlab-runner/config.toml
  • –user: set the execution user name. The default is gitlab-runner

To change user to root, simply set the –user property as follows:

  1. Delete gitlab – runner
sudo gitlab-runner uninstall
Copy the code
  1. Install and set –user(for example I want to set it to root)
gitlab-runner install --working-directory /home/gitlab-runner --user root
Copy the code
  1. Restart gitlab – runner
sudo service gitlab-runner restart
Copy the code

Verify:

Again the ps aux | grep gitlab – runner will find – the username of the user has change into the root

/usr/bin/gitlab-ci-multi-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --syslog --user root
Copy the code

So far, when gitlab-runner executes. Gitlab-cli. yaml, it is the root user to execute the operation, and there is no tedious permission problem any more

3. Delete/unregister Runner

#1. Check whether runner has connected to GitLab
gitlab-runner verify
# 2. Delete from registration list
gitlab-runner verify --delete 
# 3. Unregister a runner
gitlab-runner unregister 

Use the token to log out
gitlab-runner unregister --url http://gitlab.example.com/ --token t0k3n
 
# delete first by name
gitlab-runner unregister --name test-runner

# delete all
gitlab-runner unregister --all-runners
Copy the code

4. CI syntax

Gitlab-ci. yML official website portal

Gitlab-ci. yML Chinese document