Jenkins made the GitHub project Docker image

This article is "Jenkins pipeline (pipeline) combat," the third in a series, previously have a basic understanding of Jenkins pipeline, also tried to download the script from GitHub and execution pipeline, combat today is to write a script for a pipeline, the role of the script download a Java program (Maven build) from GitHub, then edit the project build and build results Docker made into mirror image and push private warehouse;

Series of articles Address

  1. "Jenkins pipeline (pipeline) of combat: from deployment to experience" ;
  2. "Let Jenkins execution pipeline scripts on GitHub" ;
  3. "Jenkins made the GitHub project Docker image" ;

    Environmental Information

    In order to quickly deploy Jenkins, this practical uses Docker, please have the following environment before the actual combat:
  4. Operating System: Ubuntu 18.04.2 LT
  5. Jenkins:2.176.3
  6. Docker:19.03.1
  7. Private mirror warehouse: Harbor 1.9.0

    Introduction combat

    Today's actual function is shown below:
    Here Insert Picture Description

    About Docker private warehouse mirror Harbor

    The constructed Docker mirror, the mirror will be pushed to Docker warehouse Harbor, this is my own warehouses built locally, please refer to the detailed steps to build a "CentOS deployment Harbor image warehouse"
    If you have an account hub.docker.com site, also the mirror can be directly pushed to this site, so we can use a mirror up to you to make.

    The project on GitHub used

    This will be a real project on GitHub compile, and then constructed Docker mirror and I am prepared SpringBoot a project based on Maven build, it is a Hello world level project, the only special is used pom.xml to Jib plug-in for the project to build SpringBoot Docker image.

The project's source code can be downloaded from GitHub, address and link information in the following table:
| Name | link | Note |
|: -------- |: ---- |: ---- |
| project Home | https://github.com/zq2599/blog_demos | the project's home page on GitHub |
| git repository address (https) | https://github.com/zq2599/blog_demos.git | the project's source code repository address, https protocol |
| git repository address (ssh) | [email protected]: zq2599 / blog_demos.git | the project's source code repository address, ssh protocol |

The git project has multiple folders, applications under this chapter in hellojib folder, as shown below:
Here Insert Picture Description
a look at pom.xml this project, focusing on Jib plug-in part, as follows:

<!--使用jib插件-->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.3.0</version>
                <configuration>
                    <!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
                    <from>
                        <!--使用openjdk官方镜像,tag是8-jdk-stretch,表示镜像的操作系统是debian9,装好了jdk8-->
                        <image>openjdk:8-jdk-stretch</image>
                    </from>
                    <to>
                        <!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
                        <image>bolingcavalry/hellojib:${project.version}</image>
                    </to>
                    <!--容器相关的属性-->
                    <container>
                        <!--jvm内存参数-->
                        <jvmFlags>
                            <jvmFlag>-Xms4g</jvmFlag>
                            <jvmFlag>-Xmx4g</jvmFlag>
                        </jvmFlags>
                        <!--要暴露的端口-->
                        <ports>
                            <port>8080</port>
                        </ports>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>

Use Jib will SpringBoot project to construct more information Docker mirroring, refer to "Docker and Jib (maven plug-in version) combat" ;

With private GitHub repository to combat

  1. Taking into account the actual work most of the source code are not open to the public, so I have here also built a Private GitHub repository, the address is: https: //github.com/zq2599/pipeline.git, hellojib engineering of all the previously mentioned this source are placed in a warehouse;
  2. Requires authentication pipeline to download the source code private warehouse, authentication will talk later.
  3. This private repository is not only hellojib a project under the root of the repository hellojib directory Maven project is the construction of the mirror will be used:
    Here Insert Picture Description

    Create a task Jenkins

  4. Create a file called build-image pipeline tasks:
    Here Insert Picture Description
  5. Access to our grammar tool to help Jenkins provided to generate the code to download the GitHub pipeline, as shown in the Page Setup task click on the red box assembly line syntax button:
    Here Insert Picture Description
  6. Jenkins is now entering the page where we write auxiliary pipeline scripts, operate according to the following chart:
    Here Insert Picture Description
  7. In the window holds a secret key configured configuration items GitHub account and password, follow the sequence numbers of the FIG:
    Here Insert Picture Description
  8. As shown in FIG red box, select just created in the pull-down menu keys arranged:
    Here Insert Picture Description
  9. At this point the red error can be found before has disappeared (as authenticated by a), and click the button under the map the red frame to generate pipeline Source:
    Here Insert Picture Description
  10. Complete pipeline script reads as follows, in which the Checkout stage downloaded from GitHub part of the script is automatically generated image above:
pipeline {
    agent any
    tools {
        maven 'mvn-3.6.2'
    }
    stages {
        stage('Checkout') {
            steps {
                echo '从GitHub下载项目源码'
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'git-account-passwd', url: 'https://github.com/zq2599/pipeline.git']]])
            }
        }        
        stage('Build') {
            steps {
                echo '开始编译构建'
                sh 'cd hellojib && mvn clean compile -U -DskipTests jib:dockerBuild'
            }
        }
        stage('Push') {
            steps {
                echo '将本地Docker镜像推送到Harbor镜像仓库'
                echo '给当前镜像设置带有Harbor地址和项目名称的Tag,这样才能推送到Harbor的library项目之下'
                sh 'docker tag bolingcavalry/hellojib:0.0.1-SNAPSHOT 192.168.50.167/library/hellojib:0.0.1-SNAPSHOT'
                echo '登录Harbor'
                sh 'docker login 192.168.50.167 -u admin -p Harbor12345'
                echo '登录chengg成功,开始推送镜像'
                sh 'docker push 192.168.50.167/library/hellojib:0.0.1-SNAPSHOT'
            }
        }
        stage('Clean') {
            steps {
                echo '清理Maven工程'
                sh 'cd hellojib && mvn clean'
                echo '删除镜像'
                sh 'docker rmi bolingcavalry/hellojib:0.0.1-SNAPSHOT 192.168.50.167/library/hellojib:0.0.1-SNAPSHOT'
                echo '清理完毕'
            }
        }
    }
}
  1. The above script to fill the pipeline build-image task pipeline script input box, as shown below:
    Here Insert Picture Description
  2. Click below the red box build immediately to begin the assembly line tasks:
    Here Insert Picture Description
  3. If the configuration is correct, then the task can be executed successfully, as shown, time-consuming and the results of each stage are show out:
    Here Insert Picture Description
  4. If you fail a mission architecture, click below the red box of small ball, it opens a detailed log of task execution, used to check the cause of the error:
    Here Insert Picture Description
  5. Login Harbor site, you can see the push over the mirror:
    Here Insert Picture Description
    ### The pipeline script on GitHub
    today's combat, pipeline scripting is saved in the settings task, remember "Let Jenkins execution pipeline on GitHub script " one of the contents of it? You can try to today's pipeline scripts stored on GitHub, so from a business project build scripts to implement version control, more practical than in the development phase.

At this point, based on actual GitHub project pipeline operation is complete, CI if you are looking for container environments && CD program, this article would like to give you some reference.

Guess you like

Origin www.cnblogs.com/bolingcavalry/p/11568774.html