Recently, leaders have asked local projects in Shanghai to use Jenkins to achieve automated deployment so that the projects can be handed over to the operation and maintenance students to reduce the workload of development. Record the process of climbing this pit.
1. Preface
Jenkins is a continuous integration tool developed based on Java to monitor continuous repetitive work, and its functions include:
1. Continuous software version release/testing projects.
2. Monitor the work executed by external calls.
I downloaded the above from the encyclopedia. To be honest, I didn’t understand it. What’s this? In fact, based on my current understanding and application, the most common thing is deployment. Other functions are to be studied (_) The master is currently working in an unknown state-owned enterprise in Shanghai. The current project release process is:
1. Local or packaged server uses maven to make a war package
2. Connect to the project server remotely or SSH
3. Stop tomcat, delete the old package and cache, and put the newly-printed package in
4. Restart tomcat. The seemingly simple steps to complete, but it actually takes up a lot of development time.
Then use jenkins:
1. Click to build now and complete. What? Just click a button and it's done. Yes, it's a button. In fact, after you click the button, jenkins has helped you run the background of the above process. This time, I will introduce how to make it run in the background.
2. Installation and construction
This introduction is based on Windows, which can be installed on a development computer or a Windows Server server
1. Download jenkins
jenkins provides multiple installation methods
1.1jenkins.msi installation is just like when we usually install software, dots. Download address: https://jenkins.io/download/
1.2jenkins.war package Since it is a war package, it goes without saying that everyone knows how to start it, just throw it into tomcat. Download address: http://updates.jenkins-ci.org/download/war/
2. Start jenkins
I personally recommend the installation method of 1.1. I often encounter memory overflow problems when using 1.2. Of course, memory overflow can be adjusted, but there is an extra workload.
Using the 1.1 installation method, after the installation is completed, we will see the Jenkins service in the Windows service. As shown in the figure below, just start and close and restart the service directly.
If you use 1.2 to download the war package, there are actually two ways to start it.
The first type is placed in the tomcat container.
The second type is to enter cmd into the installation directory, java -jar jenkins.war
3. Install jenkins
The installation method 1.1 and 1.2 are the same
jenkins default port 8080, can be modified, method Baidu.
Enter http://localhost:8080/. The red font path has a password, fill in the password, and then next step.
When choosing a plug-in, it is recommended to choose an official recommended plug-in to avoid a lot of trouble.
Waiting for installation...
After installation, you will see the picture below. The user name and password are used to log in. The full name is used to display it. Continue to the next step.
Because I want to deploy the maven project, there is no maven in the default plugin of jenkins, so I need to install it first.
Click the plugin below to install it directly, maven integration plugins
Install the hot deployment plugin Deploy to container Plugin
4. Configure jenkins
We need to match the maven setting file, jdk and maven address. The setting file must be matched. Don’t use it as default. Because this is not matched, you have been struggling for a long time.
Finally, just apply and save. Remember not to forget to save the application, otherwise it will be in vain.
Next, we will create a new project, first enter the project name, select the maven project, OK
Select source code management, because we are an enterprise project, not open source, use SVN, choose Subversion to configure SVN
The URL is your SVN address, and Credentials is the account password
Then go to Pre Steps, this thing is what operations are needed before packaging. For example, our current project needs to copy the log4j.xml file, etc. You can write windows or shell scripts according to the environment
If there is no operation, there is no need to match it.
Post my script for your reference. I will use XXX to replace the relevant parts.
@echo offrem Set variable:: APP.XXX app.XXX code local and directory set APP.XXX="D:/svn/svn.173.XXX/app.XXX/"::home.ommm's log4j.xml variable START:: HOME_OMM_LOG4J_GOALS_PATH The path to store log4j.xml of home.ommm set HOME_OMM_LOG4J_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/home.ommm/src/main/resources"::HOME_OMM_LOG4J The path of the log4j.xml test environment of home.ommm set HOME_OMM_LOG4J="D:/svn/svn.173.XXX/app.XXX/config/home.ommm/sh_test/resources"::home.ommm's log4j.xml variable END::home.weixin's variable START::HOME_WEIXIN_LOG4J_GOALS_PATH The path of the log4j.xml stored at home.weixin's log4j.xml HOME_WEIXIN_LOG4J_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/home.weixin/src/main/resources"::HOME_WEIXIN_LOG4J The path of the log4j.xml test environment of home.weixin set HOME_WEIXIN_LOG4J="D:/svn/svn.173.XXX/app.XXX/config/home.weixin/sh_test/resources"::HOME_WEIXIN_CONFIG.JS_GOALS_PATH config.js path HOME_WEIXIN_CONFIG.JS_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/home.weixin/src/main/webapp/module/js"::HOME_WEIXIN_CONFIG.JS home.ommm's path HOME_WEIXIN_CONFIG.JS home.ommm's path set HOME_WEIXIN_CONFIG.JS="D:/svn/svn.173.XXX/app.XXX/config/home.weixin/sh_test/webapp/module/js"::home.weixin variable END::service variable START::SERVICE_LOG4J_GOALS_PATH The path to store the log4j.xml of the service set SERVICE_LOG4J_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/service/src/main/resources"::SERVICE_LOG4J The path of the log4j.xml test environment of the service is set SERVICE_LOG4J="D:/svn/svn.173.XXX/app.XXX/config/service/sh_test/resources":: SERVICE_APPLICATION_RESOURCES_ALL_GOALS_PATH The path of the application-resources-all.xml is set SERVICE_APPLICATION_RESOURCES_ALL_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/service/src/main/resources/config/spring":: SERVICE_APPLICATION_RESOURCES_ALL application-resources-all.xml test environment path set SERVICE_APPLICATION_RESOURCES_ALL="D:/svn/svn.173.XXX/app.XXX/config/service/sh_test/resources/config/spring"::service variable END::service.refresh variable START::SERVICE_REFRESH_LOG4J_GOALS_PATH The path set of log4j.xml that stores service.refresh SERVICE_REFRESH_LOG4J_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/service.refresh/src/main/resources"::SERVICE_REFRESH_LOG4J service's path set SERVICE_REFRESH_LOG4J="D:/svn/svn.173.XXX/app.XXX/config/service.refresh/sh_test/resources":: SERVICE_REFRESH_APPLICATION_RESOURCES_ALL_GOALS_PATH The path to store application-resources-all.xml is set SERVICE_REFRESH_APPLICATION_RESOURCES_ALL_GOALS_PATH="D:/svn/svn.173.XXX/app.XXX/src/service.refresh/src/main/resources/config/spring":: SERVICE_REFRESH_APPLICATION_RESOURCES_ALL application-resources-all.xml test environment path set SERVICE_REFRESH_APPLICATION_RESOURCES_ALL="D:/svn/svn.173.XXX/app.XXX/config/service.refresh/sh_test/resources/config/spring"::service.refresh variable END::Judge whether the log4j.xml file of home.ommm exists if exist %HOME_OMM_LOG4J_GOALS_PATH%/log4j.xml (del %HOME_OMM_LOG4J_GOALS_PATH%/log4j.xml)cd %HOME_OMM_LOG4J_GOALS_PATH%copy %HOME_OMM_LOG4J%/log4j.xml log4j.xml::Judge whether the log4j.xml file of home.weixin exists if exist %HOME_WEIXIN_LOG4J_GOALS_PATH%/log4j.xml (del %HOME_WEIXIN_LOG4J_GOALS_PATH%/log4j.xml)cd %HOME_WEIXIN_LOG4J_GOALS_PATH%/log4j.xml)cd %HOME_WEIXIN_LOG4J_GOALS_PATH%copy %HOME_WEIXIN_LOG4J%/log4j.xml log4j.xml::Judge whether the config.js file of home.weixin exists if exist %HOME_WEIXIN_CONFIG.JS_GOALS_PATH%/config.js (del %HOME_WEIXIN_CONFIG.JS_GOALS_PATH%/config.js)cd %HOME_WEIXIN_CONFIG.JS_GOALS_PATH%copy %HOME_WEIXIN_CONFIG.JS%/config.js config.js::Judge whether the config.xml file of service exists if exist %SERVICE_LOG4J_GOALS_PATH%/log4j.xml (del %SERVICE_LOG4J_GOALS_PATH%/log4j.xml)cd %SERVICE_LOG4J_GOALS_PATH%copy %SERVICE_LOG4J%/log4j.xml log4j.xml::Judge whether the service's application-resources-all.xml file exists if exist %SERVICE_APPLICATION_RESOURCES_ALL_GOALS_PATH%/application-resources-all.xml (del %SERVICE_APPLICATION_RESOURCES_ALL_GOALS_PATH%/application-resources-all.xml)cd %SERVICE_APPLICATION_RESOURCES_ALL_GOALS_PATH%copy %SERVICE_APPLICATION_RESOURCES_ALL%/application-resources-all.xml application-resources-all.xml::Judge whether the log4j.xml file of service.refresh exists if exist %SERVICE_REFRESH_LOG4J_GOALS_PATH%/log4j.xml (del %SERVICE_REFRESH_LOG4J_GOALS_PATH%/log4j.xml)cd %SERVICE_REFRESH_LOG4J_GOALS_PATH%copy %SERVICE_REFRESH_LOG4J%/log4j.xml log4j.xml::Judge whether the application-resources-all.xml file of service.refresh exists if exist %SERVICE_REFRESH_APPLICATION_RESOURCES_ALL_GOALS_PATH%/application-resources-all.xml (del %SERVICE_REFRESH_APPLICATION_RESOURCES_ALL_GOALS_PATH%/application-resources-all.xml)cd %SERVICE_REFRESH_APPLICATION_RESOURCES_ALL_GOALS_PATH%/application-resources-all.xml)cd %SERVICE_REFRESH_APPLICATION_RESOURCES_ALL_GOALS_PATH%copy %SERVICE_REFRESH_APPLICATION_RESOURCES_ALL%/application-resources-all.xml application-resources-all.xmlexit
Build has been compiled, specifying the pom location and mvn command. By the way, you can also configure workspace in the advanced level, which does not match the default in the jenkins installation directory.
After building, the operation is where to put it after the package is completed. Select the following image
After this construction operation, we use tomcat's hot deployment. You can refer to my previous article: Tomcat's hot deployment
WAR/EAR: The address of the war package
Context: is the deployed package name
UserName/Password refers to the account/password for hot deployment tomcat
URL: tomcat interface, such as: http://172.16.101.71:8180/
5. Start building
After talking so much, it’s finally time to come. The operation of a button has finally arrived. You can see our project when you return to the homepage of jenkins.
After building, click on the build execution status in the lower left corner to see that our project is under construction. Click on the project name to enter
Select Console to see the log or error message printed during the construction process.
3. Existing problems
The jenkins role issue, since it is released, it involves the production environment. All users cannot have permission to deploy the production environment. If anyone accidentally clicks it. The consequences...can't believe it.
Solution: Install the Role-based Authorization Strategy plug-in, refer to the remaining steps: http://www.cnblogs.com/zz0412/p/jenkins_jj_14.html. I am not detailed.
The problem of tomcat hot deployment, the memory rise and overflow after deploy several times, stop running or report an error. I checked the problem of the hot deployment itself online, and I am looking for a solution...
Solution: There are problems with tomcat hot deployment itself, and the production environment generally does not adopt the hot deployment method. Currently, because my test environment is using the Windows Server 2008 system, it adopts the hot deployment method. The problem still exists. However, I have read a post that it can be solved using the Windows batch command, which is the bat command, but I have not studied it in depth. In the Linux system in production environment, I currently use the Publish Over SSH plug-in. The specific steps are to add the steps below to the configuration.
1. In System Management-》 System Settings, find Publish over SSH and enter:
2. Configure Post Steps
Here we will continue with the above project. This configuration will only be displayed after the above plug-in is installed!
Transfer SetSource files: Indicates the local war package and path to upload, you can go to the workspace to view it.
Remove prefix: means the folder to be removed when uploading, that is, only the war package is uploaded
remote drill: means the path during execution, which is equivalent to uploading the war package here
exec commad: The command to be executed
As shown in the figure, the war package will be placed in the home/admin folder, and then I will execute the shell command I wrote. It mainly involves stopping tomcat, backing up the original war package, deleting the original war package under webapp, copying the new war package under home/admin, starting tomcat, and then it is done. This avoids the problem of server crash caused by hot deployment memory overflow.
Script reference:
#!/bin/sh #defined TOMCAT_HOME="/usr/java/tomcat/apache-tomcat-7.0.67/" ID=`ps -ef | grep java | grep tomcat|awk '{print $2}'` echo $ID echo "kill tomcat" kill -9 $ID echo "remover war file" cd "$TOMCAT_HOME"/webapps rm -rf JavaWeb-0.0.1-SNAPSHOT rm -rf JavaWeb-0.0.1-SNAPSHOT.war echo "copy war to webapp" cd /home/lin cp JavaWeb-0.0.1-SNAPSHOT.war "$TOMCAT_HOME"/webapps cd "$TOMCAT_HOME"/bin echo "start tomcat" ./startup.shThe Deploy plugin problem, the Deploy to container Plugin plugin used above is used to implement hot deployment, but the deploy plugin limits that one project can only deploy one war package. My project is that two wars separated from the front and back ends of a project can't be built twice,  ̄へ ̄.
Solution: Try not to use hot deployment. If the test environment is temporarily used, I am currently building two new projects and only compile one war package at a time.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.