When we submit code to GitHub after , You can Jenkins Build on , But it's a little troublesome to do it every time , Today we're going to fight Jenkins Automatic building of , Every time code is submitted to GitHub after ,Jenkins Will be built automatically ;

Original address :http://blog.csdn.net/boling_cavalry/article/details/78943061

Preparatory work

Before the actual combat of this chapter , Recommended for your reference 《docker Next Jenkins Installation and experience 》
Do the actual battle of installation and experience , So that Jenkins Preliminary understanding of the service ;

Important premises

GitHub Active notification after receiving submitted code Jenkins, therefore Jenkins The server must have an external network IP, otherwise GitHub cannot access , my Jenkins The server is deployed on the virtual machine of Tencent cloud , With extranet IP;
* The source code to be submitted to GitHub, So you need to have a GitHub Number , And create a project on it ;
demo Project source code

This time, it is used to Jenkins The project built on is a springboot Of web engineering , the address is :[email protected]:zq2599/jenkinsdemo.git, The address accessed by browser is :
The code is simple , only one controller, receive http Request to return a string , This is shown below :
@RestController public class Hello { @RequestMapping("/") public String
sayHello(){ return "3. Hello jenkins, " + new Date(); } }
You need to GitHub Create a project , The source code can refer to the above demo Source code of the project ;

Combing the practical steps

The whole actual combat process is as follows :
1. GitHub Last one spring boot Of web engineering ;
2. GitHub Top configuration Jenkins Of webhook address ;
3. stay GitHub Create a access token,Jenkins Use this when doing some operations that require permission access token De authentication ;
4. Jenkins install GitHub Plugin plug-in unit ;
5. Jenkins to configure GitHub Access rights ;
6. Jenkins Create a build project on , The corresponding source code is the step 1 In web engineering ;
7. modify web Source code of the project , And submit to GitHub upper ;
8. inspect Jenkins Whether the build project of is triggered to build automatically , After successful construction , Download project operation , See if it's built on the latest code ;

webhook address

webhook It's a notice Jenkins Request address at , Used to fill in GitHub upper , such GitHub Can be notified through this address Jenkins;
hypothesis Jenkins The address of the server is :, Port is 8080, that webhook The address is <>

Remind again , The above address must be accessible from the Internet , otherwise GitHub Unable to access Jenkins;

to configure GitHub

* Sign in GitHub, Enter the project to be used in this construction ;
* On the project main page, click the ”Settings”, Click left again ”Webhooks”, Then click “Add webhook”, As shown below :

* As shown below , stay ”Payload URL” Location fill webhook address , Click on the bottom ”Add
webhook Button ”, That's it webhook Configured , Code submission for current project in the future ,GitHub That's where you're going webhook Address request , notice Jenkins structure :

generate Personal access tokens

Jenkins visit GitHub At the time of the project , Some operations need authorization , So we have to GitHub Generate authorized on token to Jenkins use , This is it. Personal
access tokens, The generation steps are as follows :
1. Sign in GitHub, get into ”Settings” page , Click on the ”Developer settings”, As shown below :

2. Jump to ”Developer settings” After page , Click on the “Personal access tokens”, As shown below :

3. Jump to ”Personal access tokens” After page , Click on the ”Generate new token” Button , As shown below :

May prompt for input GitHub password , Jump to create after entering token Page of , As shown in the figure below , input title, Check again ”repo” and ”admin:repo_hook”, Click on the bottom ”Generate
token” Button , You can create a new one access token, Copy this string , behind jenkins It will be used in the task :

Jenkins to configure

* GitHub Plugin plug-in unit , stay ” system management -> Manage plug-ins ” Location check if this plug-in is installed , If not, please install it first ;
* to configure GitHub, click “ system management -> System settings ”, As shown below :

* Found on the system settings page ”GitHub”, Configure a ”GitHub Server”, As shown below ,”API URL” fill in ”https://api.github.com
<https://api.github.com>“,”Credentials” The position is shown in the red box below , choice ”Add->Jenkins”:

* In the pop-up page ,”Kind” choice ”Secret text”,”Secret” Fill in the front GitHub Generated on Personal access
tokens,Description Write a description , As shown below :

* After completion , Click on the right ”Test connection” Button , If the information is correct , The displayed content is shown in the following figure :

* Click on the bottom of the page ” preservation ” Button ;
GitHub Project home page and warehouse address of the project

The project home page and warehouse address are different , As shown below , Red box 1 Project home page in , Click on the red box 2 After the button of , Red box 3 Warehouse address in ( To use HTTPS Of , no need SSH Of ):

Project Home :https://github.com/zq2599/jenkinsdemo
Warehouse address :https://github.com/zq2599/jenkinsdemo.git

New build project

stay Jenkins Last New maven Build project , be known as test003, As shown below :

Next set up test003 Configuration information for , Divided into ” Source management settings ” and ” Build environment settings ” Two parts ;

Source management settings

The settings of each red box in the above figure are explained as follows :
1. choice ”Git”;
2. “Repository URL” Enter warehouse address :https://github.com/zq2599/jenkinsdemo.git
3. “Credentials” Create a Credentials,Kind choice ”Username with
password”,Username input GitHub account number ,Password input GitHub password ;
4. “ Source library browser ” choice ”githubweb”;
5. “URL” Enter project home page :https://github.com/zq2599/jenkinsdemo
6. “ Build trigger ” Middle check ”GitHub hook trigger for GiTScm polling”;

Build environment settings

As shown in the figure below , Tick ”Use secret text(s) or file(s)”, Below ”Credentials” Select the one we configured before ”Personal
access tokens”

After setting up , Click at the bottom of the page ” preservation ” Button ;

modify web Project code and submit to GitHub

take GitHub Code of warehouse clone To local , Make some changes and submit to GitHub upper , For example, I modified it Hello.java Source code in ( Modified sayHello Methodical return The contents of the string for ), As follows :
@RestController public class Hello { @RequestMapping("/") public String
sayHello(){ // Modify the contents of the returned string return "abcdefg. Hello jenkins, " + new Date(); } }
Submit to GitHub The operation is shown in the figure below :

Jenkins Auto build

go back to Jenkins Of test003 page , You can see that it's started to build automatically , As shown below :

Verify auto build results

* stay test003 Page of , click ” working space ”->” work area ”, As shown below :

* Click in the red box below ”mavendockerplugindemo-0.0.1-SNAPSHOT.jar”, The browser will download this file :

* After downloading successfully , stay mavendockerplugindemo-0.0.1-SNAPSHOT.jar Execute command in directory java -jar
mavendockerplugindemo-0.0.1-SNAPSHOT.jar,web App started successfully , As shown below :

* Browser input localhost:8080, You can see web The content returned by the app is the latest content we just submitted , As shown below :

thus ,GitHub Commit trigger Jenkins The actual battle of automatic construction is completed , I hope it can help you to build a sustainable construction environment .