The introduction

We often need to modify native code during development and submit it to Git or SVN. To run the program, you compile and deploy the code on SVN or Git to a Web server. If this deployment is manual, you need to execute the compile command every time, and then manually copy the WAR file to the server, and finally restart Tomcat, which would be very tedious. Jenkins was born to solve the problem of automated deployment.

My environment: local windows10, server centOS 7. Both have JDK and Tomcat installed.

1. Download and install Jenkins on your local machine

IO/For Jenkins

If you have JDK and Tomcat installed on your Windows server, you should install the JDK and Tomcat. Here I’m using JDK8 and Tomcat9.

1.1 Click download to download the war file. Click download to local.

Add the file to the tomcat webapps directory. Note that the file size exceeds 50M. Modify the /manager/ web-inf /web.xml file in the tomcat installation directory. Comment out the following configuration.

<! -- <max-file-size>52428800</max-file-size> -->Copy the code

1.2 Start Tomcat and access localhost:8080/ Jenkins in the browser

The following screen appears, prompting you to enter your password. The password is in the red file.

Select the recommended plug-ins to install. This is going to take some time.

Create an administrator account.

The installation is complete.

2. Install Maven and Git on the Web server

Download Maven to compile the project. Download Git to automatically get the latest git server code.

2.1 You can obtain the download address from the Maven official website by using Wget

If wget is not installed on your server, install it with the following command

yum -y install wget
Copy the code

Visit the Maven website find maven.apache.org/download.cg download address… Execute the following command

We usually download it herecd /usr/local/ download wget http://us.mirrors.quenda.co/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz unzip the tar XVZF Apache maven - 3.6.3 - bin. Tar. GzCopy the code

2.2 Setting Maven environment variables facilitates the use of MVN commands anytime and anywhere

Edit /etc/profile and add the following two lines at the end.

M2_HOME=/etc/local/ apache maven -- 3.6.3export PATH=${M2_HOME}/bin:${PATH}
Copy the code

Overloaded set

source /etc/profile
Copy the code

Verify that the installation is successful

mvn -v
Copy the code

2.3 install git

yum install git
Copy the code

3. Register the Web server with the Jenkins node

If your Jenkins is installed on the Web server, you can skip this step because by default Jenkins has created a node called master to represent the machine on which Jenkins is installed. However, when You restart Tomcat later, since Jenkins is running in Tomcat, Will be shut down with Tomcat and lose the link. Instead of installing Jenkins on a local machine like I did, in order to be able to deploy to a remote Web server, you need to register the Web server with Jenkins as a node in this step. As shown in the following figure, the system has built the master and I created the test node TestEnv.

To create a node, do as follows. First select Build Executor Status.

Next fill in the configuration.

  • Remote root Directory is your working root directory on the Web server. I’m going to log in as root and put my working directory in /root/jenkins.
  • Launch Agent agents via SSH. This represents SSH login.
  • Host Specifies the address of your Web server
  • Credentials Enter the password of your login account
  • Host Key Verification Strategy Select Known hosts file Verification Strategy

Finally, click Save to complete the creation of the node.

4. Jenkins creates tasks

Click Create New Task to enter the project creation interface

Fill in the task name and select the free style project.

  • Repository URL: Git HTTP Repository URL
  • Credentials: Indicates the user name and password required for login
  • Branch Specifer: Branches of Git
  • Check out to s sub-directory: Enter a subdirectory name for downloading code

Finally, add the shell command to execute and save the Settings.

Here’s the code.

BUILD_ID=DONOTKILLME

. /etc/profile

export PROJ_PATH=`pwd`
export TOMCAT_APP_PATH=/usr/local/ apache tomcat -- 9.0.30# compile code (note that siled is an arbitrary name, which is the directory name entered in the previous step)
cd $PROJ_PATH/siled
mvn clean install

Delete the original war file
rm -rf $TOMCAT_APP_PATH/webapps/*.war

# to shut down tomcat
$TOMCAT_APP_PATH/bin/shutdown.sh

Copy a new WAR to Tomcat
cp $PROJ_PATH/siled/target/*.war $TOMCAT_APP_PATH/webapps/

# restart tomcat
$TOMCAT_APP_PATH/bin/startup.sh
Copy the code

5. Try deployment

Click Build Now to Build now. Jenkins will then automatically link to the server, get the latest code from Git, and execute the shell command you added in the previous step.

Click console output.

The result can be confirmed as a success.

6. Verify the deployment application

Access the application address, and you can see that the automatic deployment succeeds.

7. Scheduled deployment

The above steps are basically fine, but what if we want to deploy the latest code every day? Of course, every manual click of deployment is too much trouble. Jenkins provides us with timed deployment capabilities.

The preceding configuration indicates that the trigger is triggered at 23:00 on June 13.

If this parameter is set to 00 06 * * *, the alarm is triggered at 6:00 am every day. If you are not familiar with this, you can search cron expressions.

Official Configuration Description

conclusion

Configuring Jenkins for the first time was tedious, but not difficult. If there is any question in the middle, there are answers on the Internet, here I try to write a little more detailed, hoping to help readers. That’s what you say about Jenkins sometimes, Jenkins always. Especially for bigger projects, the more effortless you can be with flexible automated commands.