Jenkins bis --- Jenkins build and deploy distributed trigger

Real one: to build a trigger based on Jenkins

Construction of trigger Introduction

Construction of the flip-flop (some people actually called the hook for a callback after the HTTP developers submit code to gitlab can trigger the automatic execution of code jenkins build operation.

The following is a new development branch, only when developers submit code to developers develop branch will trigger the code to build, and the code submitted to the main branch will not automatically build, operation and maintenance personnel need to manually deploy code to production.

1. Create develop branches in Gitlab

 1, create a branch within web1 develop projects created on the page Gitlab

2, start creating develop branch

 2, is mounted on the plug-in and configure Jenkins

1, pages in jenkins jenkins ----> System Management ----> Plugin Manager, install gitlab related plug-ins.

 

2, modify jenkins login authentication methods: System Management ----> Global security configuration changes are complete, need to restart jenkins services on linux system: systemctl restart jenkins, jenkins due consuming memory, 8G start production in the least.

Note: https: //jenkins.io/security/advisory/2018-05-09/#SECURITY-263

  • User login authentication instead can do anything
  • Cancel CSRF protection
  • Gitlab Hook Plugin stored in plain text and a display token GitLab API

3. Create a page on the Jenkins project develop job can also be used prior to the linux-jobs program, in jenkins Home ---> New task to create a new project.

 

4, create a source code management, pathetic gitlab host code and develop branch

 5, Jenkins trigger member configured to generate a token from the value defined

 (1) first randomly generated 12-bit random number

[root@gitlab ~]# openssl rand -hex 12
046e1cb057bce8073a7eddca

(2) filling the generated random number to trigger jenkins specified configuration, a first set of tests to execute shell commands

 

6, then filtering it before custom token values, you can see at a designated /var/lib/jenkins/jobs/linux-job1-develop/config.xml file

[root@jenkins ~]# grep 046e1cb057bce8073a7eddca  /var/lib/jenkins/  -R
grep: /var/lib/jenkins/jobs/linux-jobs/builds/lastUnstableBuild: No such file or directory
grep: /var/lib/jenkins/jobs/linux-job1-develop/builds/lastSuccessfulBuild: No such file or directory
grep: /var/lib/jenkins/jobs/linux-job1-develop/builds/lastStableBuild: No such file or directory
grep: /var/lib/jenkins/jobs/linux-job1-develop/builds/lastFailedBuild: No such file or directory
grep: /var/lib/jenkins/jobs/linux-job1-develop/builds/lastUnstableBuild: No such file or directory
grep: /var/lib/jenkins/jobs/linux-job1-develop/builds/lastUnsuccessfulBuild: No such file or directory
/var/lib/jenkins/jobs/linux-job1-develop/config.xml:  <authToken>046e1cb057bce8073a7eddca</authToken>

 7, execute commands on the host jenkins test, or visit on the web page and see if the job normally performed at jenkins console, then blue, indicating normal operation, if it is red, indicating fail

[root@jenkins ~]# curl http://192.168.7.101:8080/job/linux-job1-develop/build?token=046e1cb057bce8073a7eddca

3, create a system hook on gitlab page

1, create a hook on the page gitlab

 2, hook-created test, return to 201, indicating the system hooks created no problem

 

 3, the return value is HTTP 201, Description Normal

4, the start code Gitlab test taken Jenkins pulled to the backend server 

1, first pulling gitlab jenkins shell script code added to the website under linux-job1-develop the project, back-end server is 192.168.7.104, stored under / data / tomcat / tomcat_appdir directory is a compressed file, / data / tomcat / under tomcat_webdir / myapp directory ask stored access code:

cd /var/lib/jenkins/workspace/linux-job1-develop
tar cf code.tar.gz index.html
scp code.tar.gz  [email protected]:/data/tomcat/tomcat_appdir/ 
ssh [email protected]  "/apps/tomcat/bin/shutdown.sh && rm -rf /data/tomcat/tomcat_webdir/myapp/*  && cd /data/tomcat/tomcat_appdir/ && tar xf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"
ssh [email protected]  "/apps/tomcat/bin/startup.sh"

 2, the first host will develop branch gitlab pulling down the code modification, and from there to the warehouse gitlab

[Root @ gitlab ~] # git clone -b develop http://192.168.7.100/linux_gitlab/web1.git # -b specifies pulling branch code clone http address to submit code, but the code is git clone It can not be submitted gitlab warehouse
[Root @ gitlab web1] # cd web1 / # pull switch to the directory code
[Root @ gitlab web1] # cat index.html # code file to be modified
linux web 192.168.7.100
linux web 192.168.7.100
[Root @ gitlab web1] # when git config --global user.name "root" # submit code the first time, you need to specify the login user name, login may not be required to submit later
[Root @ gitlab web1] # git config --global user.email [email protected] # first need to specify the e-mail address
[Root @ gitlab web1] # git add index.html # add code
[Root @ gitlab web1] # git commit -m "v2" # code submitted on gitlab warehouse
[Root @ gitlab web1] # git push # code to be uploaded on gitlab
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Username for 'http://192.168.7.100': root
Password for 'http://[email protected]': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 254 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for develop, visit:
remote:   http://192.168.7.100/linux_gitlab/web1/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote: 
To http://192.168.7.100/linux_gitlab/web1.git
   429d346..1b28d52 develop -> develop # start cloning is develop branch, the default is to develop branch submitted

3, look at the code at this time the information reached the develop branch on gitlab page

4, when we pass code to be gitlab, this time jenkins will automatically pull gitlab code on the website, open jenkins console, you can see all jenkins shell script is finished

 5, at this time keepalived access VIP address 192.168.7.248/myapp, this time to access the contents of the code jenkins pulling the gitlab

 Construction of the association item (operates on demand)

A plurality of interrelated job, the job need to perform a plurality of scenes, for example, the following: the linux-job1-develop as a test environment, linux-jobs as the production environment, when the test environment is executed successfully, it will pull the code directly into the production environment

1, the operation after the build configuration, when finished linux-job1-develop the project, the project will be executed linx-jobs added, and the command executed will be sent to the mailbox that you added in.

 2, began to test the effect, after the implementation of the linux-job1-develop the project is successful, it will build a custom linux-jobs program

 3, this time back-end server to access web pages: 192.168.7.248/myapp, access is pulled linux-jobs content code

 

Distributed Jenkins

1, Jenkins distributed introduction

Among the many Job scenario, a single jenkins master while executing code clone, compilation, packaging and building performance bottlenecks may occur which will affect the deployment of code efficiency, the official jenkins build distributed, the number of job dispersed run into different jenkins slave node, a substantial increase in parallel job processing capabilities.

2, installation and deployment jenkins-slave servers

1, install and configure jenkins service: For details, see: https: //www.cnblogs.com/struggle-1216/p/12387377.html

Jenkins Official Download: https://jenkins.io/zh/ 

 Download the official JDK Address: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

[root@jenkins-slave src]# yum install jenkins-2.164.3-1.1.noarch.rpm -y

# Vim / etc / sysconfig / jenkins # modify configuration files jenkins
JENKINS_USER = "root" # will start as root jenkins

JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -server -Xms1g -Xmx1g -Xss512k -Xmn1g \
-XX:CMSInitiatingOccupancyFraction=65 \
-XX:+UseFastAccessorMethods \
-XX:+AggressiveOpts -XX:+UseBiasedLocking \
-XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 \
-XX:NewSize=2048M -XX:MaxNewSize=2048M -XX:NewRatio=2 \
-XX:PermSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5 \
-XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC \
-XX:+CMSParallelRemarkEnabled -Djava.awt.headless=true \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname = "192.168.7.103" "# write address of the machine I

2, start jenkins-slave service

[root@jenkins-slave ~]# systemctl start jenkins

3, began a visit to jenkins-slave configuration pages operated initialization: 192.168.7.103: 8080, create a login account login jenkins specific installation deployment steps, see: https: //www.cnblogs.com/struggle-1216/p/12387377 .html

3, jenkins-slave node configuration in the JAVA environment java

1, Jenkins-slave node configuration java environment

(1) Slave slave server creates a working directory if you need to perform to compile job, you also need to configure and install java environment and master the same basic operating environment git, svn, maven, etc., and we should create a master directory of the same data, because the script only a path called a master of the relative path, this path must be consistent with each node in the master node.

[Root @ jenkins-slave ~] # mkdir / var / lib / jenkins -p # Create master server and from the server code directory consistent pull jenkins
[Root @ jenkins src] # scp jdk-8u212-linux-x64.tar.gz 192.168.7.103:/usr/local/src # JDK the primary file server jenkins transmitted from the server
[Root @ jenkins-slave src] # tar xvf jdk-8u212-linux-x64.tar.gz # jenkins from the decompression on the server JDK
[Root @ jenkins-slave src] # ln -sv /usr/local/src/jdk1.8.0_212/ / usr / local / jdk # creating a soft link from the server to JDK
‘/usr/local/jdk’ -> ‘/usr/local/src/jdk1.8.0_212/’
[Root @ jenkins-slave src] # ln -sv / usr / local / jdk / bin / java / usr / bin / java # java implementation of the program will also create a soft link, otherwise not start jenkins

(2) defined JAVA environment variables / etc / profile

export HISTTIMEFORMAT="%F %T `whoami`"
export export LANG="en_US.utf-8"
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

[Root @ jenkins-slave src] #. / Etc / profile # the environment variables to take effect
[Root @ jenkins-slave src] # java -version # Check that the JAVA version, you can run the java command
java version "1.8.0_212"
Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)

4, start adding jenkins-slave node

1, jenkins ---> System Management ---> Node Manager ----> new node, create a name jenkins-node1

2, add jenkins-slave information

3, add the credential provider jenkins

 

5, the final information jenkins-slave host

6, see jenkins-node1 node logs, then you can see jenkins slave node is up and running.

 

 7, see the slave synchronization state, has been synchronized normal

 

 8, jenkins-slave operation logs to solve the problem did not find java directory, create a directory on jenkins-slave machine

[Root @ jenkins-slave ~] # mkdir / var / lib / jenkins / jdk / bin -p # Create a directory
[Root @ jenkins-slave ~] # ln -s / usr / local / jdk / bin / java / var / lib / jenkins / jdk / bin # java will be stored in the file directory to create a soft link to the new directory can

9, this time to see jenkins-node1日志 again, here you can not find the directory has been resolved.

 

 10, if you think about adding a new node nodes, only need to copy the template to jenkins-node1, and modify the IP address and the name and other information to the inside, where it is no longer demonstrated.

 

 

 

  

Guess you like

Origin www.cnblogs.com/struggle-1216/p/12431174.html