preface

The front end boss of the company left the company for some reasons. He left in a hurry. I had little contact with this aspect before, and I had only a half-knowledge of it. In these two days, WHILE learning, I began to build, while recording the whole process of building.

In this series of articles, from setting up Gitlab, to installing and registering Gitlab-Runner, to combining the two to deploy a simple project, you will learn how to automate the packaging and deployment of your projects on Gitlab.

There are four articles in the series, including:

  1. How to install Gitlab on Aliyun
  2. Install GITLAB – RUNNER
  3. LINUX login without password
  4. Deploy GITLAB’s projects using Gitlab-Runner

As I have been doing is the front end, I am not skilled for Linux, if there is a mistake, please point out.

Original address: Install gitlab-Runner

In this second installment of the series, we’ll install Gitlab-Runner and write a simple.gitlab-ci.yml file to see how it works.

Pre – work

I need to install Gitlab on two servers, I have Linux CentOS 7.6 64-bit, one needs to install Gitlab, about how to install Gitlab, can see this article ali Cloud install Gitlab notes. The other was used to install Gitlab-Runner.

Step1: install gitlab-runner

Download the corresponding gitlab-Runner for the system (the current version is 11.9.2) :

 # Linux x86-64
 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

 # Linux x86
 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

 # Linux arm
 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm
Copy the code

Add execution permission to gitlab-runner:

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

If you want to use Docker, you can install Docker.

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

Create a GitLab CI user

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

Install and start the service

 sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
 sudo gitlab-runner start
Copy the code

Step2: register the Runner

Run the following command to start registration:

 sudo gitlab-runner register
Copy the code

Fill in the Gitlab URL:

 Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
 # No domain name, so fill in IP
 http://xx.xx.xxx.xx:8888
Copy the code

Enter the token required to register Runner:

There are two types of tokens here. One is Shared Runner, which can be used by all projects: the top setting icon 🔧 -> Overview -> Runner

The other is the Specific Runner, who specifies a Specific project to use

 Please enter the gitlab-ci token for this runner
 # Here we use Shared Runner Token
 xxxxxxx
Copy the code

Enter a Runner’s description, which can be changed later in GitLab’s UI:

 Please enter the gitlab-ci description for this runner
 test-gitlab-runner-description
Copy the code

Type Runner’s tags (which will be used later)

 Please enter the gitlab-ci tags for this runner (comma separated)
 my-tag
Copy the code

Choose Runner’s executor

Here I use a shell.

 Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
 shell
Copy the code

We’ll see if everything works

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Copy the code

If Docker is selected, an additional step is required:

Please enter the Docker image (eg. Ruby :2.1): alpine:latestCopy the code

We’ll go back to Share Runners to see our new runner

Step3 create the project and.gitlab-ci.yml file

Create a.gitlab-ci.yml file in the project root directory and commit it with git.

# define stages, which are performed sequentially
stages:
  - install_deps
  - build_prod
  - deploy_prod

cache:
  key: ${CI_BUILD_REF_NAME}
  paths:
    - node_modules/
    - dist

Install build dependencies
install_deps_job:
  stage: install_deps
  # In which branch the script will be executed
  only:
    # - dev
    # - release
    - master
  script:
    - echo 'Simulate install build dependency phase'
  tags:
    - my-tag

# Build preprod environment in SRC directory
build_prod_job:
  stage: build_prod
  only:
    - master
  script:
    - echo 'Build pre-prod environment SRC application phase'
  tags:
    - my-tag

# Deploy the production environment
deploy_prod_job:
  stage: deploy_prod
  only:
    - master
  script:
    - echo 'Deploy production Phase'
  tags:
    - my-tag

Copy the code

Then you might see an error

Running with gitlab-runner 11.9.2 (fa86510e)
  on desc Z1UPKJjn
Using Shell executor...
Running on iZwz98jvb8bcz40ko474qsZ...
bash: line 68: git: command not found
bash: line 66: cd: /home/gitlab-runner/builds/Z1UPKJjn/0/main-group/main-project: No such file or directory
ERROR: Job failed: exit status 1
Copy the code

The reason for the error was that my server was only installed with Gitlab-Runner. According to the error message, git was required to pull the code on the Gitlab server, so we installed Git:

yum -y install git
Copy the code

Then use the

Git --version Check whether Git is installed successfullyCopy the code

After re-executing the pipline or submitting the code, you can see that everything works:

Note:

  1. Git needs to be installed on the Gitlab-Runner server.
  2. Pending: Pending pending: Pending pending: Pending pendingThis build is stuck, because the project doesn't have any runners online assigned to it. Go to Runners pageThis is because the corresponding runner is not found. Cause 1: The registration of gitlab-runner may fail. Cause 2: It may be that tags in the configuration file gitlab-ci.yml do not match the registered runner, so the tags entered by the corresponding runner during registration can be added to the stage.
  3. It is best not to install GitLab on the same machine as GitLab Runner.

Reference:

Install GitLab Runner manually on GNU/Linux Registering Runners GitLab Runner commands GitLab CI/CD Pipeline Configuration Reference Docker builds its own Gitlab CI Runner