Build a set based Docker Jenkins CI CD devOpts environment

0. Introduction

In the Internet era, for each company, the importance of software development and release of the self-evident, has formed a set of standard procedures, the most important part is the continuous integration (CI) and continuous deployment, delivery (CD). Based Jenkins + Docker + Github CI achieve an automated publishing process.
  This article would like to introduce, for no contact CI, CD of the students, based on how Jenkins + Docker + Github to build an automated publishing process, the paper will be a simple Java Web project as an example, each Push code to github, jenkins will re pull code and finish the UT IT test, then the latest war package build good, publish to the docker container.

5977533-73980624e36ef787.png
image.png

1. environmental structures

1.1 Docker installation

My environment is CentOS7.5, first install Docker on the server. Specific steps can refer to the following article:
Docker Getting Started Installation Guide

1.2 Jenkins installation

  1. docker very easy to install jenkins execute the following script on OK

docker run --name devops-jenkins --user=root -p 8080:8080 -p 50000:50000 -v /opt/data/jenkins_home:/var/jenkins_home -d jenkins/jenkins:lts

  1. registry installation, the purpose of this step is to do as a developer, will naturally make yourself some images, distributed in the company intranet, or the development team to mirror, and then delivered to the test team for testing, which requires an in-house distribution mirrored server, the Docker team naturally thought of, and provides a mirror retistry , put this image or leave a local

docker run --name devops-registry -p 5000:5000 -v /opt/devdata/registry:/var/lib/registry -d registry

  1. After starting the finished jenkins enter an address via the browser:

http: // deploy jenkins Host IP: port

  1. The administrator password prompt or may be input by actuating the log

docker logs devops-jenkins


5977533-c3d5a93f3da39f14.png
image.png
  1. Plug into the installation interface, networking wait for plug-in installation, after installing the plug-in, enter the administrator interface to create, after you enter the administrator account, click continue as admin installation is complete, if you want to install some plug-ins, you can Manage jenkins => Manage Plugin modified.


    5977533-7e3eb66f2b89243c.png
    image.png

1.3 Java Projects

This is not described in detail, is the use of a simple Eclipse built a simple Maven (Spring Boot) project, contains a simple Controller and UT, IT test case, the project is structured as follows:

5977533-a12e32356b36ee62.png
image.png

Github following address: https://github.com/Syfhehe/CI-CD-Demo

2. Jenkins Pipeline

2.1 Jenkins Pipeline get through Github integrated bridge.

  1. Maven configuration
    enter Jenkins -> Global Tool Configuration, add a Maven.
5977533-9ab25ee9abd54229.png
image.png
  1. Enter Github -> Setting -> Develper Settings -> Personal Access Token -> Generate new token, generate a new Token.
    5977533-6e1680da9ad3f929.png
    image.png

    His first save this token, if you lose, then could not find it token.
5977533-76babf581a9a8b3b.png
image.png

3. In order to capture Pipeline warehouse events (such as push), should be properly set up Jenkins GitHub webhook on GitHub.

Set xxx / github-webhook / in Payload URL, as follows:


5977533-eb19bffee364ea0d.png
image.png
  1. Log Jenkins, go to System -> System Settings -> GitHub -> Add GitHub Sever, add a Github Server

    5977533-ec8f92d57aa5944a.png
    image.png

    API URL input https://api.github.com , click Add Credentials added, Kind select Secret Text, specifically as shown below.
    5977533-aa0c1d1c90b7229f.png
    image.png

    After the setup is complete, click on the TestConnectionprompt Credentials verified for user UUserName, rate limit: xxx, it indicates that effective.

  2. Adding a Maven's installation in the Global Tool Configuration.


    5977533-88d7578df32420d5.png
    image.png

2.2 The establishment of a Github has push event, Jenkins Pipeline will run automatically Jenkins Item

1. In the root directory of your Git repositories before we built the Java project Create and save a text file named Jenkinsfile of.
Copy the following declarative lines of code and paste it into Jenkinsfile file:

pipeline {
   agent any

   stages {
       stage ('Compile Stage') {

           steps {
               withMaven(maven : 'Maven-3.6.1') {
                   sh 'mvn clean compile'
               }
           }
       }

       stage ('Testing Stage') {

           steps {
               withMaven(maven : 'Maven-3.6.1') {
                   sh 'mvn test'
               }
           }
           post {
               always {
                   junit 'target/surefire-reports/*.xml' 
               }
           }
       }
       
   }
}

2. Create a Pipeline on Jenkins. Name casually.


5977533-26178fac89c7fe61.png
image.png

3. Check GitHub hook trigger for GITScm polling, so every push will survive a jenkinsfile script.


5977533-b3d633fab0a49a2e.png
image.png

If you want to pipeline execution Jenkinsfile script needs the following settings, Credentials here in the last chapter is generated.
5977533-512d08265f900248.png
image.png
  1. Save and exit, and then just push a little modification to github, you will find Jenkins Pipeline will automatically build.


    5977533-a3f022c76ac79c24.png
    image.png

The first half of this CI-CD: CI, even completed.

To Be Continue.....

Reference documents:

Reproduced in: https: //www.jianshu.com/p/f087e78aac45

Guess you like

Origin blog.csdn.net/weixin_33755557/article/details/91180023