Jenkins+Git+Maven+Shell+Tomcat Continuous Integration Deployment Environment Popular Solution

Highlights

  • Using shell custom scripts to control the integrated deployment environment is more convenient and flexible
  • Simplify the lib package in the war package and reside in tomcat to reduce the transmission time of the war package
  • Jenkins user rights management
  • If the build fails, send an email to notify the relevant personnel to solve the problem
  • Automatic daily backup of war package, Jenkins configuration backup and version control

environment

  • Ubuntu 14.10 (GNU/Linux 3.16.0-33-generic x86_64)

Ready to work

  • Git version control server
  • Tomcat Publisher
  • An empty server (with Maven, Git, Jdk installed)

It can be on the same server but distributed deployment is more flexible and easy to maintain

step

1. Install Jenkins server

1.1 Download the war package and put it in tomcat to run the same as deploying a normal javaweb program. Jenkins official website

1.2 Start tomcat

此时Jenkins在初始化配置目录 默认配置目录路径:当前用户下的.jenkins目录 最好不修改By default, Jenkins reads the data in the configuration file into memory. If you replace the previous configuration file (involving Jenkins configuration version management and backup), you need it at this time 点击Jenkins的读取设置或者重启Tomcat. If the Jenkins page is unresponsive at this time, you should check Tomcat's Catalina .out, mostly due to memory overflow (the solution is to increase the memory size when Tomcat calls the Java virtual machine, this article will not focus)

2. Configure Jenkins

2.1 Install the required plugins

All plug-ins required for automated deployment Email Extension Plugin(email notification) GIT plugin(may be installed by default) Publish Over SSH(Remote Shell) Installation method: Home -> System Management -> Manage Plug-ins -> Optional Plug-ins -> Filter (Search for Plug-in Name) -> Check -> Click on the bottom to install directly (it takes a while, you can see the log changes of catalina.out for details)

2.2 Jenkins configuration

2.2.1 Basic information configuration

There is a question mark after each option to explain the current meaning (novices can skip this step and leave it blank by default) Configuration method: Home -> System Management -> System Settings

2.2.2 Email Configuration

The administrator's email address is the sender's email address (must be the same as the sender's email address configured in the following emails, otherwise the email will not be sent successfully)

2.3 Maven configuration

2.3.1 Maven Configuration configuration

The path is the setting.xml path of maven (Maven installation is omitted)

2.3.2 Maven project configuration

2.3.3 Maven installation directory configuration

2.4 JDK configuration

JAVA_HOME is the Jdk path. Jdk can also be downloaded, installed and decompressed from here (not recommended, you need to fill in the oracle account)

2.5 Git configuration

Among them, Path to Git executable is the path of your git execution, which is generally /usr/bin/git by default. If there is a difference, you canwhereis git

2.6 Mail configuration

2.6.1 Configure the template of the email (you can use html to write it yourself) User Name is the username Password is the password SMTP is different for different mailboxes. Google it by yourself (in addition, if there is no proxy for gmail email, do not use it, it is recommended to configure 163)

No VPN proxy connection

VPN connection

Template renderings:

Default Subject code:

构建通知:$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!

Template Default Content sharing:

(本邮件是程序自动下发的,请勿回复,<span style="color:red">请相关人员fix it,重新提交到git 构建 </span>)<br/><hr/>

项目名称:$PROJECT_NAME<br/><hr/>

构建编号:$BUILD_NUMBER<br/><hr/>

GIT版本号:${GIT_REVISION}<br/><hr/>

构建状态:$BUILD_STATUS<br/><hr/>

触发原因:${CAUSE}<br/><hr/>

构建日志地址:<a href="${BUILD_URL}console">${BUILD_URL}console</a><br/><hr/>

构建地址:<a href="$BUILD_URL">$BUILD_URL</a><br/><hr/>

变更集:${JELLY_SCRIPT,template="html"}<br/><hr/>
2.6.2 Configuring Email Triggers

Trigger email when it fails

2.7 Publish over SSH configuration

The remote execution shell script uses the public key and private key to connect. The private key is pasted in the key. The private key is pasted in the remote managed host. The two hosts trust each other, so scp and other operations do not need to enter the user name and password.

Public key and private key generation method: 1. On the management host linux, press Enter all the ssh-keygen -t rsa -C"[email protected]way to generate id_rsa (private key) id_rsa.pub (public Yue) under /root/.ssh. 2. Copy the content of public Yue to the remote for communication ( Managed) host /root/.ssh/authorized_keysIf there is no such directory file, it will be created manually.

After the configuration, you can Test Configuration

3. Configure Job

Steps: Home -> New -> Build a maven project (enter item name) -> enter the project -> configure

3.1 Basic information of JOB

3.2 Project source code management

Repository UR project address Credentials authorization can be SSH or username and password (SSH method is the same as above)

Select the branch that needs to be built. Our project adopts the default master and develop of git workflow. We usually develop and build the develop branch, and officially build the master and label it (the previous company's git submission standardization is quite complicated, and there are quite a lot of branches, here you can come according to the actual situation)

3.3 Building triggers

Here we choose poll to check the version of the git repository code base every 1 minute. If there is a change, it will be built immediately. Here you can make it according to the actual situation of your team. Of course, there is another plug-in gitlab-hook that can actively notify Jenkins Build, but the memory occupied by the plug-in is relatively large, you need to increase the memory configuration of the tomcat virtual machine, otherwise the memory will overflow,个人觉得如果一个团队人很多的话,选择poll更适合并且时间间隔设置长一些,避免频繁构建,gitlab-hook 适合人很少甚至一个人的情况。

3.4 Build Commands

We use the simplest clean install. Of course, we can upload the deployed products to nexus according to their own needs. For details, please refer to the Maven command

clean install deploy:deploy-file -DgroupId=com.weitoo -DartifactId=common -Dversion=0.1-SNAPSHOT -Dpackaging=jar -Dfile=D:workspaceserver-aggregatorcommon	argetcommon-0.1-SNAPSHOT.jar -Durl=http://192.168.0.200:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

3.5 Add post-build step

  1. Execute the shell command after the build is successful

    The purpose of this shell is to take out all other lib packages in the war package lib, leaving only common-0.1-SNAPSHOT.jar, which greatly reduces the size of the war package (the complete war package of 30M takes more than 2 minutes to transfer the package to the Alibaba Cloud server, and after the simplification of 2M, 10 more than seconds, greatly improving the build speed)

share my shell

mv ~/.jenkins/jobs/server/workspace/server/target/server/WEB-INF/lib/common-0.1-SNAPSHOT.jar ~/.jenkins/jobs/server/workspace/server/target/
rm -rf ~/.jenkins/jobs/server/workspace/server/target/server/WEB-INF/lib/*
rm -rf ~/.jenkins/jobs/server/workspace/server/target/server.war 
mv ~/.jenkins/jobs/server/workspace/server/target/common-0.1-SNAPSHOT.jar ~/.jenkins/jobs/server/workspace/server/target/server/WEB-INF/lib/
cd ~/.jenkins/jobs/server/workspace/server/target/server/
zip -r ~/.jenkins/jobs/server/workspace/server/target/server.war * -r
scp /root/.jenkins/jobs/server/workspace/server/target/server.war [email protected]:/opt/war/

2. The build is successful and the shell script is executed remotely

exec command is the path to the remote sh

share my publish.sh file

The function is to back up the war package uploaded each time and restart tomcat

TOMCAT_HOME="/opt/apache-tomcat-7.0.59"
TOMCAT_PORT=80
PROJECT="server"
BAK_DIR=/opt/war/bak/$PROJECT/`date +%Y%m%d`

#shutdown tomcat
${TOMCAT_HOME}/bin/shutdown.sh
echo"tomcat shutdown"

#check tomcat process 
#TOMCAT_PID="lsof -n -P -t -i :${TOMCAT_PORT}"
#echo"current tomcat pid :"${TOMCAT_PID} 
#while [ -n"${TOMCAT_PID}"] 
#do
# sleep 5 
# tomcat_pid="lsof -n -P -t -i :${TOMCAT_PORT}"
# echo"scan tomcat pid :"${TOMCAT_PID} 
#done
sleep 3 

#publish project 
rm -rf"${TOMCAT_HOME}"/webapps/${PROJECT}
cp /opt/war/"${PROJECT}".war"${TOMCAT_HOME}"/webapps/${PROJECT}.war

#bak project
mkdir -p"${BAK_DIR}"
cp"${TOMCAT_HOME}"/webapps/${PROJECT}.war"${BAK_DIR}"/"${PROJECT}"_`date +%Y%m%d%H%M%S`.war

#remove tmp
rm -rf /opt/war/${PROJECT}.war

#start tomcat
"${TOMCAT_HOME}"/bin/startup.sh
echo"tomcat is starting!"

Share my Tomcat streamlined approach

  1. Create a new custom jar package file under tomcat_home/lib, and import other jar packages required by the project (if there are new ones in the future, import them again separately)
  2. Modify tomcat_home/conf/catalina.properties to search to shared.loaderadd the path

    shared.loader=${catalina.base}/lib/server,${catalina.base}/lib/server/.jar,${catalina.home}/l ib/server,${catalina.home}/lib/server/.jar

At this point, Tomcat will load the lib package under the server before running.如果是多个项目公用一个tomcat的时候,就需要这里放公共的lib包,避免tomcat加载多余的jar包,消耗内存。

3.6 Post-build email settings

3.6.1 Email subject recipient configuration

3.6.2 Email triggers

局部配置会覆盖掉全局配置, We have previously configured the build failure email trigger in the global configuration. Here is a more detailed configuration. We choose the build failure Failure-1st trigger, and send an email to the developer after the failure. (This can be configured according to actual needs, can be configured Multiple triggers) The developer's email is configured in the Recipient List

4. Jenkins user rights management

Steps: Home -> System Management -> Configure Global Security Basic configuration: Only registered users can operate, of course, if it is a large enterprise, you can use the project matrix authorization strategy, details can be Google

5. Backup and version control of Jenkins configuration

In many cases, the configuration of Jenkins has been changed inadvertently, the platform is broken, and you want to restore it. At this time, you have to configure or version the Jenkins configuration file. You only need to /root/.jenkins/add it to the git repository. The directory contains all the information of Jenkins, including every time the build history information and historical jar package are fully backed up and then overwritten, the folder already exists when rebuilding the JOB. 只需要手动删掉这些目录即可,不会丢失数据。( This is a bug of Jenkins, refer to JENKINS-21330 )

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691301&siteId=291194637