Configure Jenkins + Maven + git for continuous integration and automatic deployment

Source: Internet
Author: User

Preface

The concept of continuous integration has become the mainstream of software development. It can be tested more frequently to detect problems and prompt them as soon as possible. Automated deployment can accelerate deployment and effectively reduce human errors. I have never done this before. The recent new project has just had a chance and I have made some effort to do a good job. I hereby record it.


1. Development Environment

The standard development environment created here is as follows:

1. MAVEN manages projects;

2. Git manages code;

3. springmvc + spring + mybatis program framework;

4. MySQL is used as the data storage, And Druid is used as the connection pool;

5. unitils as the testing framework;

6. hibernate validator is used for data verification;

7. log4j is used as the log output.

Note: In fact, this set of things is very similar to grails, but they do not dare to use too radical technologies and frameworks. They are worried about recruitment issues -_-!


2. Deployment of Jenkins

The original name of Jenkins is Hudson, which is not traced back here. Many are available on the Internet, but never go wrong. The official website address is http://jenkins-ci.org /. We recommend that you download the latest version.

The installation of this software is the simplest I have ever seen. Just take the war package and put it under Tomcat and start Tomcat. The corresponding project configuration will be in ~ /. Jenkins directory. (Of course, you can install openjdk according to the method provided on the official website, but you have to download openjdk In the Debian method. Many other things are added, and the related configurations are also separated according to the Linux Directory standards, also go .)

In addition, we recommend that you install Jenkins on Linux so that no problem can be found by commands such as SSH. Otherwise, you need to solve the problem.


3. Jenkins plug-in

After installation, you can directly access "http: // yourhost: 8080/Jenkins" to enter the main interface. Click "System Management"-> "manage plug-ins". The first entry is blank, it takes about 1 minute to see the content. It is estimated that the content is being updated or downloaded in the background, and then you can see the content again on this page.


3.1 git plugin

Find the "Git plugin" installation in the "optional plug-ins", and check the installation and restart options at the bottom. The most funny thing here is that the way to check whether the network is connected is to try to open Google. I don't know whether tianchao cannot be opened. Fortunately, the download will not be affected...


3.2 email plug-in

This problem is very painful. I tried to release all the emails before, and finally upgraded the default plug-in. Select and update Mailer Plugin in updatable. If you want more email content, go to "optional plug-ins" to install "email extension plugin". You can search for the specific email content configuration on the Internet.


3.3 Other plug-ins

Many commonly used plug-ins are installed by default, such as Maven and JUnit. If you feel any problem, you can try to upgrade the version, but there is no Upgrade description and you do not know what to upgrade.


4. system settings

Click "System Management"> "system settings" on the main interface. Configure the following content:

1. directory location of Java, git, and Maven to ensure that commands can be correctly found;

2. Jenkins URL, which is automatically generated. Check the URL;

3. email settings. Note that the above "System Administrator email address" must be filled in, and the configuration in "extended E-mail Notification" should be filled in. The original "email configuration" should be ignored.


5. Project Settings

Create a project directly on the main interface. Configure the following content:

1. source code management: Select git, enter "repository URL", and add the corresponding "credentials". If the authentication information uses the private key, simply enter the private key content, the province does not know what the Directory Search rules are.

2. build trigger: Select "build periodically" and "poll SCM" here, and enter the same time format, for example, "H/15 ****", the following is a prompt about the specific execution time. The build operation will automatically be delayed by 3 minutes 40 seconds compared with poll. This setting is reasonable.

3. Build: Add two building steps: "execute shell" and "INVOKE top-level Maven target". Pay attention to the order and drag and drop the building steps. Script Execution is based on your own needs. For example, if you need to modify the database connection configuration, the official suggestion is to write the script in the project and call it directly here, instead of writing a complete script here. Add "clean test" to Maven build, that is, run the "MVN clean test" command.

4. publish JUnit test result report: Add "**/target/surefire-Reports/*" to the test report (XML /*. XML, the test report is automatically found after each test, and can be viewed in each build structure on Jenkins.

5. Email Notification: After building, add "editable email notification". You can directly use global variables to enter the recipient and content format of the email. The focus is to configure the sending trigger conditions.


6. Security Configuration

After building the above configuration, Jenkins can see too much content, including the pull source code, so it is necessary to add permission control. Go to System Management> Configure global security to perform the following steps:

1. Enable Security;

2. Jenkins private user database, which allows user registration first;

3. Select "security matrix" for the authorization policy, add a new "admin" user, and grant all permissions to the admin user;

4. Find admin in the user on the main interface, configure and set the login password;

5. Log in again to test whether the admin is normal. If there is no problem, disable allow user registration and remove all permissions of anonymous users.


7. automated deployment

Here I didn't let Jenkins deploy each test. On the one hand, considering that the unit test has basically met the needs, on the other hand, because the test is too frequent, the stage test environment should be restarted frequently, which affects normal manual testing. Therefore, I wrote a script to automatically complete the deployment when necessary.

#!/bin/sh# update codegit pull# packagemvn cleanmvn package -Dmaven.test.skip=true# deployWAR=`ls target | grep war`TOMCAT=/home/test/apache-tomcat-6.0.41mv target/$WAR $TOMCATcd $TOMCAT# invoke another deploy scriptsh deploy-war.sh $WAR webapps


8. A tough problem

This has nothing to do with the above issues, but it is very puzzled that this error does not appear in windows, and it does not appear when it is converted into war in Linux, an error occurs only when Maven test is directly executed in Linux. In fact, the root cause of the problem is that the configuration writing is not standardized, but the inconsistency in the error is really annoying. The following error is reported:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): xxx

This is because mybatis cannot find the bound class, but XML is correctly packaged. It seems that there is no big problem, and Windows is also correct. Finally, I found that when I was writing a fuzzy path, the classpath must be followed by an asterisk (*), which is written as follows:

<Bean class = "org. mybatis. spring. mapper. mapperscannerconfigurer "> <property name =" basepackage "value =" com.gz xitao. demo. *. dao "/> </bean> <bean id =" sqlsessionfactory "class =" org. mybatis. spring. sqlsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" configlocation "value =" classpath: configuration. XML "/> <! -- To scan files in multiple directories, the file must be declared as "classpath *", otherwise, an error occurs in some cases --> <property name = "mapperlocations" value = "classpath *: COM/gzxitao/demo/*/Dao /*. XML "/> </bean>


Configure Jenkins + Maven + git for continuous integration and automatic deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.