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.
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
- 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
- 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
- After starting the finished jenkins enter an address via the browser:
http: // deploy jenkins Host IP: port
- The administrator password prompt or may be input by actuating the log
docker logs devops-jenkins
-
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.
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:
Github following address: https://github.com/Syfhehe/CI-CD-Demo
2. Jenkins Pipeline
2.1 Jenkins Pipeline get through Github integrated bridge.
- Maven configuration
enter Jenkins -> Global Tool Configuration, add a Maven.
- Enter Github -> Setting -> Develper Settings -> Personal Access Token -> Generate new token, generate a new Token.
His first save thistoken
, if you lose, then could not find ittoken
.
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:
-
Log Jenkins, go to System -> System Settings -> GitHub -> Add GitHub Sever, add a Github Server
API URL input https://api.github.com , click Add Credentials added, Kind select Secret Text, specifically as shown below.
After the setup is complete, click on theTestConnection
promptCredentials verified for user UUserName, rate limit: xxx
, it indicates that effective. -
Adding a Maven's installation in the Global Tool Configuration.
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.
3. Check GitHub hook trigger for GITScm polling, so every push will survive a jenkinsfile script.
If you want to pipeline execution Jenkinsfile script needs the following settings, Credentials here in the last chapter is generated.
-
Save and exit, and then just push a little modification to github, you will find Jenkins Pipeline will automatically build.
The first half of this CI-CD: CI, even completed.
To Be Continue.....
Reference documents:
Reproduced in: https: //www.jianshu.com/p/f087e78aac45