Jenkins+Gitlab for continuous integration

1, Jenkins and continuous integration

1) What is Jenkins?

Jenkins is an open source software project, which aims to provide an open and easy-to-use software platform and make continuous integration of software possible. Jenkins is a continuous integration tool developed based on Java, which is used to monitor continuous and repeated work. Its functions include:
1) Continuous software release / test project;
2) Monitor the work performed by external calls;

Friends interested in Jenkins can refer to Jenkins Chinese document

2) What is continuous integration?

1) What is integration?

It refers to the process of compiling, releasing, testing and launching the code!

2) What is continuous integration?

Efficient and continuous iterative code integration!

3) How to achieve continuous integration efficiently and accurately?

We need a set of automatic and visual platform to help us!

2, Why can Jenkins help us with continuous integration?

1) Jenkins is an open source and JAVA based continuous integration system

Because of the rich plug-in support, we can easily get through all aspects of the version library, test build environment and online environment. And rich and friendly notification users and development, management personnel.

2) Simple installation and maintenance

It's not too complicated to install Jenkins. And it supports a common platform.

3) Common Java applications

In the process of enterprise software construction, JAVA application engineering is a little complex, because of complex construction and code online, and service restart. In the whole process, it takes a lot of time, but Jenkins can integrate maven's compilation method well, and use the support of automatic plug-ins and custom development scripts. So it is widely used in the continuous integration platform of JAVA engineering.

Most of the packages used next are from Tsinghua University open source image station Download!

3, Deploy and install Gitlab

1) Install gitlab

[root@jenkins ~]# yum -y install  epel-release curl openssh-server openssh-clients postfix cronie policycoreutils-python
#Installation dependency
[root@jenkins ~]# wget
#Get gitlabRPM package
[root@jenkins ~]# rpm -ivh gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
#Install gitlab package
[root@jenkins ~]# vim /etc/gitlab/gitlab.rb 
external_url ''
#Change to local IP address for easy access
[root@jenkins ~]# gitlab-ctl reconfigure
#Reconfigure gitlab. Even if you do not modify the configuration file, you need to reconfigure gitlab after installation
[root@jenkins ~]# netstat -anpt | grep -w 80   
#Make sure its port 80 is listening

2) Configure gitlab

As shown in the picture:

3) Generate key pair for ssh login

[root@jenkins ~]# ssh-keygen -t rsa -C ""      #Generate key pair
[root@jenkins ~]# cat ~/.ssh/          #View public key and copy
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDuyAYPbmf92Pi6OOfcbdi7BCxN+Fq1c5lc4YAQhM8420mwfT4WltUOSXUidxR2oT3ViXSxwzlOr+XoHJ4YJEeQ3jIeXr5aSwtU5YTNgvVA+SYk0uvPLJg11NdEuMkSIB2K8PJ9zSxvppKbAkynmLEyZ1n61j5NbbApjgMslUqdB10vWowSE/Xoo11ugooQH2y3GxVrSv/EMeaVkToMVFM7BWx0pLBLi+tMJpb6Aj0pOSyv5rBj0hA1+2qrPQ3DtJc4QGMK+FRyxw/N13slfmoLzZ+6bF9oyNQZNWa9isL5C80srZhF7JkCrscYMIo0hOCkYOLVGJDFSgxiceG9ZmYh

Next, you need to operate on the web page. As shown in the picture:

4) Create a library

As shown in the picture:

5) Test on local server

[root@jenkins ~]# git clone git@
#Perform the clone operation. Enter yes at the prompt for the first time!
[root@jenkins ~]# cd test01/
[root@jenkins test01]# ls
#Enter the cloned library and check whether it is consistent with the content of the library created by the web page
[root@jenkins test01]# git config --global admin
[root@jenkins test01]# git config --global
#Self report home, simple declaration of user name and email address
[root@jenkins test01]# echo "hello world" > 123.txt
[root@jenkins test01]# git add 123.txt 
[root@jenkins test01]# git commit -m "commit form local first"
#Create a file to test
[root@jenkins test01]# git push origin master
#Push the local test file to the remote (gitlab)

6) gitlab verification

After refreshing the page, as shown in the figure:

This is the end of gitlab configuration. Next, install Jenkins!

4, Deploy and install Jenkins

1) Install Jenkins

[root@jenkins ~]# wget
#Get Jenkins package
[root@jenkins ~]# rpm -ivh jenkins-2.172-1.1.noarch.rpm 
#Install Jenkins
[root@jenkins ~]# rpm -ql jenkins               #View the location of Jenkins related files
[root@jenkins ~]# sed -i 's/JENKINS_PORT="8080"/JENKINS_PORT="8081"/g' /etc/sysconfig/jenkins
#Since the components of gitlab have occupied port 8080, you need to change the listening port
[root@jenkins ~]# /etc/init.d/jenkins start               # Start Jenkins service
[root@jenkins ~]# ss -lnt | grep 8081                 #Make sure the port is listening
LISTEN     0      50          :::8081                    :::*  

2) Configuration of Jenkins web page

As shown in the picture:

[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
#View and copy password information

Because it takes a long time to download its plug-in, another method is adopted:
After installing a plug-in, disconnect the network! download Required software

[root@jenkins ~]#  tar zxf jenkins-plugins.tar.gz -C /var/lib/jenkins/
[root@jenkins ~]# /etc/init.d/jenkins restart

Visit the web page again, as shown in the figure:

You can already see Jenkins's work page!

3) Configure the warehouse code of Jenkins pulling Gitlab

As shown in the picture:

Because SSH is used to get the code, a key pair is needed. The public key has been stored on gitlab. The information in red in the figure above is that the private key cannot be found, so you need to add the private key manually next!

[root@jenkins ~]# cat ~/.ssh/id_rsa            #View private key and copy all contents

Server side detects synchronized data:

[root@jenkins ~]# ls /var/lib/jenkins/workspace/web01
[root@jenkins ~]# cat /var/lib/jenkins/workspace/web01/123.txt 
hello world

5, Configure Jenkins+gitlab for continuous integration

It is recommended to download the following configuration for speed and convenience Plug in provided by me

The configuration is as follows:

Upload the packages I provided in the following order: gitlab OAuth - > gitlab plugin - > windows slaves - > Ruby runtime - > gitlab hook!

If you want to install the plug-in online, you can change the URL of its plug-in source to the Jenkins image station of Tsinghua University, and attach a Link to changed method , change by yourself!
The amendment is as follows:

This is the end of modifying plug-in address!

Because most of the next mouse points are OK, here is only the key figure attached!
Click: system management - > global security configuration - > authorization policy

Click: system management - > system settings

After the above operations are completed, carry out the following operations:

Generate token at terminal and copy:

[root@jenkins ~]# openssl rand -hex 10               #Copy the following

Go back to Gitlab's web page and do the following:

Next, you need to go to the Jenkins page to find the required URL! As follows:

We need to splice the URL s of Jenkins, which are: Jenkins's access address + / job.... = after splicing the authentication token code:

Then on the page to gitlab, enter the URL of Jenkins, as shown in the figure:

After adding, drop down the page and do the following:

At this point, the continuous deployment is completed, and the next step is to test. Submit the code on the terminal to gitlab, and check whether Jenkins has automatic synchronization, as follows:

#Push new code to gitlab
[root@jenkins ~]# cd test01/
[root@jenkins test01]# echo "123456" > test.txt
[root@jenkins test01]# git add test.txt 
[root@jenkins test01]# git commit -m "commit form local second"
[root@jenkins test01]# git push origin master

Jenkins can see the following, which shows that the continuous deployment configuration is complete!

According to the information output from the console, the code has been synchronized successfully. Check it locally!

[root@jenkins ~]# cat /var/lib/jenkins/workspace/web01/test.txt 
#Same as the document we just submitted

————————Thank you for reading————————

Keywords: Linux jenkins GitLab git RPM

Added by ferozsho on Tue, 11 Feb 2020 16:09:03 +0200