When we submit code toGitHub after, Can be inJenkins Build on, But it's a little troublesome to do it every time, Today we're going to fightJenkins Automatic building of, Every time code is submitted toGitHub 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 LowerJenkins Installation and experience》
Do the actual battle of installation and experience, So as toJenkins Preliminary understanding of the service;

Important premise

GitHub Active notification after receiving submitted codeJenkins, thereforeJenkins The server must have an external networkIP, otherwiseGitHub cannot access, MyJenkins The server is deployed on the virtual machine of Tencent cloud, With external networkIP;
* The source code should be submitted toGitHub, So you need to have aGitHub Number, And create a project on it;
demo Engineering source code

This time, it is used toJenkins The project built on is aspringboot Ofweb engineering, The address is:[email protected]:zq2599/jenkinsdemo.git, The address accessed by browser is:
The code is simple, Only onecontroller, Receivehttp Request to return a string, As shown below:
@RestController public class Hello { @RequestMapping("/") public String
sayHello(){ return "3. Hello jenkins, " + new Date(); } }
You need toGitHub Create a project, The source code can refer to the abovedemo Source code of the project;

Combing the practical steps

The whole actual combat process is as follows:
1. GitHub Last onespring boot Ofweb engineering;
2. GitHub Upper configurationJenkins Ofwebhook address;
3. stayGitHub Create aaccess token,Jenkins Use this when doing some operations that require permissionaccess token Authentication;
4. Jenkins installGitHub Plugin Plug-in unit;
5. Jenkins To configureGitHub Access right;
6. Jenkins Create a build project on, The corresponding source code is the step1 Mediumweb engineering;
7. modifyweb Source code of the project, And submit toGitHub upper;
8. inspectJenkins 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 inGitHub upper, suchGitHub Can be notified through this addressJenkins;
hypothesisJenkins The address of the server is:, Port is8080, thatwebhook The address is <>

Remind again, The above address must be accessible from the Internet, otherwiseGitHub Unable to accessJenkins;

To configureGitHub

* Sign inGitHub, 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”, Following chart:

* Following chart, stay”Payload URL” Position fillingwebhook address, Click on the bottom”Add
webhook Button”, That's itwebhook Configured, Code submission for current project in the future,GitHub It will go to this.webhook Address request, noticeJenkins structure:

generatePersonal access tokens

Jenkins VisitGitHub At the time of the project, Some operations need authorization, So we have toGitHub Generate authorized ontoken toJenkins Use, This is it.Personal
access tokens, The generation steps are as follows:
1. Sign inGitHub, Get into”Settings” page, Click on the”Developer settings”, Following chart:

2. Jump to”Developer settings” Post page, Click on the“Personal access tokens”, Following chart:

3. Jump to”Personal access tokens” Post page, Click on the”Generate new token” Button, Following chart:

May prompt for inputGitHub Password, Jump to create after enteringtoken Page, As shown in the figure below, inputtitle, Check again”repo” and”admin:repo_hook”, Click on the bottom”Generate
token” Button, You can create a new oneaccess token, Copy this string, behindjenkins It will be used in the task:

Jenkins To configure

* GitHub Plugin Plug-in unit, stay” system management-> Management plug-in” Location check if this plug-in is installed, If not, please install it first;
* To configureGitHub, click“ system management-> System setup”, Following chart:

* Found on the system settings page”GitHub”, Configure one”GitHub Server”, Following chart,”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 frontGitHub Generated abovePersonal access
tokens,Description Write a description, Following chart:

* 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, Following chart, Red box1 Project home page in, Click the red box.2 After the button, Red box3 Warehouse address in( To useHTTPS Of, No needSSH Of):

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

New build project

stayJenkins Last Newmaven Construction project, be known astest003, Following chart:

Next set uptest003 Configuration information for, Divided into” Source management settings” and” Build environment settings” The two part;

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 aCredentials,Kind Choice”Username with
password”,Username inputGitHub Account number,Password inputGitHub Password;
4. “ Source library browser” Choice”githubweb”;
5. “URL” Enter project home page:https://github.com/zq2599/jenkinsdemo
6. “ Build trigger” Middle hook”GitHub hook trigger for GiTScm polling”;

Build environment settings

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

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

modifyweb Project code and submit toGitHub

takeGitHub Code of warehouseclone To local, Make some changes and submit toGitHub upper, For example, I modified itHello.java Source code( RevisedsayHello Methodreturn The contents of the string for), As shown below:
@RestController public class Hello { @RequestMapping("/") public String
sayHello(){ // Modify the contents of the returned string return "abcdefg. Hello jenkins, " + new Date(); } }
Submit toGitHub The operation is shown in the figure below:

Jenkins Automatic construction

Go back toJenkins Oftest003 page, You can see that it's started to build automatically, Following chart:

Verify auto build results

* staytest003 Page, click” working space”->” work area”, Following chart:

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

* After downloading successfully, staymavendockerplugindemo-0.0.1-SNAPSHOT.jar Execute command in directoryjava -jar
mavendockerplugindemo-0.0.1-SNAPSHOT.jar,web App started successfully, Following chart:

* Browser inputlocalhost:8080, Can seeweb The content returned by the app is the latest content we just submitted, Following chart:

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