Automating builds using Jenkins to configure Git+maven

Source: Internet
Author: User
Tags shebang

Background

Recently installed Jenkins, referring to all kinds of information on the internet to try, toss for a long time, but found so much information, similarity in more than 90%!!! , the same installation process, the test of several machines, has not succeeded, have to sigh with their limited ability, eventually slowly groping, the formation of ideas, is now shared to everyone, I hope you in the installation of the time less detours.

PS: I hate this paste copy, no thinking bloggers, not only wasted everyone's time, but also wasted everyone's feelings.

Knowledge background

The first thing to understand is that Jenkins is helping us to package the code in a uniform compilation and put it into a tomcat container for publishing.

This means that we configure the process of compiling, packaging, uploading, and deploying into Tomcat by jenkins,jenkins the code to its host server (where Jenkins is installed), compiling, packaging, and publishing it to the container via the given code address URL.

So what we can notice is that in Jenkins's host server there must be a basic composition that can be done: Code clone (Git), code compilation (Maven), Code Run (TOMCAT).

The next step is to explain the installation process for each part

One, the installation of Git

1. Installation dependent

sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

Note: Perl-extutils-makemaker is also a member of these installation lists, so do not assume that you are two execution commands because of the line breaks that are displayed.

2. Download git

Select a directory to store the downloaded git, for example my:/home/xuliugen/tmp

https://github.com/git/git/archive/v2.3.0.zip

3. Unzip git

After the download is like this, do not feel strange, directly decompression can

Because the download is a zip, use the command to extract it to the Git directory:

#参数-d 后边的是解压到的目录unzip v2.3.0-d

4. Compile and install Git

Go to git directory

Install it in the "/usr/local/git" directory with the following command:

make prefix=/usr/localallsudo make prefix=/usr/local/git install

5. Configure Git

sudo#编辑profile文件

And then just to see that the file is already configured with a lot of configuration, simply add the git path to the bottom:

PATH=/usr/local/git/bin:$PATH

Put the phrase on the last side, and then apply the modification using the source command:

source /etc/profile

6. Verify that Git is installed successfully

git--version

Second, MAVEN installation

1. Download and decompress maven

MAVEN's installation package:

After decompression, my position is:

2. Configure Maven

Edit: ~/.bash_profile file, according to the comments corresponding changes can be

# maven所在的目录  export M2_HOME=/home/xuliugen/software/maven/apache-maven-3.3.3# maven bin所在的目录  export M2=$M2_HOME/bin# 将maven bin加到PATH变量中  export PATH=$M2:$PATH# 配置JAVA_HOME所在的目录,注意这个目录下应该有bin文件夹,bin下应该有java等命令  export JAVA_HOME=/usr/lib/jvm/jre-1.7.0

Then run: source ~/.bash_profile

3. See if MAVEN is installed successfully

Use: mvn-version to view results

Third, the deployment of Jenkins

The installation of Jenkins is simple, just put the war in a container and run it

1. Put Jenkins in Tomcat and unzip to start Tomcat

After the successful start, the interface is entered as above, indicating that the boot was successful.

2, install the plug-in

Before you configure it, you need to install the plugin first because you are using the way git pulls the code.

Found: Plugin management in the "optional Plugins" interface using CTRL+F Search: Deploy to Container Plugin plugin (this is to support the deployment of code to the Tomcat container)

Tick, click the button below: Direct installation, this may be longer, wait.

The section after success is:

Then install: GIT plugin, using the above method, due to network reasons, will install failure, which requires us to use another way to install

Search for Git plugin in the "optional plugins" in the same way, then click on the blue link (preferably open in a new window)

Will jump to the information address of Git plugin

You can see the plugin ID: git, and then in: http://updates.jenkins-ci.org/download/plugins/This place search can find, click in, select a version to download:

Point in the download is an HPI file

Then the same in: System Management-Management plug-in Advanced tab, drop-down can see the upload plugin, and then select the file upload just download the HPI file, can, after uploading, will automatically install. The above installation of the deploy to container plugin can also be done in this way.

After you have installed it, you can see the plug-in you just installed in the Installed tab:

3, to configure

See in the left side there is a: System admin click to go

The first line of yellow information on the top, indicating the encoding problem, can be modified in Tomcat's conf– "Server.xml file"

4. Jenkins System Setup

Click: System settings, which are not highlighted here are set to the default can be

(1) JDK, alias is arbitrary

(2) Git configuration

Note that the git location here is the executable file's address, which is the executable git directory under git–bin– (similar to the Java executable location under bin in Java)

If you fill in the wrong, late returns can not find Git and other errors, please pay attention!

(3) Ant configuration

Since this time there is no knife ant, do not configure

(4) MAVEN configuration

(5) Jenkins location

Here is the IP plus port and a path name, the mailbox to be set

(6) Email notification

Here I configure the QQ mailbox, we set according to the situation, the user name, password for their own QQ mailbox what password

Finally click on the app below to save

5. Configure Global Security

Then go back to system management, see Configure Global Security Click to go in, according to the configuration below

Click on the app to save, then in the upper right corner of the screen, there is a login-registration, click Register to realize the user's login.

The basic configuration of Jenkins is complete.

Create Job

1. Create a project

After clicking Sign in, you will see the following interface: My views

Click to enter

Click Create a new task

Select Maven Project and click OK

2, the configuration of the project

(1) Source management Choose Git, this time add the URL, below will be error, display let go certification, certification (other installation encountered this problem)

(2) Building triggers

Choose the first one, and the rest is basically a timed execution.

(3) Post Steps

Select and then the Execute shell below:

#!/bin/bash#copy file and restart TomcatExportJava_home=/data/home/server/jdkExportCatalina_home=/home/xuliugen/dubbo-providerExportCatalina_base=/home/xuliugen/dubbo-providerExportBuild_id=dontkillmetomcat_path=/home/xuliugen/dubbo-providerproject=dubbo-providerwar_name=dubbo-provider.war war_path=http://132.153.131.237/:8022/jenkins/job/manendemo/ws/targetserver_port=8082File_path=~/.jenkins/jobs/manendemo/workspace/spring_provider/target$tomcat _path/bin/shutdown.sh Sleep3SEcho "Rm-rf ${tomcat_path}/webapps/root/*"Rm-rf${tomcat_path}/webapps/root/*CD $file _pathCp${war_name} ${tomcat_path}/webapps/root/CD $tomcat _path/webapps/root/unzip${war_name}Rm-rf${war_name}Sleep5S# $tomcat _path/bin/startup.shCD $tomcat _path/bin/./startup.shEcho "Server restarted"

(4) Email notification

Now that the basic setup is complete, click Apply to save

(5) Build now

Click Build now in the top left corner

Then see the build history below and click on the blue to go into the console

Analysis of the script

View on the server of Jenkins:

You can see in the/root/.jenkins this directory, actually stored the Jenkins pull git code after the location of the compilation of the war, to this, we may all understand, the post-compilation location is here Ah! Then, the deployment into the Tomcat container, how to put the compiled war into Tomcat, which requires a script to complete, is also the process configured in the above process, the following line is analyzed.

First we should understand that putting a compiled war into a tomcat container is the first thing we need to know about these two locations, and obviously we do.

1, the head of the script file, this has no say

#!/bin/bash 

2. Environment variable setting

export JAVA_HOME=/data/home/server/jdkexport CATALINA_HOME=/home/xuliugen/dubbo-providerexport CATALINA_BASE=/home/xuliugen/dubbo-providerexport BUILD_ID=dontKillMe

The top of each position, you may know, catalina_home and catalina_base position is the location of the Tomcat container, here according to the requirements I will be the name of the Tomcat container Dubbo-provider, if changed to/home/ Xuliugen/tomcat is not better understood.

Export Build_id=dontkillme This sentence is important, because the job started after the execution of the next job,jenkins directly to kill the Tomcat process, so no matter how the script to write the command to start Tomcat, he will not start.

3. Other Locations

tomcat_path=/home/xuliugen/dubbo-provider #我的Tomcat位置project=dubbo-provider #我的项目编译的名称war_name=dubbo-provider.war  #最后打包的war名称war_path=http://132.153.131.237/:8022/jenkins/job/ManenDemo/ws/targetserver_port=8082 file_path=~/.jenkins/jobs/ManenDemo/workspace/spring_provider/target

This: War_path is actually the host server location IP of Jenkins, the port is the port that Jenkins runs, we just need to replace the IP address port Manendemo with its own;

File_path: The location of the war where Jenkins helped us compile the project, and we can see

4, familiar with the execution of orders

Below is the command that we used to deploy the war in the original way, which is easy to understand, which:

I am assuming that the project is already running, so I'm going to turn off Tomcat first, the correct way, should determine if the port is open, if it is not started by Tomcat, so this situation, I will be in the console of Jenkins to report the connection failure problem.

In addition to sleep 3s This allows the thread to hibernate, so you can look at Tomcat log information.

 $tomcat _path /bin/shutdown.sh Sleep 3  s echo   "RM-RF" Span class= "hljs-variable" >${tomcat_path} /webapps/root/* " rm-rf ${ Tomcat_path} /webapps/root/*CD   $file _path  cp ${war_name}  ${tomcat_path} / Webapps/root/CD   $tomcat _path /webapps/root /unzip ${war_name}  rm-rf ${war_name}  Sleep Span class= "Hljs-number" >5 s CD   $tomcat _path /bin/./startup.shecho   "server restarted" 
Other questions

1. Error when deploying project

This is very well understood, because the download jar failed problem, because it is the first time to use Maven, the first time to download a lot of jars to the Maven repository, because the network problem download failed, you can build multiple times to let him re-download, or directly to: https:// Repo.maven.apache.org/maven2 based on the jar of the error, find your true jar in Maven2, and then add the jar in the/root/.m2/repository corresponding location.

2. scripting files start Tomcat but the project still doesn't work.

The problem with the hormone hi says:
Export Build_id=dontkillme, because the job started after the execution of the next job,jenkins directly to kill the Tomcat process, so no matter how the script to write the command to start Tomcat, he will not start.

At this point, Jenkins's deployment is complete, hoping to help you.

Automating builds using Jenkins to configure Git+maven

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.