Jenkins+Gitlab of CI/CD

Posted by totof06 on Thu, 23 Dec 2021 11:17:17 +0100


This article has reached 16000 words, a little more, but more complete

1, Jenkinx+Gitlab continuous integration environment overview

(1) What is CI/CD

  • CI is continuous integration, and CD is continuous delivery and continuous deployment. Generally, CD is continuous delivery

  • CI/CD overview:

Continuous integration (CI): continuous integration means that developers can frequently deploy code to the master backbone and conduct automated testing during code development

Continuous Delivery (CD): Continuous Delivery refers to the deployment of code to the pre production environment, i.e. test environment, on the basis of continuous integration

Continuous deployment (CD): the process of deploying code to the production environment on the basis of continuous delivery

  • Layer by layer, the purpose of using CI/CD is to turn the whole development, operation and maintenance environment into a closed loop and improve efficiency

(2) Jenkins overview

  • Jenkins is an open source software project. It is a continuous integration tool developed based on JAVA. It is used to monitor continuous repetitive work. It aims to provide an open and easy-to-use software platform to make continuous integration of software possible

  • Official website: https://jenkins.io/

(3) Gitlab overview

  • Gitlab is an open source application developed by Ruby on Redis, which implements a self managed Git project repository. Users can access public or private projects through the web interface

  • Gitlab has similar functions as Github. It can browse source code, manage defects and comments, and manage the team's access to the warehouse. Gitlab will provide a file history library to store submitted project versions, and also provide a code fragment collection function, which can easily realize code reuse and facilitate search when necessary in the future

  • Ruby on Redis: a framework that makes it easy to develop, deploy and maintain web applications

  • Official website: https://about.gitlab.com/

(4) The difference between Gitlab and Github

  • Same point

Both of them are web-based Git warehouses. To a large extent, Gitlab is modeled on Github. Both of them provide a platform for sharing open source projects and provide a centralized and cloud storage place for the development team to store, share, publish and cooperate in the development of projects

  • difference
  1. If GitHub wants to use private warehouses, it needs to pay for more than four, and Gitlab can create private free warehouses on it, because the company can deploy Gitlab itself
  2. Gitlab allows the development team to have more control over their code warehouse. Compared with Github, gitlab has many features, such as setting warehouse permissions for free, running users choose to share the source code of a project, and running users set project access permissions, which further improves security and can also set access to the overall improvement progress of the team, Through innersourcing, people who are not within the permission range cannot access the resource. Developers can also label a code as "still in progress" to remind other members that the code is not completed, so as to prevent the unfinished code from being merged into other code

In general, from the perspective of code privacy, sometimes companies don't want employees to get all the code, and Gitlab is undoubtedly a better choice, but for open source code, GIthub is still the first choice for code hosting

******notes******
git It is a distributed version controller, a command and a tool, Gitlab and Github All based on git The difference is that Github It is open to the Internet, and Gitlab It is usually used by the company's internal network, and both have website pages
gitlab-ce It's community service. It's free, gitlab-ee It's an enterprise version. It's free

(5) Jenkins works with Gitlab to continuously integrate the workflow of the system

  1. Developers push the new version to Gitlab

  2. Gitlab will trigger the master node of Jenkins (Jenkinx can be divided into master and slave nodes) to be built once

  3. The Jenkins master node assigns the construction task to one of several registered slave nodes. The slave node will be built according to the preset script. The script can do many things, such as compiling, testing, production test report and so on. These tasks that need to be completed manually can be completed by Jenkins

If compilation is required during the construction process, the distributed compiler distcc is generally used to speed up the compilation

2, Deploy continuous integration environment

(1) Experimental environment

systemhost nameipPlay a roleInstall software
Centos7.4gitlab192.168.100.202Gtilabgitlab-ce-12.0.2-ce.0.el7.x86_64.rpm
Centos7.4jenkins192.168.100.203Jenkinsjenkins-2.222.4-1.1.noarch.rpm
Centos7.4apache192.168.100.204webapache

Note: the memory of Gitlab should be larger, preferably 4G, and Jenkins should be 2G, otherwise an error will be reported later

(2) Experimental steps

-Set up Gitlab (100.202)

******(1)Basic configuration first
[root@Centos7 ~]# hostnamectl set-hostname gitlab
[root@Centos7 ~]# su
[root@gitlab ~]# systemctl stop firewalld
[root@gitlab ~]# setenforce 0
setenforce: SELinux is disabled
[root@gitlab ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 Write protected, will mount as read-only
mount: /dev/sr0 Already mounted or /mnt busy
       /dev/sr0 Already mounted to /mnt upper

******(2)install gitlab Dependent package
[root@gitlab ~]# yum install -y curl policycoreutils openssh-server openssh-clients postfix policycoreutils-python
. . . . . . 
complete!

******(3)Open the mail service, which is used by default postfix Send mail
[root@gitlab ~]# systemctl enable postfix
[root@gitlab ~]# systemctl start postfix

******(4)Turn off the firewall or allow http Yes, it has been turned off when making the basic configuration
[root@gitlab ~]# firewall-cmd --permanent --add-service=http  #Allow http to pass
[root@gitlab ~]# systemctl reload firewalld  #heavy load

******(5)install gitlab
#Installing gitlab requires the rpm package. There are two ways to download the rpm package
 Download with thunderbolt: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.0.2-ce.0.el7.x86_64.rpm
 use YUM Source download:( YUM Download very slow)
[root@gitlab ~]# vim /etc/yum.repos.d/gitlab.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
[root@gitlab ~]# yum install -y gitlab-ce  #Install community version of gitlab
. . . . . . 
complete!

#Here you can directly upload the software package to install gitlab
[root@gitlab ~]# ll
 Total consumption 625204
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 640204790 6 June 30-17:16 gitlab-ce-12.0.2-ce.0.el7.x86_64.rpm
[root@gitlab ~]# yum -y install gitlab-ce-12.0.2-ce.0.el7.x86_64.rpm 
. . . . . . 
complete!
       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  


     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/

******(6)to configure gitlab Access address
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb 
. . . . . . 
  13 external_url 'http://192.168. 100.202 '# modify the url to your own ip address
. . . . . . 
#Save exit

******(7)Reset gitlab,Every time the configuration file is modified, it needs to be reset, which takes about three minutes
[root@gitlab ~]# gitlab-ctl reconfigure
. . . . . . 
Running handlers:
Running handlers complete
Chef Client finished, 509/1343 resources updated in 02 minutes 27 seconds
gitlab Reconfigured!    #This indicates that the reset was successful

******(8)View ports, gitlab Default use nginx As web Interface, note that if you are accessing web If 502 is always reported in the interface, you need to clear the firewall rules or close the firewall, and the memory should be greater than 4 G Otherwise, it sometimes reports 502. Do not visit immediately after each reset. Wait a minute or two before visiting
[root@gitlab ~]# netstat -anpt | grep 80
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      2038/unicorn master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2103/nginx: master  
tcp        0      0 127.0.0.1:8082          0.0.0.0:*               LISTEN      2056/sidekiq 5.2.7  
tcp        0      0 0.0.0.0:8060            0.0.0.0:*               LISTEN      2103/nginx: master  
tcp        0      0 127.0.0.1:8080          127.0.0.1:47272         TIME_WAIT   -                   
tcp        0      0 127.0.0.1:8080          127.0.0.1:47264         TIME_WAIT   -                   
tcp        0      0 127.0.0.1:8080          127.0.0.1:47274         TIME_WAIT   -                   
tcp        0      0 127.0.0.1:44214         127.0.0.1:8060          ESTABLISHED 2526/prometheus     
tcp        0      0 127.0.0.1:8060          127.0.0.1:44214         ESTABLISHED 2104/nginx: worker  
tcp        0      0 127.0.0.1:52642         127.0.0.1:8082          ESTABLISHED 2526/prometheus     
tcp        0      0 127.0.0.1:8080          127.0.0.1:47266         TIME_WAIT   -                   
tcp        0      0 127.0.0.1:8082          127.0.0.1:52642         ESTABLISHED 2056/sidekiq 5.2.7  

(9) Log in to gitlab

The first time you log in to gitlab, you need to change the password for the root user. The root user is the super administrator of gitlab. Enter the new password here: nihao123!

Successfully logged in to gitlab

******(10)Sinicization gitlab
# GitLab Chinese community version project, v7-v8 8 is the "GitLab Chinese community version project" initiated by Larry Li( https://gitlab.com/larryli/gitlab ), from V8 After 9, @xhang began to continue the Sinicization project( https://gitlab.com/xhang/gitlab ). 
# The software download address here is the V12 version of gitlab, so you need to download the Chinese package of V12
# Download address: https://gitlab.com/xhang/gitlab/-/archive/12-0-stable/gitlab-12-0-stable-zh.tar.gz
[root@gitlab ~]# ll  #Upload package
 Total consumption 699968
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root  76557595 6 June 30-17:35 gitlab-12-0-stable-zh.tar.gz
-rw-r--r--  1 root root 640204790 6 June 30-17:16 gitlab-ce-12.0.2-ce.0.el7.x86_64.rpm
[root@gitlab ~]# tar xf gitlab-12-0-stable-zh.tar.gz 
[root@gitlab ~]# cat gitlab-12-0-stable-zh/VERSION   #Confirm version
12.0.3

******(11)Start Sinicization
[root@gitlab ~]# gitlab-ctl stop   #Stop gitlab first
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 1s, normally up
ok: down: gitlab-monitor: 0s, normally up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: grafana: 0s, normally up
ok: down: logrotate: 0s, normally up
ok: down: nginx: 1s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 0s, normally up
ok: down: redis: 1s, normally up
ok: down: redis-exporter: 0s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 0s, normally up
[root@gitlab ~]# unalias cp       #Delete alias for cp
[root@gitlab ~]# cp -rf gitlab-12-0-stable-zh/* /opt/gitlab/embedded/service/gitlab-rails/  #Don't worry about the error report in the back
cp: Unable to create directory"gitlab-12-0-stable-zh/log" To overwrite non directories"/opt/gitlab/embedded/service/gitlab-rails/log"
cp: Unable to create directory"gitlab-12-0-stable-zh/tmp" To overwrite non directories"/opt/gitlab/embedded/service/gitlab-rails/tmp"
[root@gitlab ~]# gitlab-ctl start    #Start gitlab and wait a minute or two before accessing gitlab, otherwise an error will be reported
ok: run: alertmanager: (pid 4094) 0s
ok: run: gitaly: (pid 4102) 1s
ok: run: gitlab-monitor: (pid 4123) 0s
ok: run: gitlab-workhorse: (pid 4127) 0s
ok: run: grafana: (pid 4134) 1s
ok: run: logrotate: (pid 4147) 0s
ok: run: nginx: (pid 4153) 1s
ok: run: node-exporter: (pid 4158) 0s
ok: run: postgres-exporter: (pid 4162) 1s
ok: run: postgresql: (pid 4243) 0s
ok: run: prometheus: (pid 4252) 0s
ok: run: redis: (pid 4261) 1s
ok: run: redis-exporter: (pid 4265) 0s
ok: run: sidekiq: (pid 4270) 1s
ok: run: unicorn: (pid 4276) 0s

Visit the web interface and successfully sinicize it


-Use of Gitlab

  • Basic command
nameeffect
gitlab-ctl stopClose gitlab
gitlab-ctl startOpen gitlab
gitlab-ctl restartRestart gitlab
gitlab-ctl reconfigureReset gitlab. It needs to be executed after modifying the configuration file. It can't be used indiscriminately
  • gitlab related configuration files and directories
nameeffect
/etc/gitlab/gitlab.rbgitlab main configuration file, you can customize some mail services, etc
/var/log/gitlab/Log directory, which corresponds to the log directory of each service
/var/opt/gitlab/Service directory, corresponding to the directory of each service
/var/opt/gitlab/git-dataWarehouse directory, record project warehouse and other submission information

-Turn off gitlab registration function

By default, users can register their own accounts, which is obviously unsafe, so they can turn off the account registration function of gitlab


-Configure gitlab mail alarm

The most important thing that the operation and maintenance personnel pay attention to is the alarm email, so gitlab is also equipped with the email sending function, but the default setting of gitlab may not receive the email, so we need to modify it manually. We need to add a bridge network card when making the email alarm

Configuration examples of various mailboxes on the official website: https://docs.gitlab.com/omnibus/settings/smtp.html

******(1)Modify the configuration file. The configuration here is qq of smtp
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb 
. . . . . .    #Add comments in line 58. Just write them in an empty line
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"     #Specify smtp server
gitlab_rails['smtp_port'] = 465       #Each server has a corresponding port, and the qq is 465
gitlab_rails['smtp_user_name'] = "1248873545@qq.com"   #mailbox
gitlab_rails['smtp_password'] = "tlefpqamwrhkhage"    #smtp number
gitlab_rails['smtp_authentication'] = "login"     
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = '1248873545@qq.com'   #Mailbox to send mail
#Save exit

******(2)Reset gitlab
[root@gitlab ~]# gitlab-ctl  reconfigure
. . . . . . 
Running handlers:
Running handlers complete
Chef Client finished, 16/669 resources updated in 01 minutes 07 seconds
gitlab Reconfigured!

******(3)Log in to the console to send mail
[root@gitlab ~]# gitlab-rails console
. . . . . . 
irb(main):002:0> Notify.test_email('1248873545@qq.com', 'test', 'test').deliver_now   #Send email to 1248873545@qq.com The title is test and the content is test
Notify#test_email: processed outbound mail in 123.1ms
Sent mail to 1248873545@qq.com (2811.6ms)
Date: Wed, 30 Jun 2021 10:07:59 +0800
From: GitLab <1248873545@qq.com>
Reply-To: GitLab <noreply@192.168.100.202>
To: 1248873545@qq.com
Message-ID: <60dbd1ff541c0_1ff33fc03fccf97c28335@gitlab.mail>
Subject: test
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Auto-Submitted: auto-generated
X-Auto-Response-Suppress: All

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>test</p></body></html>

=> #<Mail::Message:70094578102080, Multipart: false, Headers: <Date: Wed, 30 Jun 2021 10:07:59 +0800>, <From: GitLab <1248873545@qq.com>>, <Reply-To: GitLab <noreply@192.168.100.202>>, <To: 1248873545@qq.com>, <Message-ID: <60dbd1ff541c0_1ff33fc03fccf97c28335@gitlab.mail>>, <Subject: test>, <Mime-Version: 1.0>, <Content-Type: text/html; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <Auto-Submitted: auto-generated>, <X-Auto-Response-Suppress: All>>

After the mail can be successfully sent, start to modify the mailbox of root user, because the default mailbox of root user is admin@example.com , after modifying the mailbox, creating a group will send a mail warning

-Create a new group

  • Private: only group members can see it
  • Internal: as long as the logged in user can see it
  • Public: everyone can see it

-Create a new account





Log in to gitlab with a new account

It is obvious that ordinary users do not have as many permissions as root

-Add the newly created user to the group



#Gitlab users have five permissions in the group: Guest, Reporter, Developer, Master and Owner
Guest(Visitors): can create issue,Comment, can't read or write version Library
Reporter(Reporter): code can be cloned, but cannot be submitted, QA(Tester),PM(Supervisor or product manager)You can give this permission
Developer(Developer): you can clone code, develop, submit push,RD(R & D personnel)You can give this permission
Master(Main programmer): you can create projects and add tag,Protect branches, add project members, edit projects, core RD The person in charge can grant this authority
Owner(Owner): you can set project access - Visibility Level,Delete projects, migrate projects, manage group members, and develop groups leader You can give this permission

-Create a new project

You can create a project in a group or a private project. After creation, the use method is the same as that of github


After that, it has been translated into Chinese. The ssh key is generated below

#Because it is necessary to cooperate with jenkins, all the keys used here can be operated directly on the jenkins host
[root@Centos7 ~]# hostnamectl set-hostname jenkins
[root@Centos7 ~]# su
[root@jenkins ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:gh6chv+S4eK5Tnd8mWzbQNTmWdi4BLK9/H/VrA1Qenk root@Centos7.4-1
The key's randomart image is:
+---[RSA 2048]----+
|      . .        |
|       + o +  .  |
|      . o * oo . |
|   o o o = +o o E|
|  . * . S +  o o.|
|   +.o + +    . +|
|  ..+oo B .    = |
| ..o+o o + .  o .|
| o=o .. . . ..   |
+----[SHA256]-----+
[root@jenkins ~]# cat .ssh/id_rsa.pub   #View the public key and copy it to the web interface of gitlab
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcUghkjbif7vFFzmUfeWaon08+VDF314si8Fa0xonE3dFbpfr1tzSLmldaulj8yBwXvvtS7XKJUuh54gqgQycor/T1apfyfPSLHtCoeZWYzbACufm9penKCA0USkYwTkOQU9HpOx17p85nBO6ydo6Xz0l8UBNqvoy7/tO7mIslcignj+r6mgskTxCoOk9ujwDSQ3QmfhLmc/7B4wGSWclvETZyRGyb8g3rye7+16fpPENpq1YaweoiNAytdb+IZBVjgdIT0k2PVggVdYxntoA0IulCYwolT1KcjJllcSm47IHtDL4J8AgPGO8ZXf7oJM+vMtZRiHlURjLlQzbcAnOh root@Centos7.4-1







Use the jenkins host to copy the warehouse

[root@jenkins ~]# git clone git@192.168.100.202:root/test.git  #Clone, and copy the following URL directly in the web interface
 Cloning to 'test'...
The authenticity of host '192.168.100.202 (192.168.100.202)' can't be established.
ECDSA key fingerprint is SHA256:VhTZ5YxS5af2rHtfCvyc6ehXh3PD2A8KY2MyE6rHjiU.
ECDSA key fingerprint is MD5:e8:41:d2:8a:7e:e9:a9:47:a3:f0:29:be:e9:6d:df:51.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.202' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
In receiving object: 100% (3/3), done.
[root@jenkins ~]# ll
 Total consumption 4
-rw-------. 1 root root 1264 1 December 18:27 anaconda-ks.cfg
drwxr-xr-x  3 root root   32 6 June 30-18:57 test
[root@jenkins ~]# cd test/
[root@jenkins test]# ll    #Successfully cloned warehouse
 Total consumption 4
-rw-r--r-- 1 root root 16 6 June 30-18:57 1.html

-Build Jenkins (100.203)

  • Jenkins download address
https://pkg.jenkins.io/redhat-stable/jenkins-2.176.2-1.1.noarch.rpm
  • wget download address of linux:
wget https://pkg.jenkins.io/redhat-stable/jenkins-2.176.2-1.1.noarch.rpm
******(1)Basic configuration first
[root@jenkins ~]# systemctl stop firewalld
[root@jenkins ~]# setenforce 0
setenforce: SELinux is disabled
[root@jenkins ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 Write protected, will mount as read-only
mount: /dev/sr0 Already mounted or /mnt busy
       /dev/sr0 Already mounted to /mnt upper
       
******(2)because Jenkins Is to use JAVA Development, so it needs to be installed JDK
[root@jenkins ~]# ls  #Upload jdk package
anaconda-ks.cfg  jdk-8u91-linux-x64.tar.gz  test
[root@jenkins ~]# tar xf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@jenkins ~]# mv /usr/local/jdk1.8.0_91 /usr/local/java
[root@jenkins ~]# ls /usr/local/
bin  etc  games  include  java  lib  lib64  libexec  sbin  share  src
[root@jenkins ~]# vim /etc/profile
. . . . . .    #Add in the last line to optimize the environment variables
JAVA_HOME=/usr/local/java 
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar 
export PATH JAVA_HOME CLASSPATH
#Save exit
[root@jenkins ~]# source /etc/profile  #Execute the script to make the configuration just take effect
[root@jenkins ~]# java -version  #Check the java version and confirm the successful installation
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

******(3)upload Jenkins Software package, installation, configuration
[root@jenkins ~]# ll
 Total consumption 241624
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
drwxr-xr-x  3 root root        32 6 June 30-18:57 test
[root@jenkins ~]# yum -y install jenkins-2.222.4-1.1.noarch.rpm   #Install Jenkins
. . . . . . 
complete!
[root@jenkins ~]# vim /etc/sysconfig/jenkins   #Modify profile
. . . . . . 
 28 #
 29 JENKINS_USER="root"    #Change the user of jenkins to root, because the user who uploads the key to gitlab is root
 30 
. . . . . . 
#Save exit
[root@jenkins ~]# ln -s /usr/local/java/bin/java /usr/bin/  #Add java soft connection
[root@jenkins ~]# /etc/init.d/jenkins start    #Start jenkins
Starting jenkins (via systemctl):                          [  determine  ]
[root@jenkins ~]# netstat -anpt | grep 8080    #The default listening port is 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      15122/java  
[root@jenkins ~]# chkconfig jenkins on   #Add boot auto start
[root@jenkins ~]# chkconfig --list jenkins

Note: the output result is only displayed SysV Service, not included
 Primordial systemd Service. SysV Configuration data
 May be native systemd Configure overrides. 

      To list systemd Service, please execute 'systemctl list-unit-files'. 
      View in detail target Please execute
      'systemctl list-dependencies [target]'. 

jenkins        	0:shut	1:shut	2:open	3:open	4:open	5:open	6:shut

-Access Jenkins and install related plug-ins

(1) Unlock jenkins

Access with browser http://192.168.100.203:8080 Then, the unlock page will appear, and the path to save the password is provided. Open the file and enter the password

[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword   #After viewing, copy it to the web interface
820ee2b087484685b4b04ebb5d834c34

#Note: because jenkins needs to download plug-ins online, the default plug-in website is jenkins's official website (especially slow), so it is necessary to change the download source of the plug-ins for jenkins
#The steps to replace the plug-in download source are as follows:
1,Don't click on the above figure to continue and give Jenkins Server disconnection
2,After the network is disconnected, click to continue
3,As shown in the figure below, select skip plug-in installation



(2) Login Jenkins


-Install Jenkins plug-in

Jenkins plug-ins can be installed manually or online

  • Networking installation

Jenkins' default plug-in source downloads very slowly, so we can modify the plug-in source

(1) Network the jenkins server and restart jenkins

[root@jenkins ~]# /etc/init.d/jenkins restart

(2) Login Jenkins

(3) Select Manage Jenkins ----- > Manage plugins ------ > Advanced ------ -- > to slide down

Connect the following https://updates.jenkins.io/update-center.json Replace with

http://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

(4) Go back to the jenkins server and do the following

[root@jenkins ~]# cd /var/lib/jenkins/updates/
[root@jenkins updates]# sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json

So far, the plug-in source modification is complete

  • Manual installation

If the plug-in fails to be installed online or installed in a network free environment, you can choose to install it manually, either in the web interface or in the linux system

web interface installation:

The plug-in needs to be downloaded under the condition of network, then uploaded to the server, and directly click to select the file

Download address: http://updates.jenkins-ci.org/download/plugins/ , after downloading, upload it


linux command line installation:

[root@jenkins ~]# ls    #Upload plugins tar. GZ plug-in package
anaconda-ks.cfg  jdk-8u91-linux-x64.tar.gz  jenkins-2.222.4-1.1.noarch.rpm  plugins.tar.gz  test
[root@jenkins ~]# rm -rf /var/lib/jenkins/plugins   #Delete the original plug-in package
[root@jenkins ~]# tar xf plugins.tar.gz -C /var/lib/jenkins/   #Extract it directly into the jenkins directory
[root@jenkins ~]# systemctl restart jenkins  #Restart the service. The plug-in cannot take effect without restarting

Directly replace the plugins plug-in package directory under / var/lib/jenkins, and then restart. It is best to upload the plug-in package in Chinese, and then modify this to zh_CN restart jenkins will become Chinese

-Jenkins upgrade

******(1)see jenkins of war Directory where the package is located
[root@jenkins ~]# rpm -ql jenkins
/etc/init.d/jenkins
/etc/logrotate.d/jenkins
/etc/sysconfig/jenkins
/usr/lib/jenkins
/usr/lib/jenkins/jenkins.war    #This is the directory where the war package is located
/usr/sbin/rcjenkins
/var/cache/jenkins
/var/lib/jenkins
/var/log/jenkins

******(2)Download the latest stable war package
#Download address: wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war/latest/jenkins.war
#It's uploaded directly here
[root@jenkins ~]# systemctl stop jenkins  #Shut down the service first
[root@jenkins ~]# cd /usr/lib/jenkins/
[root@jenkins jenkins]# mv jenkins.war jenkins.war.bak   #Back up the original war package
[root@jenkins jenkins]# ll    #Upload a new war package
 Total consumption 133996
-rw-r--r-- 1 root root 70969355 6 June 30 23:57 jenkins.war
-rw-r--r-- 1 root root 66239216 5 June 28, 2020 jenkins.war.bak
[root@jenkins jenkins]# systemctl start jenkins   #Start service
[root@jenkins jenkins]# netstat -anpt | grep 8080    #Confirm startup
tcp6       0      0 :::8080                 :::*                    LISTEN      15538/java     

Login web interface authentication

It is obvious that the ui has changed and jenkins has been successfully upgraded

-Configure jenkins to update the code with gitlab

The following plug-ins are required

 #Some have been installed by default. You can search in the search bar above the installed to confirm
 Credentials ((installed by default)   #Signing certificate management plug-in
 Gitlab                       #Get the code from gitlab after installation
 Git and Git Client ((installed by default)#Used for jenkins to pull the source code from gitlab
 GitLab Hook                  #gitlab triggers jenkins build project 
                              #hook [h ʊ ʊ k] Hook; plugin ['pl ʌ ʌ g ɪ ɪ n] Plug in
 Gitlab Authentication        # gitlab and jenkins certification related plug-ins
 SSH Plugin                   #Process execution shell script
 Publish Over SSH             #Used to deploy applications via ssh


There is no optional plug-in, which may be due to the version problem. You can do it first

The public key of the root user of the jenkins host has been transferred to gitlab, and the synchronization of the warehouse of gitlab has been successful. Next, just transfer the private key of the root user of the jenkins host to the web interface of jenkins, so that jenkins can directly access the warehouse of gtilab without secret pull

[root@jenkins ~]# cat .ssh/id_rsa  #View and copy the private key
-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA3FIIZI24n+7xRc5lH3lmqJ9PPlQxd9eLIvBWtMaJxN3RW6X6
9bc0i5pXWrpY/MgcF777Uu1yiVLoeeIKoEMnKK/09WqX8nz0ix7QqHmVmM2wArn5
vaXpyggNFEpGME5DkFPR6Tsde6fOZwTusnaOl89JfFATar6Mu/7Tu5iLJXIoJ4/q
+poLJE8QqDpPbo8A0kN0Jn4S5nP+weMBklnJbxE2ckRsm/IN68nu/ten6TxDaatW
GsHqIjQMrXW/iGQVY4HSE9JNj1YIFXWMZ7aANCLpQmMKJU9SnIyZZXEpuOyB7Qy+
CfAIDxjvGV3+6CTPrzLWUYh5VEYy5UM23AJzoQIDAQABAoIBAQCcVtW/zNVLIDoz
Z89YM0SGuW149DOaL+h6pYNRimTuvty2JSTdQeDWg3IjXoKQ9csBirM62GXjTmLL
Qnx51s0t0RaJI9eo6cR6CCbWtlhScvrIJII0ckxg4a+uSzpdQtwsyV32G83DFoOf
lN9w4jIzRZOG1CfDIm4pZR2ITCnJKlRv+hL4gGDTEh3oFQHMJZwxA6qiwXqmRxJ+
jUNP9S2LHl2Zsz3QwTHCsdHUoJisLz348M9bjia5Kn7i/kJONdk3inxBTWhxX9sA
j/GDTO++H4bklRgtJuB+hL7wpYuL5T/dK0VShtWTZVQQZTycPNsmBEf6XYYx37K2
07ZRyCjBAoGBAPNBuWYP1RgIShVBaQsEXjWdP+4orN9cDvvueMKroCkg7QGv2m0v
SDmpKWkhLNurnd3QoqyW1Q1w0+iw1QOKmjmHxo95tu3ieNAQ5TOK8fAiEA996zpC
DiADVjo0qMrUncf1taWCytmhSu0kPDOVsniy8BMlJdzTlgL5QJ+G55NJAoGBAOfc
trZ9b9gI0bBG2Jx9khmY54RZReaJF2q21eR75YTB9myh9AWZ0a+7KukJoay7hk6b
2n3tKx05RV1xbXYM7QKQ2f6Xbvcc8gZbW6viWkrst3ic57stZ2F7Bdg6Q8gH4Wy8
vExvAutE8A7GMlHOjKfx6NByhj8I5jJoPWA84wWZAoGBAOn+AdokRqXsa17Q4btg
Q0MtdS6hX9EnW56JgQ45S0Hk0zb5MpIuljoo95WR/PV9usiBBcog+OYJ7z14xB64
96t5aM3glc90NyRl7CPNdva3vV05kckjezTeINayit848GFFgKqqCniXx1wfq+0v
BVNHRNoVtLsbqMRJgRkzCpVJAoGBAJDjCkYFInPUJvxJeShpC3f1ZY79nkGOH1Tv
Bb/ucCFCd3OwzGUHltGCWzEedKoU0Lukhqc0IkHzvNXEgItJTn3Q+HgwNwYyT0cL
pcBGBPhW7dtv6Rq9J6Hxl1hkKGECE6q4Uc5NfHx6KqyAy4b1VE3KTBqdxifzrq2H
F/tgcNjZAoGBAMoF+HmoroSp0obSTn/CHK15wF7aZBB6clA9d0Ezz63PAd2yLMe3
dMfTpRHVKcMiDrCeYVj/XNjwhA+GXexq4h1WgGBBHuiRwiFP67p5p4Jlad7t6qP9
pnh+oFpRclAmJEJwkj45u5J8oUFeQ7AztILU3qWFMpaYoYLYSdAbkdQR
-----END RSA PRIVATE KEY-----



-Create a new project using Jenkins




Click save


Create new project complete

-Publish code to web server

You can publish code by executing commands or scripts, and use root user to publish code on each web server. You can also set up some ordinary users, such as www or apache, to publish. If ordinary users are used to publish code, the user IDs on all servers should be consistent, so there is no permission problem. Here, the root user is directly used to publish.

******(1)by apache Host installation httpd
[root@Centos7 ~]# hostnamectl set-hostname apache
[root@Centos7 ~]# su
[root@apache ~]# systemctl stop firewalld
[root@apache ~]# setenforce 0
setenforce: SELinux is disabled
[root@apache ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 Write protected, will mount as read-only
mount: /dev/sr0 Already mounted or /mnt busy
       /dev/sr0 Already mounted to /mnt upper
[root@apache ~]# yum -y install httpd
. . . . . . 
complete!
[root@apache ~]# systemctl start httpd

Test it first and visit it successfully

******(2)Configure password free authentication, all required jenkins All servers that automatically publish code need to be authenticated without secret
[root@jenkins ~]# ssh-copy-id 192.168.100.204   #Send public key
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be ins talled: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.204 (192.168.100.204)' can't be established.
ECDSA key fingerprint is SHA256:VhTZ5YxS5af2rHtfCvyc6ehXh3PD2A8KY2MyE6rHjiU.
ECDSA key fingerprint is MD5:e8:41:d2:8a:7e:e9:a9:47:a3:f0:29:be:e9:6d:df:51.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.100.204's password:    #Enter 204 password

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.100.204'"
and check to make sure that only the key(s) you wanted were added.

[root@jenkins ~]# ssh 192.168.100.204  #Login 204
Last login: Thu Jul  1 00:38:03 2021
[root@apache ~]# exit    #Successful secret free
 Logout

******(3)Create a script to automatically upload files
[root@jenkins ~]# vim aaa.sh
#!/bin/bash
scp /var/lib/jenkins/workspace/aaa/* root@192.168.100.204:/var/www/html    #Here you need to see what your warehouse name is
#Save exit
[root@jenkins ~]# chmod +x aaa.sh  #Add executable permissions
[root@jenkins aaa]# pwd
/var/lib/jenkins/workspace/aaa
[root@jenkins aaa]# ll
 Total consumption 4
-rw-r--r-- 1 root root 16 6 June 30-16:32 1.html
[root@jenkins aaa]# cat 1.html 
aaaaaaaaaaaaaaaa

(4) Add script to jenkins







Access the httpd test results of 204

******Go to 204 first and find that the operation of the script is successfully executed
[root@apache ~]# ll /var/www/html/
Total consumption 4
-rw-r--r-- 1 root root 16 7 January 1 00:53 1.html
[root@apache ~]# cd /var/www/html/   
[root@apache html]# mv 1.html index.html   #Here we have modified the web page name, otherwise we can't access it. We can modify the name on gitlab

Successfully visited!!!

You can modify the file name and content in gitlab and test again




Found that the web page has become modified content!!

3, Actual combat project

(1) Update gitlab code to trigger jenkins automatic deployment

Actual scenario: in the company's test environment, when developers submit code to the gitlab warehouse, gitlab automatically notifies jenkins to build the project and test the code quality, and then deploy it to the test environment. Here, it is temporarily deployed to the test environment. For production environments, use manual deployment code later.

Plug ins to be installed: (some are installed by default, you can search in the installed search bar)

Gitlab Hook Plugin (previously installed)

Build Authorization

Token Root and Build Token Trigger

-Configure authentication token

******stay jenkins Generate random code on server
[root@jenkins ~]#  openssl rand -hex 12   
9525c873b906b013078337c1
#-The result of hex output is hexadecimal data, and 12 indicates that it is specified to generate 12 random characters

-Configuring triggers on jenkins



-Set the link and token in the configuration interface of git project

Log in to the user who created the project. Because the user who created the project above is root, I log in to root directly

If hooks and services are not allowed to access the local network, an error will be reported when adding a web hook. Add a web hook below



URL: http://192.168.100.203:8080/buildByToken/build?job=aaa&token=9525c873b906b013078337c1 , the job behind this URL needs to be the same as its own project name. You can click jenkins to the project first and then view the current URL

-Testing web hooks


-Update the project code in gitlab

******stay jenkins Upper operation
[root@jenkins ~]# ll
 Total consumption 477292
-rwxr-xr-x  1 root root        84 6 June 30-16:53 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
drwxr-xr-x  3 root root        32 6 June 30-18:57 test
[root@jenkins ~]# rm -rf test/  #Delete the original warehouse
[root@jenkins ~]# ll
 Total consumption 477292
-rwxr-xr-x  1 root root        84 6 June 30-16:53 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
[root@jenkins ~]# git clone git@192.168.100.202:root/test.git  #Clone it again
 Cloning to 'test'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 8 (delta 0), reused 0 (delta 0)
In receiving object: 100% (8/8), done.
[root@jenkins ~]# ls
aaa.sh  anaconda-ks.cfg  jdk-8u91-linux-x64.tar.gz  jenkins-2.222.4-1.1.noarch.rpm  plugins.tar.gz  test
[root@jenkins ~]# cd test/
[root@jenkins test]# ll
 Total consumption 4
-rw-r--r-- 1 root root 14 7 January 16:22 index.html
[root@jenkins test]# cat index.html 
bbbbbbbbbbbbbb
[root@jenkins test]# echo "aaaaaaaaaaaaaaaaaa" > index.html   #Modify the web page file
[root@jenkins test]# cat index.html 
aaaaaaaaaaaaaaaaaa
[root@jenkins test]# git add .   #Add to staging area
[root@jenkins test]# git commit -m "add index"  #Add to local warehouse

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@jenkins.(none)')
[root@jenkins test]# git config --global user.name "root"   #Specify user name
[root@jenkins test]# git config --global user.email "1248873545@qq.com"   #Specify mailbox
[root@jenkins test]# git commit -m "add index"   #Add to local warehouse
[master 6974052] add index
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins test]# git push origin master   #master branch pushed to remote warehouse
Counting objects: 5, done.
Writing objects: 100% (3/3), 237 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/test.git
   c1d114b..6974052  master -> master

-Accessing web hosts

Only the files in the warehouse are modified here because the above experiment has been scripted

success!!!!

(2) Implementation of Build Pipeline plug-in display flowchart

Function of Bulid Pipeline plug-in: this plug-in can show the sequence and dependency of each work in the form of flow chart


-Create a view for the aaa project




Done!!!

(3) Site rollback using branches

******stay jenkins Upper operation
[root@jenkins ~]# git clone git@192.168.100.202:root/test.git
 Cloning to 'test'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 11 (delta 0), reused 0 (delta 0)
In receiving object: 100% (11/11), done.
[root@jenkins ~]# ll
 Total consumption 477292
-rwxr-xr-x  1 root root        84 6 June 30-16:53 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
drwxr-xr-x  3 root root        36 7 January 17:05 test
[root@jenkins ~]# cd test/
[root@jenkins test]# git branch   #View all branches
* master 
[root@jenkins test]# git branch bb  #Create branch
[root@jenkins test]# git checkout bb  #Switch branch
 Switch to branch 'bb'
[root@jenkins test]# git branch
* bb
  master
[root@jenkins test]# git merge master 
Already up-to-date.  
[root@jenkins test]# cat index.html 
aaaaaaaaaaaaaaaaaa
[root@jenkins test]# echo "bbbbbbbbb" > index.html   #Modify file content
[root@jenkins test]# cat index.html 
bbbbbbbbb
[root@jenkins test]# git add .    #Push to staging area
[root@jenkins test]# git commit -m "add new"  #Push to local warehouse
[bb e4c8495] add new
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins test]# git push -u origin bb   #Push to remote warehouse
Counting objects: 5, done.
Writing objects: 100% (3/3), 235 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for bb, visit:
remote:   http://192.168.100.202/root/test/merge_requests/new?merge_request%5Bsource_branch%5D=bb
remote: 
To git@192.168.100.202:root/test.git
   6974052..e4c8495  bb -> bb
 branch bb Set to track from origin Remote branch of bb. 

At this time, you can see the addition of new branches in the web interface of gtilab

******Modify script
[root@jenkins ~]# vim aaa.sh 
#!/bin/bash
cd /root
git clone -b bb git@192.168.100.202:root/test.git
scp /root/test/index.html root@192.168.100.204:/var/www/html/index.html
[root@jenkins ~]# chmod  +x aaa.sh 

Test whether to change the page


(4) Publishing tomcat project using jenkins

  • This project requires JDK and Maven to be installed on Jenkins

  • Jenkins is developed in java, so jdk must be installed. Because this project is based on maven architecture, maven must be installed

  • Maven: it is to package the java code written by the developer into a war package, and then deploy the packaged war package to tomcat for client access

  • Project Object Model: Project Object Model. POM saved in xml format xml file. This file is used to manage: source code, configuration file, developer information and role, problem tracking system, organization information, project authorization, project url, project dependency, etc. The document is developed and maintained by our operation and maintenance personnel, so we don't need to care about it.

-Install Maven

#Download Maven: wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
******(1)It has been downloaded here, so you can upload it directly for installation
[root@jenkins ~]# ll
 Total consumption 485928
-rwxr-xr-x  1 root root       144 7 January 17:14 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root   8842660 7 February 9:29 apache-maven-3.5.4-bin.tar.gz
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
drwxr-xr-x  3 root root        36 7 January 17:05 test
[root@jenkins ~]# tar xf apache-maven-3.5.4-bin.tar.gz 
[root@jenkins ~]# mv apache-maven-3.5.4 maven      
[root@jenkins ~]# ln -s maven/bin/mvn /usr/bin/mvn   #Optimize command execution path
[root@jenkins ~]# vim /etc/profile  #Add at the end
. . . . . . 
export MAVEN_HOME=/root/maven
export PATH=$PATH:$MAVEN_HOME/bin
#Save exit
[root@jenkins ~]# source /etc/profile

-Create Maven private server nexus

Because maven wants to download the jar package, maven will go back to the official website by default. The official website download is too slow, so nexus can be built to speed up the download of the jar package

Two options for configuring the warehouse: 1 POM under project XML configuration, only the current project will take effect

​ 2. Configure the global in maven, and all items take effect

******(1)upload nexus Package for installation
[root@jenkins ~]# ll
 Total consumption 605956
-rwxr-xr-x  1 root root       144 7 January 17:14 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root   8842660 7 February 9:29 apache-maven-3.5.4-bin.tar.gz
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
drwxr-xr-x  6 root root        99 7 February 9:29 maven
-rw-r--r--  1 root root 122904706 7 February 9:34 nexus-3.13.0-01-unix.tar.gz
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
drwxr-xr-x  3 root root        36 7 January 17:05 test
[root@jenkins ~]# tar xf nexus-3.13.0-01-unix.tar.gz 
[root@jenkins ~]# mv nexus-3.13.0-01 /usr/local/nexus
[root@jenkins ~]# /usr/local/nexus/bin/nexus start   #Turn on nexus
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexus
#The default listening port of nexus is 8081. Because it is developed in java, all starts slowly
[root@jenkins ~]# netstat -anptu | grep 8081
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      1460/java  

Access nexus

Default account: admin, default password: admin123

Modify the URL to Alibaba cloud: http://maven.aliyun.com/nexus/content/groups/public

Configure private server

******Modify profile
[root@jenkins ~]# cd maven/conf/
[root@jenkins conf]# vim settings.xml 
. . . . . .  #Insert in the middle of the configuration file
     <mirror>
        <id>nexus-aliyun</id>
           <mirrorOf>*</mirrorOf>
           <name>Nexus aliyun</name>
           <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        <id>nexus</id>
           <mirrorOf>*</mirrorOf>
           <url>http://192.168.100.203:8081/repository/maven-central/</url>
      </mirror>
. . . . . . #This is also inserted from the middle of the configuration file
      <server>
      <id>my-nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
      </server>
      <server>
      <id>my-nexus-snapshot</id>
      <username>admin</username>
      <password>admin123</password>
      </server>
. . . . . . #Here, start with the profiles in line 197      
197   <profiles>
      <id>nexus</id>
      <repositories>
         <repository>
            <id>central</id>
           <url>http://192.168.100.203:8081/repository/maven-central/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
          </repository>
       </repositories>
       <pluginRepositories>
         <pluginRepository>
           <id>central</id>
           <url>http://192.168.100.203:8081/repository/maven-central/</url>
           <releases><enabled>true</enabled></releases>
         <snapshots><enabled>true</enabled></snapshots>
         </pluginRepository>
       </pluginRepositories>
      </profiles>
      <activeProfiles>
      <activeProfile>nexus</activeProfile>
      </activeProfiles>
. . . . . .       
#Save exit

-Install git and maven plug-ins on jenkins's web interface


If you can't see anything in the optional plug-in, you can click to download the new version, download the latest war package, upgrade jenkins, or click to return the previous version on the web page





There must be a network during the installation process. If the installation is unsuccessful, you can manually go to the official website, or directly upload the plug-in directory and the plug-in jpi file in the jenkins directory of linux

-Enter the configuration phase





-New jenkins task









#script
[root@jenkins ~]# vim bbb.sh
#!/bin/bash

source /etc/profile

scp /var/lib/jenkins/workspace/tast/test1/target/test1.war root@192.168.100.204:/usr/local/tomcat/webapps/

ssh root@192.168.100.204 "/usr/local/tomcat/bin/shutdown.sh"

sleep 3

ssh root@192.168.100.204 "/usr/local/tomcat/bin/startup.sh"
[root@jenkins ~]# cd /var/lib/jenkins/workspace
[root@jenkins workspace]# rm -rf *  #Delete original
[root@jenkins workspace]# ll
 Total consumption 0
[root@jenkins workspace]# git clone git@192.168.100.202:root/test.git  #Re clone
 Cloning to 'test'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 14 (delta 0), reused 0 (delta 0)
In receiving object: 100% (14/14), done.
[root@jenkins workspace]# ll
 Total consumption 0
drwxr-xr-x 3 root root 36 7 February 18:19 test
[root@jenkins workspace]# cd test/
[root@jenkins test]# ll
 Total consumption 4
-rw-r--r-- 1 root root 19 7 February 18:19 index.html
[root@jenkins test]# rm -rf *
[root@jenkins test]# ll  #Transfer the project to the path specified by the script
 Total consumption 4
drwxr-xr-x 5 root root  97 7 February 17:56 test1
-rw-r--r-- 1 root root 491 7 February 17:56 time.java
[root@jenkins test]# git add test1    #Add staging area
[root@jenkins test]# git add time.java    
[root@jenkins test]# git commit -m "tomcat "  #Add to local warehouse
[master 502f979] tomcat
 20 files changed, 262 insertions(+)
 create mode 100644 test1/.classpath
 create mode 100644 test1/.project
 create mode 100644 test1/.settings/.jsdtscope
 create mode 100644 test1/.settings/org.eclipse.jdt.core.prefs
 create mode 100644 test1/.settings/org.eclipse.m2e.core.prefs
 create mode 100644 test1/.settings/org.eclipse.wst.common.component
 create mode 100644 test1/.settings/org.eclipse.wst.common.project.facet.core.xml
 create mode 100644 test1/.settings/org.eclipse.wst.jsdt.ui.superType.container
 create mode 100644 test1/.settings/org.eclipse.wst.jsdt.ui.superType.name
 create mode 100644 test1/.settings/org.eclipse.wst.validation.prefs
 create mode 100644 test1/pom.xml
 create mode 100644 test1/src/main/resources/com/Time.java
 create mode 100644 test1/src/main/webapp/WEB-INF/web.xml
 create mode 100644 test1/src/main/webapp/index.jsp
 create mode 100644 test1/target/classes/com/Time.class
 create mode 100644 test1/target/classes/com/Time.java
 create mode 100644 test1/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
 create mode 100644 test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.properties
 create mode 100644 test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.xml
 create mode 100644 time.java
[root@jenkins test]# echo $?
0
[root@jenkins test]# git push -u origin master   #Push to remote warehouse
Counting objects: 38, done.
Compressing objects: 100% (26/26), done.
Writing objects: 100% (37/37), 5.25 KiB | 0 bytes/s, done.
Total 37 (delta 1), reused 0 (delta 0)
To git@192.168.100.202:root/test.git
   6974052..502f979  master -> master
 branch master Set to track from origin Remote branch of master. 

-Installing tomcat on the web host

******(1)Delete first httpd
[root@apache ~]# yum -y remove httpd
. . . . . . 
complete!

******(2)install tomcat
[root@apache ~]# ll     #Upload the source package of jdk and tomcat
 Total consumption 186324
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root   9417469 7 March 2:08 apache-tomcat-8.5.16.tar.gz
-rw-r--r--  1 root root 181367942 7 March 2:02 jdk-8u91-linux-x64.tar.gz
[root@apache ~]# tar xf jdk-8u91-linux-x64.tar.gz 
[root@apache ~]# mv jdk1.8.0_91/ /usr/local/java
[root@apache ~]# cat <<a>> /etc/profile
> export PATH=$PATH:/usr/local/java/bin:/usr/local/java
> a
[root@apache ~]# source /etc/profile
[root@apache ~]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
[root@apache ~]# tar xf apache-tomcat-8.5.16.tar.gz 
[root@apache ~]# mv apache-tomcat-8.5.16 /usr/local/tomcat
[root@apache ~]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@apache ~]# netstat -anpt | grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      1458/java           
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      1458/java           
tcp6       0      0 :::8009                 :::*                    LISTEN      1458/java   

-Build in jenkins


-Error reporting solution

#Add permissions to the web host
[root@apache ~]# chmod a+x /usr/local/tomcat/bin/shutdown.sh 
[root@apache ~]# chmod a+x /usr/local/tomcat/bin/startup.sh 
[root@apache ~]# chmod -R o=rx /usr/local/tomcat/bin/
[root@apache ~]# chmod -R o=rwx  /usr/local/tomcat/logs/
[root@apache ~]# vim /usr/local/tomcat/bin/catalina.sh   #Add directly under comments
#!/bin/bash     
JAVA_HOME="/usr/local/java"
JRE_HOME="/usr/local/java/jre"
. . . . . . 
#Save exit

(5) Jenkins rollback

There are two ways for Jenkins rollback, namely, through the commit of gitlab warehouse_ ID to rollback and Jenkins version serial number to rollback

-Via commit_id to rollback

  • Gitlab can return to the previous state by submitting the first 12 digits of the id number
******(1)Using the above apache Project, so now web Install it on the host httpd
[root@apache ~]# yum -y install httpd
. . . . . . 
complete!
[root@apache ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@apache ~]# systemctl start httpd

(2) Install the gogs plug-in before doing this




Note: if the installation fails, you can download it manually and then upload it

(3) Configuring apache projects in jenkins





case $status  in
  rollback)
    echo "-----This operation is rollback,Version number is $version-----"
    git reset --hard $version
echo "-----Rollback complete,Packing-----"  
    ;;
  *)
  exit
esac
#It is found that the above status and version are introduced as variables. Use the case statement and take rollback as a condition to achieve the desired effect

[root@jenkins ~]# vim aaa.sh    #Script
#!/bin/bash
cd /root
scp /var/lib/jenkins/workspace/aaa/index.html root@192.168.100.204:/var/www/html/index.html
#/var/lib/jenkins/workspace / this directory is the library pulled from gitlab by jenkins. The following aaa is the project directory, and the warehouse files will be pulled to the corresponding project directory
[root@jenkins ~]# rm -rf test/   #Delete the original gitlab library and pull it again
[root@jenkins ~]# ll
 Total consumption 605960
-rwxr-xr-x  1 root root       144 7 January 17:14 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root   8842660 7 February 9:29 apache-maven-3.5.4-bin.tar.gz
-rwxr-xr-x  1 root root       274 7 February 19:05 bbb.sh
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
drwxr-xr-x  6 root root        99 7 February 9:29 maven
-rw-r--r--  1 root root 122904706 7 February 9:34 nexus-3.13.0-01-unix.tar.gz
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
drwxr-xr-x  3 root root        20 7 February 9:34 sonatype-work
[root@jenkins ~]# git clone git@192.168.100.202:root/test.git  #Pull the warehouse again
 Cloning to 'test'...
remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 57 (delta 1), reused 0 (delta 0)
In receiving object: 100% (57/57), 6.79 KiB | 0 bytes/s, done.
handle delta in: 100% (1/1), done.
[root@jenkins ~]# cd test/
[root@jenkins test]# git rm -rf *  #Use git rm -rf to delete all files in the previous warehouse
rm 'index.html'
rm 'test1/.classpath'
rm 'test1/.project'
rm 'test1/.settings/.jsdtscope'
rm 'test1/.settings/org.eclipse.jdt.core.prefs'
rm 'test1/.settings/org.eclipse.m2e.core.prefs'
rm 'test1/.settings/org.eclipse.wst.common.component'
rm 'test1/.settings/org.eclipse.wst.common.project.facet.core.xml'
rm 'test1/.settings/org.eclipse.wst.jsdt.ui.superType.container'
rm 'test1/.settings/org.eclipse.wst.jsdt.ui.superType.name'
rm 'test1/.settings/org.eclipse.wst.validation.prefs'
rm 'test1/pom.xml'
rm 'test1/src/main/resources/com/Time.java'
rm 'test1/src/main/webapp/WEB-INF/web.xml'
rm 'test1/src/main/webapp/index.jsp'
rm 'test1/target/classes/com/Time.class'
rm 'test1/target/classes/com/Time.java'
rm 'test1/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF'
rm 'test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.properties'
rm 'test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.xml'
rm 'time.java'
[root@jenkins test]# echo "aaaa1" > index.html   #Rewrite a document
[root@jenkins test]# git add index.html   #Add to staging area
[root@jenkins test]# git commit -m "aaa1"  #Add to local warehouse
[master eec82c4] aaa1
 21 files changed, 1 insertion(+), 263 deletions(-)
 delete mode 100644 test1/.classpath
 delete mode 100644 test1/.project
 delete mode 100644 test1/.settings/.jsdtscope
 delete mode 100644 test1/.settings/org.eclipse.jdt.core.prefs
 delete mode 100644 test1/.settings/org.eclipse.m2e.core.prefs
 delete mode 100644 test1/.settings/org.eclipse.wst.common.component
 delete mode 100644 test1/.settings/org.eclipse.wst.common.project.facet.core.xml
 delete mode 100644 test1/.settings/org.eclipse.wst.jsdt.ui.superType.container
 delete mode 100644 test1/.settings/org.eclipse.wst.jsdt.ui.superType.name
 delete mode 100644 test1/.settings/org.eclipse.wst.validation.prefs
 delete mode 100644 test1/pom.xml
 delete mode 100644 test1/src/main/resources/com/Time.java
 delete mode 100644 test1/src/main/webapp/WEB-INF/web.xml
 delete mode 100644 test1/src/main/webapp/index.jsp
 delete mode 100644 test1/target/classes/com/Time.class
 delete mode 100644 test1/target/classes/com/Time.java
 delete mode 100644 test1/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF
 delete mode 100644 test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.properties
 delete mode 100644 test1/target/m2e-wtp/web-resources/META-INF/maven/www.linuxfan.cn.new/test1/pom.xml
 delete mode 100644 time.java
[root@jenkins test]# git push origin master  #Push to remote warehouse
Counting objects: 5, done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (3/3), 234 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/test.git
   6783721..eec82c4  master -> master

After the configuration is completed, parameters will be taken when building the project

You can select options to build and click build

Visit the web host and successfully modify the page

******modify gitlab File content of remote warehouse
[root@jenkins ~]# cd test/
[root@jenkins test]# ll
 Total consumption 4
-rw-r--r-- 1 root root 6 7 April 14:46 index.html
[root@jenkins test]# echo "bbbbb1" > index.html   #Change to bbb1
[root@jenkins test]# git add index.html 
[root@jenkins test]# git commit -m "bbbb1" 
[master b20929a] bbbb1
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins test]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 233 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/test.git
   eec82c4..b20929a  master -> master

Build again

Test page pushed successfully

View the commit of the warehouse_ ID number

The id numbers on the right can be copied directly

Copy the id number of the previous warehouse version



-Rollback by the release version number of the project

By backing up the project during publishing, there will be a version number, which can be used for rollback

Modify the configuration of some jenkins projects and use commit in the above_ ID rollback is modified based on the experiment


case $status  in
  deploy)
    echo "-----Packaged backup before publishing-----"
echo "status:$status"
BACK_DIR=/var/lib/jenkins/workspace
    path="${BACK_DIR}/bak"   
    if [ -d $path ];
    then
        echo "The files is already  exists "
    else
        mkdir -p  $path
    fi
cd ${BACK_DIR}/${JOB_NAME}
#${JOB_NAME} = project name (test)
#${BUILD_NUMBER} = number of builds
    tar czf ${JOB_NAME}-${BUILD_NUMBER}.tar.gz *
    mv ${JOB_NAME}-${BUILD_NUMBER}.tar.gz ${BACK_DIR}/bak
scp ${WORKSPACE}/*  root@192.168.100.214:/var/www/html/index.html
    echo "Completin!"
;;
rollback)
BACK_DIR=/var/lib/jenkins/workspace
cd ${BACK_DIR}/bak
tar xf ${JOB_NAME}-${version}.tar.gz
    scp ${BACK_DIR}/bak/index.html root@192.168.100.214:/var/www/html/index.html
    rm -rf ${JOB_NAME}
;;
  *)
  exit
      ;;
esac

Modify the file content of the warehouse

[root@jenkins ~]# cd test/
[root@jenkins test]# ll
 Total consumption 4
-rw-r--r-- 1 root root 7 7 April 14:56 index.html
[root@jenkins test]# cat index.html 
bbbbb1
[root@jenkins test]# echo "cccccc" > index.html 
[root@jenkins test]# git add index.html 
[root@jenkins test]# git commit -m "add ccc"
[master e89845c] add ccc
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins test]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 233 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 2 (delta 0)
To git@192.168.100.202:root/test.git
   b20929a..e89845c  master -> master

Build first


******(1)You can see that the project has been packaged and backed up
[root@jenkins test]# cd /var/lib/jenkins/workspace/
[root@jenkins workspace]# 
[root@jenkins workspace]# ll
 Total consumption 0
drwxr-xr-x 3 root root 36 7 May 14:20 aaa
drwxr-xr-x 2 root root  6 7 May 14:20 aaa@tmp
drwxr-xr-x 2 root root 27 7 May 14:20 bak
drwxr-xr-x 3 root root 36 7 April 14:42 test
[root@jenkins workspace]# cd bak/
[root@jenkins bak]# ll
 Total consumption 4
-rw-r--r-- 1 root root 128 7 May 14:20 aaa-22.tar.gz   #This is the backup file

******(2)Modify the contents of the warehouse file again
[root@jenkins bak]# cd
[root@jenkins ~]# cd test/
[root@jenkins test]# echo "dddddd" > index.html 
[root@jenkins test]# git add index.html 
[root@jenkins test]# git commit -m "add ddd"
[master 5b934c3] add ddd
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins test]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 233 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/test.git
   e89845c..5b934c3  master -> master

Build again


Roll back this time. Roll back the previous item. The serial number of the previous item is 22



Release by version number!

(6) Jenkins add mail alarm

-Set administrator mailbox





-Configuration item





#notes
①Disable Extended Email Publisher: After checking, the email will not be sent. It depends on your own situation. If you want to debug something and don't want to send email, you can check this.
②Project Recipient List: Address of the addressee; Multiple recipient email addresses are separated by commas; If you want to use the global default configuration, you can use $DEFAULT_RECIPIENTS. 
③Project Reply-To List: Address of the person allowed to reply; If you want to use the default values in the system settings, you can use $DEFAULT_REPLYTO;
④Content Type: The type of mail document can be set HTML Equal format;
⑤Default Subject: The default subject is the mail header; Similarly, it can be used $DEFAULT_SUBJECT
⑥Default Content: Default email content; Here is the key;
⑦Attach Build Log: Whether the sent mail contains logs;
The following defaults are good, the last one Triggers Very critical;
If you don't change the last one, you can't receive the email. This is a big hole left by the official. You must add another one yourself Recipient List. 

#To be added in "add post build operation steps"
E-mail Notification
Editable Email Notification

At this point, you only need to send an email if there is an error building the project

The message received is like this. In fact, it is the output of the console during construction

(7) Jenkins remote management deployment node

Jenkins has three ways of remote management:

  1. Shell ssh (secret free)
  2. SSH Plugin
  3. ansible or saltstack

-Shell ssh method

  • During project construction, Jenkins actually uses the Execute Shell to directly operate the remote host, synchronize the code to the target host, and can operate the remote host

  • Next, directly modify the project configuration to achieve the effect

Here, delete the script first, and then directly write two lines of commands to modify the web page of the 204 web host

You can build directly without any options, because the script has been deleted

Successful build successful

The page was successfully modified and received an email because of the email alarm function above

-Method of SSH plugin

The second method is to use SSH plug-in, SSH plugin over ssh

Modify project configuration



Remove the public key of jenkins host from the web host

******(1)Delete key
[root@apache ~]# cd .ssh/
[root@apache .ssh]# ll
 Total consumption 4
-rw------- 1 root root 398 7 January 1 00:42 authorized_keys
[root@apache .ssh]# rm -rf *
[root@apache .ssh]# ll
 Total consumption 0

******(2)stay jenkins Host test
[root@jenkins ~]# ssh root@192.168.100.204
root@192.168.100.204's password:    #You need to enter a password to delete the key successfully

Click build on the jenkins host to test


The experiment using SSH plug-in is successful!

-Jenkins's method of cooperating with Ansible

******(1)stay jenkins Upper installation ansible
[root@jenkins ~]# ll
 Total consumption 605968
-rwxr-xr-x  1 root root       114 7 April 14:50 aaa.sh
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
drwxr-xr-x  3 root root      4096 7 May 15:39 ansible    #Upload ansible yum Library
-rw-r--r--  1 root root   8842660 7 February 9:29 apache-maven-3.5.4-bin.tar.gz
-rwxr-xr-x  1 root root       274 7 February 19:05 bbb.sh
-rw-r--r--  1 root root         6 7 May 15:03 index.html
-rw-r--r--  1 root root 181367942 6 June 30-22:11 jdk-8u91-linux-x64.tar.gz
-rw-r--r--  1 root root  66044542 6 June 30-22:16 jenkins-2.222.4-1.1.noarch.rpm
drwxr-xr-x  6 root root        99 7 February 9:29 maven
-rw-r--r--  1 root root 122904706 7 February 9:34 nexus-3.13.0-01-unix.tar.gz
-rw-r--r--  1 root root 241319636 6 June 30 23:49 plugins.tar.gz
drwxr-xr-x  3 root root        20 7 February 9:34 sonatype-work
drwxr-xr-x  3 root root        36 7 April 14:46 test
[root@jenkins ~]# vim /etc/yum.repos.d/centos.repo   #Modify yum file
[aaa]
name=aaa
baseurl=file:///mnt
enabled=1
gpgcheck=0
[ansible]
name=ansible
baseurl=file:///root/ansible
enabled=1
gpgcheck=0
#Save exit
[root@jenkins ~]# yum -y install ansible
. . . . . . 
complete!

******(2)test Ansible
[root@jenkins ~]# cd /etc/ansible/
[root@jenkins ansible]# > hosts 
[root@jenkins ansible]# vim hosts 
[web]
192.168.100.204
#Save exit
[root@jenkins ansible]# ssh-copy-id 192.168.100.204  #Upload the public key to the web host again
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.100.204's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.100.204'"
and check to make sure that only the key(s) you wanted were added.
[root@jenkins ansible]# ansible web -m ping  #Test ping module
192.168.100.204 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

******(3)Create a new test directory and test script
[root@jenkins ansible]# cd
[root@jenkins ~]# mkdir -p /data/ansible-playbook
[root@jenkins ~]# vim /data/ansible-playbook/jenkins.yml
---
- hosts: web
  remote_user: root
  gather_facts: no
  tasks:
  - name: Create a directory
    file: path=/root/abc state=directory
#Save exit
[root@jenkins ansible-playbook]# ansible-playbook -C jenkins.yml   #Test Scenario 

PLAY [web] ******************************************************************************************************************************

TASK [Create a directory] ***************************************************************************************************************************
changed: [192.168.100.204]

PLAY RECAP ******************************************************************************************************************************
192.168.100.204            : ok=1    changed=1    unreachable=0    failed=0   

Configure jenkins and install Ansible and Ansible Tower plug-ins. If the same download fails, download it manually

Create a new task to test



As like as two peas, the console is executed with the same output information as the script.

#Verify the effect on the web host
[root@apache ~]# ll 
Total consumption 186328
drwxr-xr-x  2 root root         6 7 June 00:01 abc   #Successfully created
-rw-------. 1 root root      1264 1 December 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root   9417469 7 March 2:08 apache-tomcat-8.5.16.tar.gz
-rw-r--r--  1 root root         6 7 May 23:30 index.html
-rw-r--r--  1 root root 181367942 7 March 2:02 jdk-8u91-linux-x64.tar.gz

Jenkins cooperated with Ansible successfully!!!

(8) Jenkins cluster

  • When the business volume gradually increases, the automatic construction and deployment of a single jenkins is obviously not flexible enough, so the jenkins cluster is introduced

  • jenkins cluster does not do load balancing and high availability like web server and mysql Cluster. jenkins cluster only increases work nodes and efficiency, and jenkins cluster does not need to install jenkins on additional hosts. It only needs to install jdk. However, for java projects, maven needs to be installed to use the cluster function

  • The plug-ins that need to be installed to use Jenkins cluster are ssh, ssh agent and ssh build agents

  • You need to synchronize the time with the Jenkins host before adding nodes

-Synchronize time for node host and jenkins

Add a host as Jenkins node: node1, ip: 192.168 100.205, two network cards and one bridge network card

******(1)stay Jenkins install ntpdate Service syncs time to alicloud
[root@jenkins ~]# yum -y install ntpdate
. . . . . . 
complete!
[root@jenkins ~]# ntpdate ntp1.aliyun.com
 6 Jul 17:21:03 ntpdate[1582]: adjust time server 120.25.115.20 offset -0.000628 sec
[root@jenkins ~]# cat .ssh/id_rsa   #Check the private key of jenkins host first
-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA3FIIZI24n+7xRc5lH3lmqJ9PPlQxd9eLIvBWtMaJxN3RW6X6
9bc0i5pXWrpY/MgcF777Uu1yiVLoeeIKoEMnKK/09WqX8nz0ix7QqHmVmM2wArn5
vaXpyggNFEpGME5DkFPR6Tsde6fOZwTusnaOl89JfFATar6Mu/7Tu5iLJXIoJ4/q
+poLJE8QqDpPbo8A0kN0Jn4S5nP+weMBklnJbxE2ckRsm/IN68nu/ten6TxDaatW
GsHqIjQMrXW/iGQVY4HSE9JNj1YIFXWMZ7aANCLpQmMKJU9SnIyZZXEpuOyB7Qy+
CfAIDxjvGV3+6CTPrzLWUYh5VEYy5UM23AJzoQIDAQABAoIBAQCcVtW/zNVLIDoz
Z89YM0SGuW149DOaL+h6pYNRimTuvty2JSTdQeDWg3IjXoKQ9csBirM62GXjTmLL
Qnx51s0t0RaJI9eo6cR6CCbWtlhScvrIJII0ckxg4a+uSzpdQtwsyV32G83DFoOf
lN9w4jIzRZOG1CfDIm4pZR2ITCnJKlRv+hL4gGDTEh3oFQHMJZwxA6qiwXqmRxJ+
jUNP9S2LHl2Zsz3QwTHCsdHUoJisLz348M9bjia5Kn7i/kJONdk3inxBTWhxX9sA
j/GDTO++H4bklRgtJuB+hL7wpYuL5T/dK0VShtWTZVQQZTycPNsmBEf6XYYx37K2
07ZRyCjBAoGBAPNBuWYP1RgIShVBaQsEXjWdP+4orN9cDvvueMKroCkg7QGv2m0v
SDmpKWkhLNurnd3QoqyW1Q1w0+iw1QOKmjmHxo95tu3ieNAQ5TOK8fAiEA996zpC
DiADVjo0qMrUncf1taWCytmhSu0kPDOVsniy8BMlJdzTlgL5QJ+G55NJAoGBAOfc
trZ9b9gI0bBG2Jx9khmY54RZReaJF2q21eR75YTB9myh9AWZ0a+7KukJoay7hk6b
2n3tKx05RV1xbXYM7QKQ2f6Xbvcc8gZbW6viWkrst3ic57stZ2F7Bdg6Q8gH4Wy8
vExvAutE8A7GMlHOjKfx6NByhj8I5jJoPWA84wWZAoGBAOn+AdokRqXsa17Q4btg
Q0MtdS6hX9EnW56JgQ45S0Hk0zb5MpIuljoo95WR/PV9usiBBcog+OYJ7z14xB64
96t5aM3glc90NyRl7CPNdva3vV05kckjezTeINayit848GFFgKqqCniXx1wfq+0v
BVNHRNoVtLsbqMRJgRkzCpVJAoGBAJDjCkYFInPUJvxJeShpC3f1ZY79nkGOH1Tv
Bb/ucCFCd3OwzGUHltGCWzEedKoU0Lukhqc0IkHzvNXEgItJTn3Q+HgwNwYyT0cL
pcBGBPhW7dtv6Rq9J6Hxl1hkKGECE6q4Uc5NfHx6KqyAy4b1VE3KTBqdxifzrq2H
F/tgcNjZAoGBAMoF+HmoroSp0obSTn/CHK15wF7aZBB6clA9d0Ezz63PAd2yLMe3
dMfTpRHVKcMiDrCeYVj/XNjwhA+GXexq4h1WgGBBHuiRwiFP67p5p4Jlad7t6qP9
pnh+oFpRclAmJEJwkj45u5J8oUFeQ7AztILU3qWFMpaYoYLYSdAbkdQR
-----END RSA PRIVATE KEY-----
[root@jenkins ~]# ssh-copy-id 192.168.100.205  #Transfer public key to node1 host
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.205 (192.168.100.205)' can't be established.
ECDSA key fingerprint is SHA256:VhTZ5YxS5af2rHtfCvyc6ehXh3PD2A8KY2MyE6rHjiU.
ECDSA key fingerprint is MD5:e8:41:d2:8a:7e:e9:a9:47:a3:f0:29:be:e9:6d:df:51.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.100.205's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.100.205'"
and check to make sure that only the key(s) you wanted were added.
[root@jenkins local]# scp -r java root@192.168.100.205:/usr/local/  #Transfer java to node1

******(2)stay node1 Host installation ntpdate Also synchronize time with alicloud
[root@Centos7 ~]# hostnamectl set-hostname node1
[root@Centos7 ~]# su
[root@node1 ~]# yum -y install ntpdate
. . . . . . 
complete!
[root@node1 ~]# ntpdate ntp1.aliyun.com
 6 Jul 17:20:44 ntpdate[1162]: adjust time server 120.25.115.20 offset -0.001442 sec
[root@node1 ~]# mkdir jenkins   #Create node directory
[root@node1 ~]# echo """
> JAVA_HOME=/usr/local/java 
> PATH=$JAVA_HOME/bin:$PATH
> CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar 
> export PATH JAVA_HOME CLASSPATH """ >> /etc/profile   #Add environment variables
[root@node1 ~]# source /etc/profile  #Make environment variables effective
[root@node1 ~]# java -version  #View java version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

-Configure jenkins









Successfully added node!, You can see the hardware address of each node

-Assign tasks to nodes



You can delete the configuration of the previous experiment and leave the git pull warehouse file

#When selecting the running node of the restricted project, you can use regular to filter, such as node *
#After the operation node of the restricted project is selected, the following build operations, scripts, etc. must exist in the specified node
******to write node1 In node aaa.sh script
[root@node1 ~]# vim aaa.sh
#!/bin/bash
scp /root/jenkins/workspace/aaa/index.html root@192.168.100.204:/var/www/html/index.html
#Save exit
[root@node1 ~]# chmod +x aaa.sh 
[root@node1 ~]# ssh-keygen -t rsa   #Generate key
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:mcAmNjk5OUzIQK4FvTJYZdTa4l3RtxWk3LOf1n0lyU4 root@node1
The key's randomart image is:
+---[RSA 2048]----+
|+= +=.   .   .o. |
|..=+ =. . ...o.  |
|.o..%o+  . .ooo  |
|=...oB...o  .. + |
|.o . o .S     E .|
|    . .      o o=|
|              .o=|
|              . .|
|                 |
+----[SHA256]-----+
[root@node1 ~]# ssh-copy-id 192.168.100.204  #Transfer public key to web host
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.100.204's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.100.204'"
and check to make sure that only the key(s) you wanted were added.

******View the file contents of the warehouse
[root@jenkins ~]# rm -rf test/
[root@jenkins ~]# git clone git@192.168.100.202:root/test.git
 Cloning to 'test'...
remote: Enumerating objects: 67, done.
remote: Counting objects: 100% (67/67), done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 67 (delta 1), reused 60 (delta 1)
In receiving object: 100% (67/67), 7.51 KiB | 0 bytes/s, done.
handle delta in: 100% (1/1), done.
[root@jenkins ~]# cd test/
[root@jenkins test]# cat index.html 
dddddd

-Testing

First look at the page before 204





(9) jenkins pipeline

In the process of Pipeline task project construction, the detailed information of each construction stage will be presented through Pipeline

-Create pipeline task


-Configure pipelined tasks and test

pipeline {
    agent any
    stages {
        stage("Hellp") {
           steps {
               echo "Hellp world"
           }  
         }
    }
}
#It is a bit like a script, but it is built using the special syntax of the pipeline pipeline. Stages can contain multiple stages. The characters in stage() are like the name ID in the script, and echo "help world" is the command executed by each name ID


  • You can see that the phase view is automatically generated after execution

  • View console output information

-Storing pipeline files through gitlab

  • First, create a warehouse for storing pipeline on gitlab


#Clone the warehouse just created by gitlab on the jenkins host
[root@jenkins ~]# git config --global user.name "Administrator"
[root@jenkins ~]# git config --global user.email "1248873545@qq.com"
[root@jenkins ~]# git clone git@192.168.100.202:root/pipeline-test.git
 Cloning to 'pipeline-test'...
warning: You seem to have cloned an empty version library.
[root@jenkins ~]# cd pipeline-test/
[root@jenkins pipeline-test]# git --bare init
 Initialize empty Git Version library at /root/pipeline-test/
[root@jenkins pipeline-test]# vim jenkinsfile
pipeline {
   agent any
   stages {
      stage("hello") {
         steps {
         echo "hello world"
         }
      }
   }
}
#Save exit
[root@jenkins pipeline-test]# git add jenkinsfile    #Add to staging area
[root@jenkins pipeline-test]# git commit -m "pipeline"  #Add to local warehouse
[master(Root (submit) da10bee] pipeline
 1 file changed, 8 insertions(+)
 create mode 100644 jenkinsfile
[root@jenkins pipeline-test]# git push -u origin master   #Push to remote warehouse
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/pipeline-test.git
 * [new branch]      master -> master
 branch master Set to track from origin Remote branch of master. 
  • Configure for pipeline projects




  • View output information

-Generating git pipeline scripts using Pipeline Syntax

Some linux commands are not supported in the pipeline syntax. You can use the pipeline's own tools to translate and automatically generate the corresponding syntax


-Using pipeline publishing to pull projects from gitlab for publishing

  • You can use Pipeline Syntax to create the statement to be used



#Generated syntax
git credentialsId: 'ae9ffc3a-85f4-49a1-8439-b602ce07f6ce', url: 'git@192.168.100.202:root/test.git'

sh label: '', script: 'scp /var/lib/jenkins/workspace/test/index.html root@192.168.100.204'

#Write jenkinsfile file
[root@jenkins pipeline-test]# vim jenkinsfile   #The warehouse pulled down by the pipeline will still be pulled to the workspace directory, and the directory name is still the project name
pipeline {
   agent any
   stages {
      stage("git") {
         steps {
            git credentialsId: 'ae9ffc3a-85f4-49a1-8439-b602ce07f6ce', url: 'git@192.168.100.202:root/test.git'
         }
      }
      stage("scp") {
         steps {            sh label: '', script: 'scp /var/lib/jenkins/workspace/pipeline-test/index.html root@192.168.100.204:/var/www/html/index.html'
         }
      }
   }  
}        
#Save exit
[root@jenkins pipeline-test]# git add jenkinsfile 
[root@jenkins pipeline-test]# git commit -m "pip1"
[master 4479c93] pip1
 1 file changed, 17 insertions(+), 2 deletions(-)
[root@jenkins pipeline-test]# git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 456 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/pipeline-test.git
   9a852c9..4479c93  master -> master
 branch master Set to track from origin Remote branch of master. 


#Rewrite the contents of the file in the gitlab library
[root@jenkins ~]# rm -rf test/
[root@jenkins ~]# git clone git@192.168.100.202:root/test.git
 Cloning to 'test'...
remote: Enumerating objects: 67, done.
remote: Counting objects: 100% (67/67), done.
remote: Compressing objects: 100% (38/38), done.
remote: Total 67 (delta 1), reused 60 (delta 1)
In receiving object: 100% (67/67), 7.51 KiB | 0 bytes/s, done.
handle delta in: 100% (1/1), done.
[root@jenkins ~]# cd test/
[root@jenkins test]# ll
 Total consumption 4
-rw-r--r-- 1 root root 7 7 July 10:02 index.html
[root@jenkins test]# cat index.html 
dddddd
[root@jenkins test]# echo "<h1>pipeline</h1>" > index.html 
[root@jenkins test]# cat index.html 
<h1>pipeline</h1>
[root@jenkins test]# git add index.html 
[root@jenkins test]# git commit -m "add 1"
[master 2d6b532] add 1
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins test]# git push -u origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 252 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.100.202:root/test.git
   5b934c3..2d6b532  master -> master
 branch master Set to track from origin Remote branch of master. 
  • Build project



(10) Deploy Blue Ocean


You can wait until the installation is completed, or you can manually download and then upload files. Manual upload needs to transfer more than 20 files

Topics: Operation & Maintenance jenkins ci CICD