What is CI/CD

  • CI continuous integration
  • CD continuous delivery

CI/CD is a way of frequently deploying applications during development through automated distribution

2. Why CI/CD

Imagine a release of a project that is manually deployed, requiring the following operations:

  • Unit testing
  • The packaged file
  • Upload server
  • , etc.

If every procedure had to be performed manually, it would have been tedious to make sure that nothing went wrong every time.

Now the big front-end projects have as many as 10+ people working on them, and there’s a lot of turnover.

If everyone releases like this, rapid iteration is error-prone.

That’s where CI/CD comes in,

CI/CD provides automated deployment, code quality checks, version management, fast rollback capabilities, and more

Three, preparation

  1. Front-end project: Code used to deploy the front-end business
  2. Gitlab: Version 8.0.0 and above supports CI/CD
  3. Gitlab runner:
    • Understood as an application that can interact with Gitlab through the Api and needs to be deployed in advance.
    • .gitlab-ci.yml configuration of each CI/CD task, need Runner to perform, and then feedback the results to gitlab instance.
    • These tasks can be configured, such as building images, code inspection, deployment publishing… And so on.
  4. Docker repository: Used to store the built image version for easy rollback at any time
  5. K8S cluster: Used to deploy projects. Multiple cluster deployment supports Dr

Iv. Specific execution process

1. Graphical process

Configure CI/CD publishing for Gitlab to understand the publishing process

2. Configuration files

The configuration is required in the project

  • .gitlab-ci.yml
  • Dockerfile
  • gitlab runner
  • production.yml

3. Specific execution

(1) The development code is completed

Once developed, submit the code to master and push the remote repository, execute the following statement that will trigger CICD

Git tag v1.0.0 && Git push Origin v1.0.0Copy the code

Note that CI/CD can be triggered in a variety of ways, including git tags, merging branches, calling apis, or Gitlab page operations, requiring configuration of.gitlab-ci.yml files

(2) Trigger CICD

As shown in the figure, Gitlab will start a pipeline that contains a set of tasks defined by the.gitlab-ci.yml file, including custom parallel and serial tasks.

Specific tasks are performed in Gitlab Runner, and Runners need to be configured in advance. I6PHy8wy is a shared runner:

(3) Build and upload the image to the self-built Docker warehouse

The CI/CD process can build a docker image for deployment, and then deploy K8S according to the [product].yml (K8S) configuration file.

We can see that in the whole process, we just execute the command git tag, and then some tasks such as static check, unit test, packaging, deployment do not need to worry about, until the completion of the completion can also configure email or wechat reminder, if there is a problem can be rolled back version. This avoids human error.

5. Installation and registration of Gitlab Runner

1. What is Gitlab Runner

From the above process, we can see that after CI/CD is triggered, there must be a Runner that can execute pipeline, so we deploy GitLab Runner first.

Runners are processes that pick up and execute CI/CD jobs for GitLab

A runner is a series of processes that can run CI/CD tasks.

Gitlab-runner is an application to execute GitLab CI/CD, first register Gitlab-runner, and then configure. Gitlab-ci. yml tasks and instructions can be successfully executed, and then feed back the results to GitLab through API.

2. Classification of Gitlab Runner

  • Shared Runner can be shared by all projects in the current GitLab repository
  • Group Runners can be shared by projects in a group and registered in group CICD
  • Project Runner can only be used in the current project and configured under the current project

3, installation,

The installation of the three types of runner is the same, supporting installation in the local, virtual machine, cloud server, Docker, K8S and so on. Official Installation Instructions

Here we choose to install to a Linux cloud server, documentation

(1) Download the corresponding Gitlab-Runner according to different server versions

# 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"

# Linux x86
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386"

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

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

# Linux s390x
sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-s390x"
Copy the code

(2) Grant permission to Gitlab-Runner

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

(3) Create a gitlab-CI user, which will be carried out under CICD later

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

(4) Install and start Gitlab-Runner

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

4, registered

(1) Run the gitlab-runner command

sudo gitlab-runner register
Copy the code

Then follow the prompts to enter the URL and token of 1 and 2 in the figure below, as well as a tag, which is used to associate with. Gitlab-ci. yml. You can define it by yourself, like test in the figure below.

To register, open the Runner module on the Setting/cicD page and get the URLH and token in the figure

  • The runner of the project goes to the setting/cicd of the project,
  • Group runner goes to group setting/cicd
  • The shared runner goes to the administrator to get it

The runner of the project is installed here, and once installed, you can see the Runner shown in Figure 3

At this point, it’s installed, and then it’s configured.gitlab-ci.ymlFiles, let CICD run.