Docker + Jenkins + Github to automatically build a maven project

initial preparation work

  1. Linux system (accessible from the external network, if it is Alibaba Cloud, you need to open the port)
  2. Docker (Requires docker foundation)
  3. Jenkins (based on docker container)
  4. Maven project uses github management
Release project ideas:
  1. Use dockerfile to write the image and execute the jar package;
  2. Run the mirror and mount the project path;
  3. After Jenkins is automatically built, upload the jar package to the mounted path through the configured SSH;
  4. Execute the restart container command after uploading;
  5. Dockerfile written:
FROM java:8
RUN cd /
RUN mkdir projects
WORKDIR /projects
EXPOSE 90
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","tuding.jar"]
  1. Run the container
docker run --name=tuding-1 -d -p 90:90 -v /usr/local/docker_volume/projects/tuding:/projects tuding-1

ps: Running the container here will fail, because there is no jar package yet, only a container is needed here, waiting for jenkins to build and upload the jar package, and execute the container restart command.

Goals

Local code modification, submit it to the Github remote warehouse, to complete the automated deployment.

Step combing

  1. The entire process is as follows:
  2. Prepare a spring boot web project on GitHub;
  3. Create an access token on GitHub, and use this access token to authenticate when Jenkins does some operations that require permissions;
  4. Configure the Jenkins webhook address on GitHub;
  5. Jenkins installs GitHub Plugin;
  6. Jenkins configures GitHub access permissions;
  7. Create a build project on Jenkins, the corresponding source code is the web project in step 1;
  8. Modify the source code of the web project and submit it to GitHub;
  9. Check whether the Jenkins build project is triggered to build automatically. After the build is successful, visit the project to see if it is based on the latest code;

webhook address

The webhook is the requested address when notifying Jenkins, used to fill in GitHub, so that GitHub can notify Jenkins through this address;
assuming that the address of the server where Jenkins is located is: 192.168.15.11, the port is 8080, then the webhook address is http:/ /192.168.15.11:8080/github-webhook

Again, the above address must be accessible from the external network, otherwise GitHub cannot access Jenkins;

1. Use IDEA to pull Github source codeInsert picture description here![Insert picture description here](https://img-blog.csdnimg.cn/20200528194825552.pngInsert picture description here

Demo project source code

  1. The project used to build on Jenkins this time is a springboot web project. The
    code is very simple, with only one controller, receiving an http request and returning a string, as shown below:
@RestController
public class Controller {
    
    

    Date date = new Date();
    @RequestMapping("hello")
    public String hello(String name){
    
    
        return name + ",<font color='red'>您好:</font>" + date.getTime();
    }
    
}
  1. Local test, http request access
    ![Insert picture description here](https://img-blog.csdnimg.cn/20200528195711656.png

Two, configure GitHub

1. Generate Personal access tokens
  1. When Jenkins accesses the GitHub project, some operations require authorization, so we need to generate authorized tokens on GitHub for Jenkins to use. This is Personal access tokens. The generation steps are as follows:

Log in to GitHub, enter the "Settings" page, and click "Developer settings" in the lower left corner, as shown below:
Insert picture description here

  1. After jumping to the "Developer settings" page, click "Personal access tokens" in the lower left corner, as shown below:
    Insert picture description here
  2. After jumping to the "Personal access tokens" page, click the "Generate new token" button in the upper right corner, as shown below:Insert picture description here
  3. You may be prompted to enter the GitHub password. After entering it, you will be redirected to the token creation page, as shown in the figure below, enter the title, and then check "repo" and "admin:repo_hook", and then click the "Generate token" button at the bottom. Generate a new access token and copy this string down, which will be used in the jenkins task later:
    Insert picture description here
2. Configure Github Webhooks (hooks, if new code is submitted, jenkins will be notified)
  1. On the main page of the project, click "Settings" in the upper right corner, then click "Webhooks" on the left, and then click "Add webhook", as shown below:
    Insert picture description here
  2. As shown in the figure below, fill in the webhook address in the "Payload URL" position, and then click the "Add webhook button" at the bottom to complete the webhook configuration. In the future, if the current project has code submission, GitHub will send a request to this webhook address to notify Jenkins Construct:
    Insert picture description here
    This step of configuring Github Webhooks (hooks) actually does not need to be configured. If configured, 405 cannot be accessed. After configuring in the jenkins configuration below, Github will automatically create hooks

Three, Jenkins configuration

1. Configure the domestic source (if you need to download the plug-in, you can configure it to increase the downloading speed of the plug-in)
  1. Click "System Management -> Manage Plugins -> Advanced", as shown below:
    Insert picture description here
    Insert picture description here
  2. Enter the domestic source address: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
    Insert picture description here
Two, system settings
  1. GitHub Plugin plug-in, check whether this plug-in has been installed in the "System Management -> Manage Plugin" location, if not, please install it first; Insert picture description hereInsert picture description here
    enter the filter plug-in in the red box 4:

    1. Search Git, select all, click the red box 5 to download, configure github to use;
    2. Search SSH, select all, click the red box 5 to download, configure the remote server to be used;
    3. Search for Maven, select all, click the red box 5 to download, configure the project Maven needs to use;
    4. Search for Locale, select all, click the red box 5 to download, configure the internationalized Chinese to be used;
    5. The above downloads can be downloaded together in parallel.
  2. Configure internationalization, click "System Management -> System Settings", as shown below:
    Insert picture description here
    Insert picture description here
    zh_CN: Simplified Chinese
    zh_TW: Traditional Chinese

  3. Configure GitHub, click "System Management -> System Settings", find "GitHub" on the system settings page, configure a "GitHub Server", as shown in the figure below, "API URL" fill in "https://api.github.com", " The location of "Credentials" is shown in the red box in the figure below, select "Add->Jenkins":
    Insert picture description here

  4. In the page that pops up, select "Secret text" for "Kind", fill in "Secret" with the Personal access tokens previously generated on GitHub, and write some descriptions in the Description, as shown below:
    Insert picture description here

  5. After writing, click the "Test connection" button on the right. If the information is not filled in, the displayed content is as shown in the figure below:
    (ps: Click Test connection may fail, switch the Credentials drop-down box and try several times)
    Insert picture description here

  6. Configure the login SSH Server information, describe which server and path to upload the constructed jar package to, as shown in the following figure: The
    Insert picture description here
    above explanation:

    1. Name: ssh name, you can fill in at will;
    2. Hostname: remote server address;
    3. Username: remote server account;
    4. Remote Directory: The path to upload to the remote server (here is the global configuration, which will affect all the projects that select this SSH Server, which will be explained later)
    5. Click the red box 2 to enter the remote server login password, as shown in the figure below, and click the "Test Configuration" button on the right to test:
      Insert picture description here
  7. Click the "Save" button at the bottom of the page;

3. Obtain the Github project address
  1. The warehouse address of the GitHub project The
    project homepage and warehouse address are different, as shown in the figure below. After clicking the button in red box 2, the warehouse address in red box 3 (use HTTPS instead of SSH):
    Insert picture description hereInsert picture description here
    warehouse address: https:// github.com/a402942988/tuding.git
Four, jenkins new build project
  1. New on Jenkins maven build a project called tuding, as shown below:
    If there is no maven options: Maven Plugin plug-in, in the "System Management -> Manage plug-in" position to check whether the plug-in has been installed, if not please install;
    Insert picture description here
    then The configuration information set down is divided into two parts: "source code management settings" and "build environment settings";
Five, source code management settings

1. If you configure for the first time, you need to click Add on the right side in the red box 3, and enter the Githu login account and password
Insert picture description here
. 2. Check the Github hook
Insert picture description here
. The settings of each red box in the above figure are explained as follows:

  1. Select "Git";
  2. "Repository URL" input warehouse address: https://github.com/a402942988/tuding.git;
  3. "Credentials" creates a Credentials, Kind selects "Username with password", Username enters GitHub account, Password enters GitHub password;
  4. Check "GitHub hook trigger for GiTScm polling" in "Build trigger";
Six, build environment settings
  1. As shown in the figure below, check "Use secret text(s) or file(s)", and select the "Personal access tokens" we have previously configured for "Credentials" below
    Insert picture description here
    Insert picture description here

  2. Maven settings, as shown below:Insert picture description here

  3. Configure the operation after the build and prepare to upload to the remote Linux Docker mount directory, as shown in the following figure: The
    Insert picture description here
    Insert picture description here
    above steps are explained:

    1. Select "Execute both if the build is successful or unstable";
    2. 选择“Send files or execute commands over SSH”;
    3. Select the SSH configuration (that is, the "GitHub Server" configured above) from the list configured in the Jenkins global configuration;
    4. Select the relative path of the jar package (this time the jar location is under the demo-www/target/ directory);
    5. Remove the prefix, that is, only leave the jar package name;
    6. For the commands executed after the upload is successful, use docker to restart the container;
  4. After setting, click the "Save" button at the bottom of the page;

  5. After the configuration is completed, check Github to see if the hook is automatically created successfully. If the following figure is displayed, it means success:
    "✔" is displayed in the purple box, indicating that Github can notify jenkins
    Insert picture description here

Seven, build and run

  1. Click Build, as shown below:
    Insert picture description here

  2. Wait a few seconds, the build progress bar appears in the lower right corner, as shown in the figure below:
    Insert picture description here

  3. Click the project name, enter the following figure, click to view the build log:
    Insert picture description here

  4. Check the log, as shown in the figure below:
    red box 1: indicates successful construction;
    red box 2: indicates the project path after construction;
    red box 3: indicates upload to the linux server and execute the command;
    red box 4: indicates successful execution;
    Insert picture description here

  5. Visit the remote http address to check whether the release is successful:
    Insert picture description here

  6. Modify the local code, as shown below:

@RestController
public class Controller {
    
    

    Date date = new Date();
    @RequestMapping("hello")
    public String hello(String name){
    
    
        return name + ",<font color='red'>您好,我修改了内容:</font>" + date.getTime();
    }
    
}
  1. After Git submits the code, jenkins will automatically build
    (the principle is that after the code is submitted to the Github warehouse, Github notifies jenkins through the configured webhook address, and jenkins automatically builds after receiving the request)
    Insert picture description here
  2. Check the Github notification log and be notified successfully, as shown in the following figure:
    Insert picture description here
  3. Check whether the automated build is successful:Insert picture description here

end

So far, github+jenkins+docker automated construction of Maven project has been achieved. This structure can run in small and medium-sized companies without any problems. If the performance cannot keep up due to frequent builds, etc., it can be expanded on this structure to add jenkins clusters and docker servers.

Guess you like

Origin blog.csdn.net/a251628111/article/details/106413844