Continuous integration and deployment based on Jenkins and Gitlab

CI/CD

Continuous Integration (CI) is a kind of software development practice. In Continuous Integration, developers integrate their development results at least once a day. After each integration, it will be tested by automatic build (including static scan, security scan, automatic test and other processes) to find errors in development. This can speed up the development of software and improve the efficiency of software development
Continuous Delivery refers to the Continuous Delivery of new versions of developed software to the quality team or users for review. If the review is passed, the code can enter the production stage
Continuous Deployment is the next step after continuous delivery. It means that the new version of the developed software can be automatically deployed to the production environment after passing the review

preparation in advance

Prepare three Centos7 virtual machines, set IP address and hostname, turn off firewall and selinux, synchronize time, modify the mapping relationship between IP address and hostname

hostname ip
master 192.168.29.132
bak 192.168.29.138
gitlab 192.168.29.140

jenkins is deployed in the master, gitlab is deployed in gitlab machine, and bak machine is used as the deployment machine
Install openjdk

[root@master ~]# yum install java -y
[root@bak ~]# yum install java -y
[root@gitlab ~]# yum install java -y

SSH password free login

[root@master ~]# ssh-keygen
[root@bak ~]# ssh-keygen
[root@gitlab ~]# ssh-keygen
[root@master ~]# ssh-copy-id root@192.168.29.138
[root@master ~]# ssh-copy-id root@192.168.29.140
[root@bak ~]# ssh-copy-id root@192.168.29.132
[root@bak ~]# ssh-copy-id root@192.168.29.140
[root@gitlab ~]# ssh-copy-id root@192.168.29.132
[root@gitlab ~]# ssh-copy-id root@192.168.29.138

Deploy Jenkins

Download yum source

[root@master ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
[root@master ~]# rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
[root@master ~]# yum install jenkins -y
#Launch jenkis
[root@master ~]# systemctl start jenkins
#jenkins listens to port 8080 by default. If there is a conflict, you need to modify the port number or stop other services
[root@master ~]# netstat -tnlp |grep 8080
tcp6       0      0 :::10086                :::*                    LISTEN      8597/java
#Check the log to find the initialization password
[root@master ~]# cat /var/log/jenkins/jenkins.log |grep  -A2 Please
Please use the following password to proceed to installation:

653fde4a04864f5589949d61f2b82415 #Initialize password

Test verification
Visit ip:8080, install the recommended plug-ins and set the administrator according to the guidance process, and then enter the home page
If the plug-in installation is not successful, you can download the corresponding plug-in and configure it manually
Manage Jenkins->Manage Plugins->Advanced->Upload Plugin->Upload




Since then, Jenkins has been deployed successfully

Deploy GitLab

Install GitLab

#Installation dependency
[root@gitlab ~]# yum install -y curl openssh-server openssh-clients postfix policycoreutils
#Download GitLab's yum source
[root@gitlab ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
#install
[root@gitlab ~]# yum install -y gitlab-ce
#Configuration (longer time)
[root@gitlab ~]# gitlab-ctl reconfigure

Test verification
visit http://ip , modify the password, log in through root/password, and then the GitLab deployment is completed

Deploy project

Configure GitLab

Create project project1

Add content such as files and folders to the project


Configure access
Set SSH KEY in settings Br / >! [insert picture description here]( https://img-blog.csdnimg.cn/20200604155827327.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpeGl4aWxhbGFsYWhhaGE=,size_16,color_FFFFFF,t_70)! [insert picture description here]( https://img-blog.csdnimg.cn/20200604155928463.png)
Copy the public Key in the master node to the Key. The end of the public Key should be changed to root@192.168.29.132

Since then GitLab configuration is complete

Configure Jenkins

Check whether there is a Publish over SSH plug-in. If there is no need to install it, the plug-ins installed in this deployment are

Configure SSH settings


You can configure password or SSH public key of bak machine. Test to see if the connection is successful. If the connection is successful, save SSH settings

Configure credentials to connect to GitLab
Select global voucher in system voucher

Add voucher

Select SSH, fill in Username, add the private Key of the machine to the Key, and click ok

Create project


Configure the project, select Git source, fill in the URL of GitLab machine, and select the GitLab certificate just configured

Set up information for the deployment machine

Click Save to save after configuration

Test verification

Deployment test

Terminal output visible deployment successful

Observe whether the existing files and folders in the GitLab project are generated in the target directory of the deployment machine

[root@bak ~]# ls /tmp/project1/
test  test1  test2.txt  test.sh  test.txt

Project deployed successfully
Continuous integration and deployment testing
Pull GitLab project from master for development, and upload after development

#Download File
[root@master ~]# git config --global user.name "git"
[root@master ~]# git config --global user.email "git@192.168.29.140"
[root@master ~]# git clone git@192.168.29.140:root/project1.git
//Cloning to 'project1'
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 15 (delta 2), reused 0 (delta 0), pack-reused 0
//In the receiving object: 100% (15 / 15), complete
//In delta processing: 100% (2 / 2), complete
[root@master ~]# ls project1/
test  test1  test2.txt  test.sh  test.txt

#Simulation development
[root@master ~]# echo hello > project1/hello.txt
[root@master ~]# ls project1/
hello.txt  test  test1  test2.txt  test.sh  test.txt
[root@master ~]# cat project1/hello.txt 
hello

#Upload the developed project
#Navigate to the project1 directory
[root@master project1]# git init
//Reinitialize the existing git repository at / tmp/project1/.git/
[root@master project1]# git remote add origin git@192.168.29.140:root/project1.git
fatal: long-range origin Already exists.
[root@master project1]# git add .
[root@master project1]# git commit -m "first commit"
[master 58860da] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
[root@master project1]# git push -u origin master
//Enumeration object: 3, complete
//In object count: 100% (3 / 3), complete
//Compression object: 100% (2 / 2), complete
//Write object: 100% (2/2), 251 bytes | 251.00 KiB/s, complete
Total 2 (delta 1), reused 0 (delta 0)
To 192.168.29.140:root/project1.git
   7a7a1a4..58860da  master -> master
//Branch 'master' is set to track remote branch 'master' from 'origin'

Check the status of projects in GitLab, hello.txt Already exists

Execute the Jenkins project to see if it is successful and if it appears in the deployment machine hello.txt

[root@bak ~]# ls /tmp/project1/
hello.txt  test  test1  test2.txt  test.sh  test.txt
[root@bak ~]# cat /tmp/project1/hello.txt 
hello

Since then, the continuous integration and deployment environment based on Jenkins and GitLab has been built successfully

Tags: Linux GitLab git jenkins ssh

Posted on Thu, 04 Jun 2020 08:57:37 -0400 by DarkReaper