Jenkins to bring html code online and rollback

Keywords: Linux GitLab jenkins git Nginx

1. Case Environment
II. Deployment of Gitlab
1) Deploy Gitlab
2) Configure Secret-Free Login
3) Test Secret Login
3. Deployment of Jenkins
4. Jenkins Integrated Gitlab
V. Deployment of Nginx
6. Jenkins Implement Code Automatically Online
7. Jenkins implements automatic code deployment, fallback and duplicate construction

1. Case Environment

system IP Address host name Services Running
Centos 7 192.168.1.6 gitlab gitlab
Centos 7 192.168.1.7 Jenkins jenkins
Centos 7 192.168.1.8 Nginx nginx

II. Deployment of Gitlab

1) Deploy Gitlab

[root@gitlab ~]#  yum -y install  epel-release curl openssh-server openssh-clients postfix cronie policycoreutils-python patch
#Installation Dependencies
[root@gitlab ~]#  wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
#There are no special restrictions on the gitlab version
[root@gitlab ~]# yum -y localinstall gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb                #Easy access using URL paths
external_url 'http://192.168.1.6'
#Download Chinese Patch Pack
[root@gitlab ~]# head -1 /opt/gitlab/version-manifest.txt           #View gitlab version
gitlab-ce 12.3.5
[root@gitlab ~]# git clone https://gitlab.com/xhang/gitlab.git -b v12.3.5-zh
[root@gitlab ~]# cd gitlab/
[root@gitlab gitlab]#  git diff v12.3.5 v12.3.5-zh > /root/v12.3.5-zh.dif
[root@gitlab gitlab]# patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 <  ../v12.3.5-zh.diff
 #Import the just diff file into gitlab as a patch
#Skip all the way back during the command execution
[root@gitlab ~]# gitlab-ctl  start                 # Start gitlab
[root@gitlab ~]# gitlab-ctl reconfigure      #Reconfigure gitlab

web Page Access Test



You can see that the website page of gitlab has been sinicized!

2) Configure Secret-Free Login

[root@gitlab ~]# ssh-keygen 
[root@gitlab ~]#  cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuWnEReFVhIiODuTO/NzdjuvPMY0tQ89NuYGtMxDJJAXnbChK8dbe0Iu4GNHY9zeSP0OaYJvVV9Nu/qtcZqgg+zKgLohdoI5QKXWcZ1JT/kNpIbnVnUYE0/AYZ8nMbjNiUsSD5BaDy+t9uuRxh+QCOrZDkYZw1EZioDpDoSR1XzrOac69QIag2XUU04rmWkR9/fbfwGzusTzfO2thk3vcZn+DzLqJqYy9ufkZeTGlqJnVC6//caakMtY475NSUKnGT+YoRsStXXI/9CVrpY8cPZ5eiFs34xGZWU1uhsx9UQ50pNeA7iqM6CIKpb4MPtqKDLIb83Bztz1VsfBJRMxer root@gitlab
#Locally generated key pairs for ssh login


3) Test Secret Login



[root@gitlab ~]# git clone git@192.168.1.6:root/web-demo.git
#Confirm that you do not need to enter a password

3. Deployment of Jenkins

[root@jenkins ~]# wget https://mirrors-i.tuna.tsinghua.edu.cn/jenkins/redhat/jenkins-2.172-1.1.noarch.rpm
[root@jenkins ~]# yum -y localinstall jenkins-2.172-1.1.noarch.rpm
#Get the Jenkins package and install it
[root@jenkins ~]# systemctl start jenkins            #Start Jenkins

web Page Access






Since you just skipped the plug-in installation, you need to install it manually, download the plug-in package I provided, and download the link: https://pan.baidu.com/s/16fxH1J0WO6tW1bhPwmd60w
Extraction Code: 432n

[root@jenkins ~]# rm -rf /var/lib/jenkins/plugins/           #Delete the plug-in directory generated by the installation
[root@jenkins ~]# tar zxf jenkins-plugins.tar.gz -C /var/lib/jenkins/
#Unzip the plug-in package to the specified location
[root@jenkins ~]# sed -i 's/http:\/\/updates.jenkinsci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' /var/lib/jenkins/updates/default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' /var/lib/jenkins/updates/default.json
#Change the path of the jenkins download Plugin to domestic source
[root@jenkins ~]# systemctl restart jenkins          #Restart Jenkins

Visit the web page again:


When you visit Jenkins again, you can see that it has become Chinese because the Chinese plug-in is included in the plug-in!

4. Jenkins Integrated Gitlab

[root@jenkins ~]# ssh-keygen     #Jenkins service generates key pairs
[root@jenkins ~]# cat ~/.ssh/id_rsa.pub      #View public key information
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbcFk7tkBes+S6BIVLxzLVj8X7bThDYscpY6uBeauHp+tPTL6CRVQDOlUcMRsyaX7o+a26iPd+yhJUaB+qT4iW1cVjhEiuKiR2NJSJdnEqccgcXWPJzIe+Owj76ofBtIhKqLGzwnYj6Lj0KmjUhn7tASu1bZ8hWubJ2ZQUottOW8KqjvHIb7q8OsGty9tsLVV3gD1XF1cE1s9rXY7XUFxDwtu1kfiA2XhSyJ3EmTWFlFlvaqzhQDBELWSVBz/4CihAfIp2eW7nx8TPXG0d0j+OwQYcccsarwkjHuc2IYRv4gVAhtRyXj8VQ7+LBsjYjfIMBTw4CFwQSTfZnWaa6Fcf root@jenkins

Store public key information on gitlab:

[root@jenkins ~]# cat ~/.ssh/id_rsa
#To view the private key information for the Jenkins server, you need to store the private key information on the Jenkins web page
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA23BZO7ZAXrPkugSFS8cy1Y/F+204Q2LHKWOrgXmrh6frT0y+
gkVUAzpVHDEbMml+6Pmtuoj3fsoSVGgfqk+IltXFY4RIriokdjSUiXZxKnHIHF1j
ycyHvjsI++qHwbSISqixs8J2I+i49Cpo1IZ+7QErtW2fIVrmydmUFKLbTlvCqo7x
yG+6vDrBrcvbbC1Vd4A9VxdXBNbPa12O11BcQ8LbtZH4gNl4UsidxJk1hZRZb2qs
4UAwRC1klQc/+AooQHyKdnlu58fEz1xtHdI/jsEGHHHLGq8JIx7nNiGEb+IFQIbU
cl4/FUO/iwbI2I3yDAU8OAhcEEk32Z1mmuhXHwIDAQABAoIBAQDXcb6qeY1JbaWr
XLTiaXjnZHklGV6cCmcvo8PHIwXd8md9PAPvMtIBOUxGmY/yNtF7O2fuNHuJ6CuG
45PzSpL4RHm/8y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zKSLq52oOQtYMZ/iIR
P6KH210ZgZjuJHQOUqeA954epRnbIosgrWz0G7qNjrpoHZkPcWXxmQ==
-----END RSA PRIVATE KEY-----






Install the plug-ins needed for jenkins to integrate gitlab, download the link: https://pan.baidu.com/s/1p_jh8X3ma46SEV_tKLKYbw
Extraction Code: zoej
Install the plug-in, as shown in the figure:



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

Tsinghua University Jenkins Source: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

The gitlab server submits code to gitlab to test the pull code:

[root@gitlab ~]# cd web-demo/
[root@gitlab web-demo]# git config --global user.email "you@example.com"
[root@gitlab web-demo]# git config --global user.name "Your Name"
[root@gitlab web-demo]# echo "123" >> 123.txt
[root@gitlab web-demo]# git add .
[root@gitlab web-demo]# git commit -m '123'
[root@gitlab web-demo]# git push origin master

The Jenkins server is built:




Build successful!

V. Deployment of Nginx

Use Nginx to simulate the online environment!

[root@nginx ~]# yum install -y gcc gcc- c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
#Install some necessary software as it is the system that minimizes installation
[root@nginx ~]# vim /etc/yum.repos.d/nginx.repo
#Configure the yum source for nginx, and Nginx officially has the appropriate configuration information
[nginx]
name=nginx_repo 
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#The yum source is installed by default with the latest and stable version of nginx
[root@nginx ~]# yum -y install nginx              #Install nginx
#There is no requirement for nginx version, just for access effect
[root@nginx ~]# rm -rf /etc/nginx/conf.d/default.conf        
#Delete default profile
[root@nginx ~]# vim /etc/nginx/conf.d/server.conf
#New Profile
server {
        listen 80;

        location / {
                root /code/web;
                index index.html index.htm;
        }
}
[root@nginx ~]# mkdir -p /code/web
[root@nginx ~]# nginx -t
[root@nginx ~]# systemctl start nginx

6. Jenkins Implement Code Automatically Online

1) The Jenkins service writes online scripts

[root@jenkins ~]#mkdir /scripts && cd /scripts/
[root@jenkins scripts]# vim html_deploy.sh 
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H-%M-%S)
web_server="192.168.1.8"
Sdir=/opt
Ddir=/code

#1) Enter the project directory, package the contents, ${WORKSPACE} is Jenkins'built-in variable, representing the absolute path to building the directory
get_code(){
        cd ${WORKSPACE} && \
        tar zcf ${Sdir}/web-${DATE}.tar.gz ./*
}
#2) Copy content to web page directory via scp
scp_web_server(){
for hosts in ${web_server}
do
        scp ${Sdir}/web-${DATE}.tar.gz root@${hosts}:/opt
        ssh root@${hosts} "mkdir -p ${Ddir}/web-${DATE} && \
                        tar zxf ${Sdir}/web-${DATE}.tar.gz -C ${Ddir}/web-${DATE}
                        rm -rf ${Ddir}/web && \
                        ln -s ${Ddir}/web-${DATE} ${Ddir}/web"
done
}

deploy(){
        get_code
        scp_web_server
}

        deploy
[root@jenkins scripts]# chmod +x html_deploy.sh 
[root@jenkins scripts]# ps -ef | grep jenkins
#You can see that the running user of jenkins is jenkins
jenkins   58626      1  1 11:23 ?        00:00:39 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkinsjenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
root      61336   2046  0 12:06 pts/0    00:00:00 grep --color=auto jenkins
[root@jenkins scripts]# vim /etc/sysconfig/jenkins 
JENKINS_USER="root"
#To prevent permission issues, change the running user of jenkins directly to root
[root@jenkins scripts]# systemctl restart jenkins
#Restart the jenkins service
[root@jenkins scripts]# ssh-copy-id root@192.168.1.8
#Configure Jenkins to log on to the nginx server confidentially

2) git server edits web page code and uploads:

[root@gitlab web-demo]# echo "lvzhenjiang" >> index.html
[root@gitlab web-demo]# git add .
[root@gitlab web-demo]# git commit -m "first"
[root@gitlab web-demo]# git push origin master








Console output when interested in being able to build!

Visit nginx to test!

Confirm that this is the code information just submitted by the gitlab server!

[root@nginx ~]# ll /code                    #View the implementation of the web page directory
#You can see that the real web directory is another directory that links to the past

//Total dosage 0
lrwxrwxrwx 1 root root 29 2 February 2519:37 web -> /code/web-2020-02-25-19-37-13
drwxr-xr-x 2 root root 39 2 February 2519:37 web-2020-02-25-19-37-13

This way of publishing still has a lot to improve, such as fallback, and then use tag to publish!

7. Jenkins implements automatic code deployment, fallback and duplicate construction

Get your project tag-enabled online so that we can distinguish between them!

1) The git server creates several tag tags and uploads them to gitlab:

[root@gitlab ~]# cd web-demo/
[root@gitlab web-demo]# echo "<h1>lvzhenjiang-version-v1.1</h1>" > index.html
[root@gitlab web-demo]# git add .
[root@gitlab web-demo]# git commit -m "v1.1"
[root@gitlab web-demo]# git push origin master
[root@gitlab web-demo]# git tag -a "v1.1" -m "v1.1"
[root@gitlab web-demo]# git push origin v1.1
[root@gitlab web-demo]# echo "<h1>lvzhenjiang-version-v1.2</h1>" > index.html
[root@gitlab web-demo]# git add .
[root@gitlab web-demo]# git commit -m "v1.2"
[root@gitlab web-demo]# git push origin master
[root@gitlab web-demo]# git tag -a "v1.2" -m "v1.2"
[root@gitlab web-demo]# git push origin v1.2
[root@gitlab web-demo]# echo "<h1>lvzhenjiang-version-v1.3</h1>" > index.html
[root@gitlab web-demo]# git add .
[root@gitlab web-demo]# git commit -m "v1.3"
[root@gitlab web-demo]# git push origin master
[root@gitlab web-demo]# git tag -a "v1.3" -m "v1.3"
[root@gitlab web-demo]# git push origin v1.3

2) Jenkins Server Install Plug-ins and Configure:

In this way, you need to install the plug-in: Git Parameter.

Installation method: System Management - > Plug-in Management - > Optional Plug-ins - Search Git Parameter - > Install directly!

[root@jenkins ~]# systemctl restart jenkins
#After installation, you need to restart Jenkins!
[root@jenkins ~]# cd /scripts/
[root@jenkins scripts]# vim html_deploy_tag.sh      #Optimize scripts
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H-%M-%S)
web_server="192.168.1.8"
Sdir=/opt
Ddir=/code
Name=${DATE}-${git_version}                 #${git_version} is a variable defined in the jenkins interface

#1) Enter the project catalog and package the contents
#${WORKSPACE} is Jenkins'built-in variable that represents the absolute path to building the directory
get_code(){
        cd ${WORKSPACE} && \
        tar zcf ${Sdir}/web-${Name}.tar.gz ./*
}
#2) Copy content to web page directory via scp
scp_web_server(){
for hosts in ${web_server}
do
        scp ${Sdir}/web-${Name}.tar.gz root@${hosts}:/opt
        ssh root@${hosts} "mkdir -p ${Ddir}/web-${Name} && \
                        tar zxf ${Sdir}/web-${Name}.tar.gz -C ${Ddir}/web-${Name}
                        rm -rf ${Ddir}/web && \
                        ln -s ${Ddir}/web-${Name} ${Ddir}/web"
done
}

deploy(){
        get_code
        scp_web_server
}

        deploy
[root@jenkins scripts]# chmod +x html_deploy_tag.sh 

Next:







Access nginx:

[root@nginx ~]# ll /code     #This gives you a clear idea of which version was built
//Total dosage 0
lrwxrwxrwx 1 root root 34 2 February 2520:21 web -> /code/web-2020-02-25-20-21-18-v1.3
drwxr-xr-x 2 root root 39 2 February 2519:37 web-2020-02-25-19-37-13
drwxr-xr-x 2 root root 39 2 February 2520:21 web-2020-02-25-20-21-18-v1.3

Deploy to this.The problem of fallback is still not solved!For example, deploying v1.3 twice will result in two v1.3 directories!

3) Solve the problem of version fallback and duplicate build

[root@jenkins scripts]# vim html_deploy_tag_rollback.sh     #Optimize scripts to support fallback
#!/bin/bash
DATE=$(date +%Y-%m-%d-%H-%M-%S)
web_server="192.168.1.8"
Sdir=/opt
Ddir=/code
Name=${DATE}-${git_version}                 #${git_version} is a variable defined in the jenkins interface

#1) Enter the project catalog and package the contents
#${WORKSPACE} is Jenkins'built-in variable that represents the absolute path to building the directory
get_code(){
        cd ${WORKSPACE} && \
        tar zcf ${Sdir}/web-${Name}.tar.gz ./*
}
#2) Copy content to web page directory via scp
scp_web_server(){
for hosts in ${web_server}
do
        scp ${Sdir}/web-${Name}.tar.gz root@${hosts}:/opt
        ssh root@${hosts} "mkdir -p ${Ddir}/web-${Name} && \
                        tar zxf ${Sdir}/web-${Name}.tar.gz -C ${Ddir}/web-${Name}
                        rm -rf ${Ddir}/web && \
                        ln -s ${Ddir}/web-${Name} ${Ddir}/web"
done
}

rollback(){
back_file=$(ssh root@${web_server} "find /code/ -maxdepth 1 -type d -name "web-*-${git_version}"")
for hosts in ${web_server}
do
        ssh root@${hosts} "rm -rf ${Ddir}/web && \
           ln -s ${back_file} ${Ddir}/web"
done
}

deploy(){
        get_code
        scp_web_server
}
#Determine whether the version of commit has been deployed before, deploy if not, and prompt to have been deployed and exit if so
#If it is a fallback, it is not restricted
if [ ${deploy_env} == "deploy" ];then
        if [ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ];then
                echo "You have already deployed this ${git_version}Edition"
                exit 1
        else
                deploy
        fi

elif  [ ${deploy_env} == "rollback" ];then
        rollback
fi
[root@jenkins scripts]# chmod +x html_deploy_tag_rollback.sh 

Next, configure:




Test:

web page:

Perform a fallback operation:

Console output when interested in being able to build!

web page:

[root@nginx ~]# ll /code            #You can see that the directory for v1.3 was created only once
//Total dosage 0
lrwxrwxrwx 1 root root 34 2 February 2520:46 web -> /code/web-2020-02-25-20-21-18-v1.3
drwxr-xr-x 2 root root 39 2 February 2519:37 web-2020-02-25-19-37-13
drwxr-xr-x 2 root root 39 2 February 2520:21 web-2020-02-25-20-21-18-v1.3
drwxr-xr-x 2 root root 39 2 February 2520:46 web-2020-02-25-20-46-15-v1.1

Repeat build, fallback problem solving!

- To the end of this article, thank you for reading -----

Posted by david-remone123 on Tue, 25 Feb 2020 08:29:14 -0800