Based gitlab-ci of CICD

Brief introduction

gitlab-ci stands for gitlab continuous integration means, that is, continuous integration. The central idea is that when every push to gitlab that they will trigger a script execution, and then the content of the script included a series of customized content test, compile, deploy, and so on.

Automatic deployment involves a number of roles, it focuses on the following

GitLab-CI
This is a continuous integration system with the use of GitLab is GitLab own, which is installed on the server where you would with a GitLab. script parse .gitlab-ci.yml would be responsible for it.


GitLab-Runner
This is the carrier of script execution, running script part of .gitlab-ci.yml is in charge of the runner. After GitLab-CI project viewed in the .gitlab-ci.yml file, according to which the rules assigned to each Runner to run the appropriate script script. Some of these scripts test items used, with some deployment.


.gitlab-ci.yml
This is a file in the root directory of the git project, recorded a series of stages and the implementation of the rules. GitLab-CI will resolve it after the push, calling runner to run according to the contents inside.


Pipeline
once Pipeline in fact equivalent to a build tasks, which can contain multiple processes, such as the installation dependency, run the tests, compile, deploy test server, such as the production server deployment process.


Stages
Stages represent the construction phase, it means the process mentioned above. We can define multiple Stages in a Pipeline, these Stages will have the following characteristics:

All Stages will run in the order, that is, when a Stage complete, will start next Stage

Only when all Stages completed, the build task (Pipeline) will succeed

If any of the Stage fail, then the back of the Stages will not be executed, the build tasks (Pipeline) failure


Jobs
Jobs shows the construction work, which represents the work of a Stage executed. We can define multiple Jobs in Stages inside, which Jobs will have the following characteristics:
the same Stage of Jobs will be performed in parallel
during the same Stage of Jobs are executed successfully, the Stage will be successful
if any Job fails, then the failure Stage that the build tasks (Pipeline) failure

Posted flow chart is as follows:

image.png

Installation and deployment

Add gitlab official repository

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

Install the latest version of gitlab-runner

yum -y install gitlab-runner


Start Service

gitlab-runner list to view the status of each Runner
gitlab-runner stop to stop the service
gitlab-runner start to start the service
gitlab-runner restart to restart the service


registered

We need to get registered before the registration token

  1. Token specific location:  gitlab Project -> Settings -> CI / CD -> Runners Set

image.png

share runner token location of: the Admin Area -> Set Runners

image.png

Start Register


gitlab-runner  register

[root@localhost ~]# gitlab-runner register

Runtime platform                                    arch=amd64 os=linux pid=1784 revision=577f813d version=12.5.0

Running in system-mode.

##输入你的Gitlab URL

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):

http://192.168.60.133/

##输入注册令牌来注册Runner

Please enter the gitlab-ci token for this runner:

SeyTs9_4mKEsYjmfPr4e

##输入Runner说明

Please enter the gitlab-ci description for this runner:

[localhost]: test

##输入Runner的tags

Please enter the gitlab-ci tags for this runner (comma separated):

test

Registering runner... succeeded                     runner=SeyTs9_4

##输入Runner的执行方式

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

shell

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


配置文件保存在/etc/gitlab-runner/config.toml

配置项类似下面,可能需要手动添加builds_dircache_dir这两个变量,再重启服务

[[runners]]

name = "216XX"

url = "https://git.XX.com/"

token = "xxxxxx"

executor = "shell"

builds_dir = "/home/gitlab-runner/builds"

cache_dir = "/home/gitlab-runner/cache"

[runners.cache]

如果要同时处理多个 build 的话,需要进 /etc/gitlab-runner/config.toml 文件配置 concurrent 它的值为 >1 的值

注销runner:

gitlab-runner  unregister --url https://asdf.com/ci --token 43f334f34f34f34f4

或者

gitlab-runner  unregister --name NAME     删除特定的Runner


下面我们去需要发布的项目里面的根目录编写.gitlab-ci.yml脚本进行自动发布

.gitlab.-ci.yml文件必须在项目的根目录进行创建:

stages:
- build
- test
- deploy

#打包阶段

  build-job:
  stage: build
  tags:
  - report
  script:
  - mvn clean package -Dmaven.test.skip=true -Pprod
  only:
  - master

#测试阶段

test-job:
  stage: test
  tags:
  - report
  script:
  - docker run -d -v $(pwd)/target:/opt/tomcat-8.5/webapps -p 8099:8080 --name=xxxx  public/tomcat-8.5
  only:
  - master

#手动部署阶段

deploy-job:
  stage: deploy
  tags:
  - report
  only:
  - master
  environment:
  name: $report_v
  url: $report_url
  script:
  - echo $(whoami)
  - ssh -p 222 $report_host "/test/apache-tomcat/bin/shutdown.sh"
  - ssh -p 222 $report_host "rm -rf /test/tomcat/webapps/*"
  - scp -P 222 target/report.war $report_host:/test/tomcat/webapps
  - ssh -p 222 $report_host "/test/tomcat/bin/startup.sh"
  when: manual


配置.gitlab-ci.yml文件中用到的变量信息:CI / CD Settings/Variables,也可以直接在.gitlab.-ci.yml文件中指定,这里是为了安全考虑配置在了外面。

image.png

在项目的根目录写好.gitlab.-ci.yml脚本后就会自动触发构建部署


image2019-12-2_12-42-54.png?version=1&modificationDate=1575261774000&api=v2

我们在jobs里面可以看到执行的状态,是否执行成功或者报错。

手动执行部署

需要在手动执行的阶段添加参数

when: manual

Here you can see build_job phase of the task has been executed successfully, test_job stage to perform manual tasks waiting for deployment.


image2019-12-9_10-53-39.png?version=1&modificationDate=1575860019578&api=v2

Click test_job can view the status of the implementation process; if there is an error, there may be something wrong.

image2019-12-9_10-55-20.png?version=1&modificationDate=1575860120897&api=v2

Next, we performed manually deploy test_job phase of the task.

image2019-12-9_10-56-26.png?version=1&modificationDate=1575860186324&api=v2

The latter perform a refresh, you can see already executed successful.

image2019-12-9_10-56-59.png?version=1&modificationDate=1575860219928&api=v2

Above we configured the rollback and manually execute deployment tasks .gitlab-ci.yml in; Next we look rollback.

Rollback

When deploy task, add the following parameters:

environment:
  name: lims3_v


We can see that there are versions of our custom, we can see the points into history before.

image2019-12-9_11-1-44.png?version=1&modificationDate=1575860504222&api=v2
Click on the back button to roll back rollback.

image2019-12-9_11-5-23.png?version=1&modificationDate=1575860723668&api=v2


Q&A:

Given as follows:

figure 1:

image2019-12-2_13-54-33.png?version=1&modificationDate=1575266073000&api=v2

The reason is git version is too low, then upgrade to version 2.12 git version.


figure 2:

image.png

Solution:

Lead git missing components can be re-compile and install git


image 3:

image.png

User permissions issue:

解决方法:chown -R gitlab-runner:gitlab-runner  /home/gitlab-runner


Guess you like

Origin blog.51cto.com/7072753/2457116