In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Jenkins Automation deployment online
Abstract
Jenkins automated deployment project, which saves operation and maintenance time through jenkins deployment, eliminating the need for manual cp launch and release
Jenkins Automation deployment online
Jenkins
Jenkins Automation deployment online
(1) Java environment demo [Jenkins and Tomcat on one server]
(2) Java environment demo [Jenkins and Tomcat are not on the same server]
(3) demonstration of Java environment [live script]
(4) demonstration of NodeJs environment [live script]
What is Jenkins
2. Online flow chart
III. Jenkins installation and configuration
IV. Jenkins Automation deployment Project case
What is Jenkins? Jenkins is a self-contained open source automation service that can be used to automate various tasks related to building, testing, and delivering or deploying software. Jenkins can now be installed through the local system software package Docker, and can even be run independently by any computer with Java operating environment. 2. Online flow chart
Now that we are talking about automated launch, we have to talk about the process of launching a project. Only by standardizing can we avoid accidents!
The online flow chart is shown below.
III. Jenkins installation and configuration
Jenkins depends on the Java environment, we need to install the Java environment and related environment preparation
# turn off the firewall
$iptables-F
$iptables-X
$systemctl stop firewalld
$systemctl disable firewalld
# install yum source
$wget-O / etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
$wget-O / etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
$yum clean all & & yum makecache
1. Download the Jdk package
Download address: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Upload a jdk package to the server
# decompress the copy jdk
$tar xf jdk-8u171-linux-x64.tar.gz-C / usr/local/
$ln-s / usr/local/jdk1.8.0_171/ / usr/local/jdk
$ln-s / usr/local/jdk/bin/java / usr/bin/java
# setting environment variables
$vim / etc/profile
Export JAVA_HOME=/usr/local/jdk
Export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
Export PATH=$JAVA_HOME/bin:$PATH
$source / etc/profile
two。 Install Jenkins
$wget-O / etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
$rpm-- import https://pkg.jenkins.io/redhat/jenkins.io.key
$yum install jenkins-y
$systemctl start jenkins
# # if there is an error when we start Jenkins, you can directly use systemctl status jenkins to view the error
Interpretation of jenkins-related catalogs:
(1) / usr/lib/jenkins/:jenkins installation directory, where the war package will be placed. (2) / etc/sysconfig/jenkins:jenkins configuration file, "Port", "JENKINS_HOME" and so on can be configured here. (3) / var/lib/jenkins/: default JENKINS_HOME. (4) / var/log/jenkins/jenkins.log:jenkins log file.
Check whether the port exists
3. Configure Jenkins
Jenkins has a security policy. We can copy the verification code according to the prompts.
Copy the CAPTCHA to the Web box
We can just use the recommendation here, because we can install it later.
In the installation plug-ins, some plug-ins can not be installed successfully because of network problems.
We can create an administrator here, or use admin directly
We'd better not use admin directly.
Installation completed access address: iP:8080
At this point, we have successfully installed Jenkins, and all that is left is to configure the plug-in and the configuration environment.
Because we don't need to install a plug-in at present, click next to install the plug-in.
In order to simulate the environment, we need to install some related plug-ins for Jenkins.
The following two maven plug-ins need to be checked
Plug-in name: maven lntergration
We check installation and restart.
After the installation is complete, as shown in the following figure
By default, there is no following maven project
4.Jenkins configuration Project
Configure SVN addr
Because I am a new Jenkins directory, I do not have permission, so I need to create one for authentication.
Fill in the SVN address, because my svn here has been linked to ldap, so there is no need to enter the password of svn. By default, this is the user and password of svn.
For specific articles, please refer to VisualSVN Migration to Linux SVN+Apache+ssl Integration LDAP
After the authentication is successful
Understand maven configuration
First of all, we need pom.xml under our svn branch.
Keep going down.
↓
↓
↓
↓
Since we only install the plug-in for maven, not the maven service, we need to configure the
We will add a name here, and maven will be installed automatically.
Maven installation is complete, need to rely on it will be slow to download from maven.apache.org, so we specify the address of private server, because private server is also used in actual production.
We also need to configure the configuration file conf/settings.xml in the configuration file of maven, because we are using the automatic installation of Jenkins, not the specified path, so we need to find this configuration file.
Configuration path for automatic installation of maven
Configure Maven warehouse address
All the addresses configured here are private server addresses.
Related articles Jenkins+Maven+SVN+Nexus to build a continuous integration environment
Configure the Maven image address
Configure Maven compilation parameters [R & D Metropolis]
Related articles: maven compilation commands
The pom.xml is configured with a private server address because there are many things in the code that need to be pulled from the dependency packages, which are stored in the local private repository (Nexus).
The pom.xml configuration in the code is as follows
The address of the private warehouse
5. Build test
Console output description
6.Jenkins project catalogue
You can modify the Jenkins home directory
The directory after Jenkins is packaged. This war package is the one under the tomcat that we need to copy.
IV. Jenkins Automation deployment Project case
Because of the current environment, I only take a screenshot of the Jenkins release process (this demonstration is only for the daily release version of the test environment)
(1) Java environment demo [Jenkins and Tomcat on one server]
1.Jenkins configuration
SVN partial configuration
Maven and script Settings
two。 The configuration of not sending scripts is as follows:
Related reference: list of Jenkins available environment variables
The storage path of the script can be found in system Administration-> Global configuration-> Jenkins path.
Last login: Thu Jun 28 18:01:59 2018 from 172.16.29.39
[root@tomcat ~] # cat / jenkins/deploy.sh
#! / bin/bash
#
# Jenkins Project Construction Universal TOMCAT deployment script
# @ author abcdocker
# @ create_time 2017-08-19
#
# configure deployment unit parameters in Jenkins
# Parameter format: MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN module name: the absolute path of the target TOMCAT to be deployed
# when there is only a single deployment unit and no Maven sub-module, the module name parameter can be absent, and the parameter format is: TOMCAT_ABSOLUTE_PATH
#
# Note:
# the TOMCAT startup script will be executed in this deployment script. In order to prevent Jenkins from killing all derived background processes after successful construction, you need to configure the global environment variable BUILD_ID in Jenkins with a value of allow_to_run_as_daemon
#
#
DEPLOY_TARGET_TOMCAT=$TOMCAT
# verify deployment parameters, which cannot be empty
If [- z "$DEPLOY_TARGET_TOMCAT"]
Then
Echo
Echo deployment parameter is empty, deployment failed!
Echo "#"
Echo
Echo single deployment unit parameter format:
Echo MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN module name: absolute path to the target TOMCAT to be deployed
Echo
Echo multiple deployment unit parameter format: (multiple deployment units are separated by spaces)
Echo MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH
Echo
Echo "#"
Exit 1
Fi
Echo
Echo deployment parameters: ${DEPLOY_TARGET_TOMCAT}
TOMCAT_ARR=$ {DEPLOY_TARGET_TOMCAT//;/}
ARR= ($TOMCAT_ARR)
ARR_LEN=$ {# ARR [*]}
Total ${ARR_LEN} deployment units of echo
I, 1
# get the target TOMCAT group passed in by Jenkins
For T in $TOMCAT_ARR
Do
Echo
Echo starts processing the ${I} deployment unit
Echo's first deployment unit: $T
# get the WAR path of the target TOMCAT and the absolute path of the TOMCATA
TOMCAT_PARAM= (${T _ swap /})
MODULE_NAME=$ {TOMCAT_PARAM [0]}
TARGET_TOMCAT_PATH=$ {TOMCAT_PARAM [1]}
WAR_PATH= "$WORKSPACE/$MODULE_NAME/target/*.war"
Echo deployment unit module name: "${MODULE_NAME}"
Echo deployment WAR package path: "${WAR_PATH}"
Echo deployment TOMCAT path: "${TARGET_TOMCAT_PATH}"
# need to consider the deployment problem under MAVEN single module
# if ["${# ARR [*]}"-eq 1-a-z "$TARGET_TOMCAT_PATH"]
If ["$ARR_LEN"-eq 1-a-z "$TARGET_TOMCAT_PATH"]
Then
# the MAVEN process has no sub-modules and a single deployment unit
TARGET_TOMCAT_PATH=$MODULE_NAME
MODULE_NAME= "NULL"
Fi
# check parameter. WORKSPACE variable comes from Jenkins environment variable.
If [- z "$MODULE_NAME"-o!-f $WAR_PATH]
Then
Echo error: MAVEN deployment module name parameter is empty or WAR package cannot be found!
Echo deployment failed!
Exit 1
Fi
If [- z "$TARGET_TOMCAT_PATH"-o!-d "$TARGET_TOMCAT_PATH"]
Then
Echo error: the destination TOMCAT absolute path parameter is empty or the TOMCAT directory does not exist!
Echo deployment failed!
Exit 1
Fi
Echo begins to clean up the target TOMCAT startup process.
TOMCAT_PID= `ps-ef | grep "$TARGET_TOMCAT_PATH" | grep "start" | awk'{print $2}'`
If [- n "$TOMCAT_PID"]
Then
Echo TOMCAT_$ {I}, PID$ {TOMCAT_PID}, ending the process...
Kill-9 $TOMCAT_PID & & echo PID$ {TOMCAT_PID} has been killed!
Else
The echo TOMCAT_$ {I} process did not start!
Fi
Echo starts cleaning the target TOMCAT cache...
Rm-rf $TARGET_TOMCAT_PATH/webapps/*
Rm-rf $TARGET_TOMCAT_PATH/logs/*
Rm-rf $TARGET_TOMCAT_PATH/work/*
Echo begins to deploy WAR packages...
The cp-a $WAR_PATH $TARGET_TOMCAT_PATH/webapps/ROOT.war & & echo WAR package has been deployed.
Echo starts starting the target TOMCAT service...
Sleep 10
/ bin/bash $TARGET_TOMCAT_PATH/bin/startup.sh
Echo starts configuring FTP permissions for the web directory.
# the startup process automatically decompresses the WAR package, so you need to wait for the WAR package to be decompressed before adjusting the directory permissions
Sleep 30
Chown-R vftpuser.vftpuser ${TARGET_TOMCAT_PATH} / webapps/ & & echo directory permissions are configured.
Echo deployed successfully
Echo completes the ${I} deployment unit processing.
Echo
((iTunes +))
Done
3. The construction effect is shown in the following figure:
(2) Java environment demo [Jenkins and Tomcat are not on the same server]
The above script is aimed at that both Jenkins and Tomcat are in the same directory. Sometimes our test environment is not on a server. The script is as follows
It's just a simple modification of the script.
[root@tomcat ~] # cat / jenkins/ysc.sh
#! / bin/bash
#
# Jenkins Project Construction Universal TOMCAT deployment script
# @ author Liu Shu
# @ create_time 2017-08-19
#
# configure deployment unit parameters in Jenkins
# Parameter format: MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN module name: the absolute path of the target TOMCAT to be deployed
# when there is only a single deployment unit and no Maven sub-module, the module name parameter can be absent, and the parameter format is: TOMCAT_ABSOLUTE_PATH
#
# Note:
# the TOMCAT startup script will be executed in this deployment script. In order to prevent Jenkins from killing all derived background processes after successful construction, you need to configure the global environment variable BUILD_ID in Jenkins with a value of allow_to_run_as_daemon
#
#
DEPLOY_TARGET_TOMCAT=$YSC
HOST=root@172.16.1.35
# verify deployment parameters, which cannot be empty
If [- z "$DEPLOY_TARGET_TOMCAT"]
Then
Echo
Echo deployment parameter is empty, deployment failed!
Echo "#"
Exit 1
Fi
Echo
Echo deployment parameters: ${DEPLOY_TARGET_TOMCAT}
TOMCAT_ARR=$ {DEPLOY_TARGET_TOMCAT//;/}
ARR= ($TOMCAT_ARR)
ARR_LEN=$ {# ARR [*]}
Total ${ARR_LEN} deployment units of echo
I, 1
# get the target TOMCAT group passed in by Jenkins
For T in $TOMCAT_ARR
Do
Echo
Echo starts processing the ${I} deployment unit
Echo's first deployment unit: $T
# get the WAR path of the target TOMCAT and the absolute path of the TOMCATA
TOMCAT_PARAM= (${T _ swap /})
MODULE_NAME=$ {TOMCAT_PARAM [0]}
TARGET_TOMCAT_PATH=$ {TOMCAT_PARAM [1]}
# WAR_PATH= "/ jenkins/workspace/ysc-all/$ {MODULE_NAME} / target/*.war"
WAR_PATH= "${WORKSPACE} / ${MODULE_NAME} / target/*.war"
Echo deployment unit module name: "${MODULE_NAME}"
Echo deployment WAR package path: "${WAR_PATH}"
Echo deployment TOMCAT path: "${TARGET_TOMCAT_PATH}"
# determine whether IP has a related directory
Ssh 172.16.1.35 "[- d $TARGET_TOMCAT_PATH]" > / dev/null 2 > & 1
If [$?! = 0]; then
Echo error
Else
Echo is correct
Fi
# check parameter. WORKSPACE variable comes from Jenkins environment variable.
If [- z "$MODULE_NAME"-o!-f $WAR_PATH]
Then
Echo error: MAVEN deployment module name parameter is empty or WAR package cannot be found!
Echo deployment failed!
Exit 1
Fi
# scp package
Ssh $HOST / etc/init.d/$ {MODULE_NAME} stop
Ssh 172.16.1.35 "[- d $TARGET_TOMCAT_PATH/webapps/ROOT/]" > / dev/null 2 > & 1
If [$? = 0]; then
Ssh 172.16.1.35 rm-rf $TARGET_TOMCAT_PATH/webapps/ROOT
If [$? = 0]; then
The scp $WAR_PATH root@172.16.1.35:$TARGET_TOMCAT_PATH/webapps/ROOT.war & & echo WAR package is deployed.
Echo $TARGET_TOMCAT_PATH is OK
Else
Echo deletes $TARGET_TOMCAT_PATH is error
Fi
Else
Echo "not found $TARGET_TOMCAT_PATH/webapps/ROOT"
The scp $WAR_PATH root@172.16.1.35:$TARGET_TOMCAT_PATH/webapps/ROOT.war & & echo WAR package is deployed.
Ssh $HOST / etc/init.d/$ {MODULE_NAME} restart
Fi
# Startup file
Done
# scp / home/config.properties/ysc/$ {MODULE_NAME} .js root@172.16.1.35:$TARGET_TOMCAT_PATH/webapps/ROOT/web/js/basePath.js
Ssh $HOST / etc/init.d/$ {MODULE_NAME} restart
The Jenkins configuration is modified as follows
After the modification, we build the demonstration.
Tip: in this environment, configuration files are controlled through maven build, that is, through R & D to control configuration files.
+
+
+
+
(3) demonstration of Java environment [live script]
Online environment demonstration
Our launch process is as follows:
The Jenkins configuration is as follows:
+
+
+
+
+
1. First test the environment script:
[root@tomcat ~] # cat / server/scripts/.upgrade-smscenter.sh
#! / bin/bash
WAR= "/ jenkins/workspace/portal-smscenter/bxg-sms-center-web/target/*.war"
Path= "/ data/hub/bxg-smscenter/ `date +% Y% m% d` /"
Scp_war () {
If [!-d $Path]; then
Ssh root@file-server mkdir-p $Path
Scp $WAR root@file-server:$Path
Else
Scp $WAR root@file-server:$Path
Fi
}
Ssh_file () {
Ssh root@file-server "/ bin/bash / server/script/bxg/bxg-smscenter.sh"
}
Scp_war
Ssh_file
two。 Springboard script modification
[root@File-server1 ~] # cat / application/smscenter/webapps/ROOT/ script function scp_file BXG function scp_file BXG ssh root@$HOST ssh root@$HOST [!-d $WAR] "` Then ssh root@$HOST "mkdir-p $WAR" fi scp $WAR/*.war root@$HOST:$WAR/ echo "scp $WAR/*.war root@$HOST:$WAR/"} ssh_deploy () {ssh root@$HOST "/ bin/bash / server/scripts/deploy_smscenter.sh"} scp_filessh_deploy
3.web server script
[root@online-server2 ~] # cat / server/scripts/deploy_smscenter.sh
#! / bin/bash
WAR= "/ data/hub/bxg-smscenter/ `date +% Y% m% d`"
OBJECT= "/ application/smscenter/webapps/ROOT/"
Backup= "/ data/tomcat/bxg-smscenter- `date +% Y% m% d`"
SCR_D= "/ application/smscenter/webapps/ROOT"
# config= "/ data/bak"
Backup_tar () {
Tar zcvf $Backup.tar.gz $SCR_D/
Echo "to prevent accidental cp entire project directory storage"
Cp-a $SCR_D/ $Backup
Rm-rf $OBJECT/*
}
Cp_war () {
Unzip $WAR/*.war-d $SCR_D/
}
Cp_config () {
Cat $Backup/WEB-INF/classes/application-prod.properties > $SCR_D/WEB-INF/classes/application-prod.properties
/ etc/init.d/smscenter restart
}
Backup_tar
Cp_war
Cp_config
[root@online-server2 ~] #
Related articles the enterprise must have the skill tomcat
+
+
+
+
(4) demonstration of NodeJs environment [live script]
Node environment launch process
The Jenkins configuration is as follows: [node project is not suitable for maven, so you can create an ordinary project in Jenkins without creating a maven project]
1. Test environment script
[root@tomcat ~] # cat / server/scripts/mobile/mobile.sh
#! / bin/bash
Source / etc/profile
HOST=file-server
BASE_DIR=/server/scripts/mobile/m
Url=$1
Server=$2
DATE= `date +% Y% m% d`
Tar () {
Rm-rf $BASE_DIR
[- d $BASE_DIR] | | mkdir $BASE_DIR
Cd $BASE_DIR
Echo "#"
Echo "Code pulling!!"
Svn co-Q $url/.
Echo "#"
}
Cp () {
Cd ${BASE_DIR}
/ bin/tar-zcvf masks ${DATE} .tar.gz. / *
Echo "#"
Echo "the file has been packaged and copied!!"
Echo "#"
Sleep 5
Scp masks ${DATE} .tar.gz root@$HOST:/data/hub/bxg-mobile/
Echo "#"
Echo "File has been copied! uploading server!!"
Echo "#"
Ssh root@file-server "/ bin/bash / server/script/bxg/bxg-mobile.sh $server"
}
Tar
Cp
[root@tomcat ~] #
two。 Springboard script
[root@File-server1 ~] # cat / server/script/bxg/bxg-mobile.sh
#! / bin/bash
HOST=$1
Mobile_tar= "/ data/hub/bxg-mobile"
DIR= "/ application/node"
DATE= `date +% Y% m% d`
Scp_file () {
If `ssh root@$HOST "[!-d $Mobile_tar]" `; then
Ssh root@$HOST "mkdir-p $Mobile_tar"
Fi
Scp $Mobile_tar/m_$ {DATE} .tar.gz root@$HOST:$DIR/
Sleep 3
Echo ""
Echo "#"
Echo "File-server is copying ${HOST}!!"
Sleep 3
Echo "#"
}
Ssh_deploy () {
Echo "Hi"
Ssh root@$HOST "/ bin/bash / server/scripts/bxg/bxg-mobile.sh"
}
Scp_file
Ssh_deploy
3.web publish script
[root@iZbp11tefvghtcfn5mudgdZ ~] # cat / server/scripts/bxg/bxg-mobile.sh
#! / bin/bash
Source / etc/profile
DIR= "/ application/node"
DATE= `date +% Y% m% d`
Backup= "/ application/node/m/"
BAK= "/ data/hub/bxg-mobile"
Backup_tar () {
Echo "to prevent accidental cp entire project directory storage"
Cp-ar $Backup ${BAK} / mobile_$DATE
Echo "online-server original directory copy backup completed!"
}
Cp_war () {
/ etc/init.d/mobile stop
# mv $Backup / tmp/m_$ {DATE} & & rm-rf / tmp/m_$ {DATE}
Rm-rf $Backup
Mkdir $Backup & & cd / application/node/
Tar xf masks ${DATE} .tar.gz-C $Backup
}
Npm_config () {
Cd $Backup
Cnpm install
Npm run build-prod
Npm run start-prod & > / var/log/mobile_$ {DATE} .log &
}
C () {
Echo "+ +"
Curl-I 127.0.0.13000
Echo "#"
Echo "if the service starts normally! you can start another one!"
Echo "#"
}
Backup_tar
Cp_war
Npm_config
C
+
+
+
Related articles Node.js environment building
Conclusion: Jenkins automation can not be completed by operation and maintenance alone and requires R & D participation. This article is just to show you the automation of our company, the so-called automation in my eyes. I hope you don't like it. If you have any comments or suggestions on the article, please leave a comment.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.