Continuous integration Gitlab CICD Runner & Jenkins

Use Gitlab Runnerrealization

And then installed on the server to be deployed gitlab runner

Download the executable file

# 按照架构自行选择 本文选择的是  Linux x86-64
# Linux x86-64
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

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

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

Set executable permission rights

chmod +x /usr/local/bin/gitlab-runner

Create a user

useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

Run Service

gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
gitlab-runner start

registered Runner

To gitlabthe need to find a URL with the token used

  • Path is:Project-> Settings -> CI/CD -> Runners -> Expand

Download the browser gitlab's ssl certificate

  1. Click your browser links on the left 锁头to download the certificate. This article is downloadable formatcer

  2. The downloaded certificate uploaded to the server to be deployed.

registeredrunner

gitlab-runner register --tls-ca-file=/home/gitlab-runner/test.cer
# 根据提示 依次输入如下内容
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://example.com/
Please enter the gitlab-ci token for this runner:
2312132dfa2fdafeafdafewaf
Please enter the gitlab-ci description for this runner:
[centos.localdomain]: test
Please enter the gitlab-ci tags for this runner (comma separated):
test
Registering runner... succeeded                     runner=nZsc7EsF
Please enter the executor: docker-ssh+machine, parallels, shell, ssh, virtualbox, docker+machine, kubernetes, custom, docker, docker-ssh:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
#——————————————————————————注——————————————————————————
本文的executor选择的是shell
# 注册成功后再次run一下gitlab-runner
[root@centos target]# gitlab-runner start
# 注册成功后生成 /etc/gitlab-runner/config.toml
[root@centos target]# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "test"
  url = "https://example.com/"
  token = "adsfasfdsafdsafdafdsfdafa"
  tls-ca-file = "/home/gitlab-runner/11111.cer"
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

Configuration in the project .gitlab-ci.ymlfile

This article.gitlab-ci.yml

Note: only the configuration of this deployment, specific other configurations can view the official website https://docs.gitlab.com/ee/ci/yaml/README.html

stages:
  - build
before_script:
  - export MVN_HOME  # export Envionment Variable
  - export JAVA_HOME
  - java -version
  - sh /home/gitlab-runner/kill.sh
# 定义 job
test:
  stage: build      # stage
  tags:
    - first         # runner tag you configured
  only:        
    - test          # branch support regex
  script:           #command
    - mvn clean
    - mvn package
    - cd ./target
    - nohup java -Xms3g -Xmx3g -jar  test-new-0.0.1-SNAPSHOT.jar --server.port=9999 > test.log 2>&1 &

Verification .gitlab-ci.ymlof the correctness

You can gitlabuse the CI Lintverification above ymlthe right line of the file. CI LintIn the CI/CDinside. Here is my validation results.

Status: syntax is correct
Parameter   Value
Build Job - test    

export MVN_HOME
export JAVA_HOME
java -version
sh /home/gitlab-runner/kill.sh
mvn clean
mvn package
cd ./target
nohup java -Xms3g -Xmx3g -jar  test-new-0.0.1-SNAPSHOT.jar --server.port=9999 > test.log 2>&1 &


Tag list: first
Only policy: refs, test
Except policy:
Environment:
When: on_success 

Precautions

  1. .gitlab-ci.ymlCulture designated runner tag must exist [runner can not find otherwise would have been pending]
  2. Error occursfatal: unable to access 'https://gitlab-ci-token:[email protected]/root/cmop.git/': Peer's Certificate issuer is not recognized.
# 关闭ssl校验
[root@gitlab-runner ~]# su - gitlab-runner
[gitlab-runner@gitlab-runner ~]$ git config --global http."sslVerify" false
# 查看
[gitlab-runner@gitlab-runner ~]$ cat /home/gitlab-runner/.gitconfig 
[http]
    sslVerify = false 

Use Jenkins achieve

Download / usejenkins

1. 访问 : https://jenkins.io/download/。本文采用的使用是`war`包安装

2. 下载: `wget http://ftp-chi.osuosl.org/pub/jenkins/war-stable/2.190.2/jenkins.war`

3. 运行 :`nohup java -Dhudson.util.ProcessTree.disable=true -jar jenkins.war --httpPort=8888 > jenkins.log 2>&1 &`
注: `-Dhudson.util.ProcessTree.disable=true`参数很重要,为了不让jenkins杀掉job创建的进程。如果不加的话,即便是`nohup`执行的命令也会在job执行之后杀掉。

Access ip:8888initial setup

Note: If you previously installed jenkinsbefore, it will be automatically upgraded, and retain data

Using the initial password

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
# 使用下面的密码进入,实际是一个md5的串
1111111111111111111111111

selectplugins

Refers directly selected suggestions plugins, portion of the insert can skip the installation fails.

Configuration Password

Reset a password

# 忘记秘密:
到`/root/.jenkins/users` `admin`用户下找到config.xml,修改下面的内容
<passwordHash>#jbcrypt:$2a$10$MiIVR0rr/UhQBqT.bBq0QehTiQVqgNpUGyWW2nJObaVAM/2xSQdSq</passwordHash>
这个密码是`123456`

In the jenkinsConfigure Gitlabautomatic deployment

Install gitlabplug-ins

1. 依次访问:`Manage Jenkins`->`Manage Plugins`
2. 在:`Available`中搜索`Gitlab`,安装`Gitlab`插件
3. 等待安装。

Configuring gitlabthe connection [may not]

1. 依次访问:`Manage Jenkins`->`Configure System`
2. 找到 Gitlab 标签页
3. 依次填入`Connection name`,`Gitlab host URL`
4. 添加一个`Credntials`,选择`Gitlab API token`
5. 填入在gitlab上设置的`Personal Access Tokens`
6. 点开`Advanced`,勾上`Ignore SSL Certificate Errors`
注:如果不勾上步骤6的话,gitlab是https的将会不成功。
7. 点击`Test Connnection`
8. 保存

Create an Freestyle projectA Jobtest

At this point you can first do any configuration, saved directly.

Configuration testjob gitlabWarehouse

1. 在job`test`页面点击`Configure`
2. 找到`Source Code Management`选择`Git`
3. 配置`Repository URL`并添加一个`Credentials`
注:此处想使用`Personal Access Tokens`添加不上,最后使用的账号密码
4. 保存

Configuration testjobTriggers

1. 在job`test`页面点击`Configure`
2. 找到`Build Triggers`选择`Build when a change is pushed to GitLab`其他默认就行
3. 保存

Configurationwebhook

1. 到`Gitlab`项目页面->`Settings`->`Integrations`
2. 输入上一步配置后面的`url`
3. 返回错误`Url is blocked: Requests to the local network are not allowed`
4. 上面的解决办法:
管理员账号登录gitlab,在Admin area中,左侧Settings -> Network -> Outbound requests,勾选Allow requests to the local network from hooks and services
但是没有gitlab管理员权限,我们将采取别的办法

Reconfigure testjob TriggerschangePoll SCM

1. 配置每分钟刷新一次
*/1 * * * *

Configuration bulidRun

export MVN_HOME # export Envionment Variable
export JAVA_HOME
java -version
sh /home/gitlab-runner/kill.sh
cd /root/.jenkins/workspace/test
mvn clean
mvn package
cd ./target
nohup java -Xms3g -Xmx3g -jar  test-new-0.0.1-SNAPSHOT.jar --server.port=9999 > test.log 2>&1 &
# 这些也可以写到一个shell脚本中,jenkins调用脚本

Compared

  • gitlab-ci
  1. Started simple
  2. And gitlabperfectly compatible
  3. No web page, but gitlabhas provided
  4. You need to configure their own build environment
  • jenkins
  1. Started simple
  2. You need to configure webhook, or, as the same polling article
  3. Have their own web page
  4. There are a wealth of plug-ins, powerful
  5. Compilation environment, for example, jdk mvncan be configured in the settings need to build

Guess you like

Origin www.cnblogs.com/primadonna/p/11843788.html