1, Jenkins+Docker+SpringCloud continuous integration process
Text flow
Developers submit their code to the Gitlab code warehouse every day.
Jenkins pulls the project source code from Gitlab, compiles it and makes it into a jar package, then builds it into a Docker image, and uploads the image to the private warehouse of Harbor.
Jenkins sends the SSH Remote command to let the production deployment server go to the Harbor private warehouse to pull the image locally, and then create the container.
Finally, users can access the container
2, Package micro service projects with Jenkins
1. Generate common sub project pom file
1.1. Modify Jenkinsfile
def git_auth="e6ede7de-abef-449e-be67-a9a72feb5fb2" def git_url="git@192.168.199.141:major/tensquare_back_New.git" node { stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '${git_auth}', url: '${git_url}']]]) } stage('check code') { //Define the SonarQubeScanner tool def scannerHome = tool 'sonar-scanner' //Reference SonarQube system environment withSonarQubeEnv('sonarqube') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } //Add public subproject stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } }
1.2. Modify each POM xml
parent project POM The maven plug-in code in XML is moved to tensquare_ POM for each subproject other than common In XML
Modify tensquare_ POM of parent xm
<build> <plugins> <plugin> <!--Provide packaging(Package the application into executable jar package)--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
common is not a running microservice, so there is no need to add a springboot Maven plug-in
Other micro service configurations
After finishing, submit it to gitlab
Jenkins builds it again and builds the eureka service first
2. Start compiling and packaging all microservice projects
2.1 modify Jenkinsfile and submit
def git_auth="e6ede7de-abef-449e-be67-a9a72feb5fb2" def git_url="git@192.168.199.141:major/tensquare_back_New.git" node { stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '${git_auth}', url: '${git_url}']]]) } stage('check code') { //Define the SonarQubeScanner tool def scannerHome = tool 'sonar-scanner' //Reference SonarQube system environment withSonarQubeEnv('sonarqube') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } //Add public subproject stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //Packaged microservice project stage('make package') { sh "mvn -f ${project_name} clean package" } }
Build the project again and build the eureka service first
Continue building tensquare_zuul service gateway
Reason: there is no tensquare in Jenkins server_ Paren project, and zuul needs to rely on this project solution: pass the parent project into Jenkins warehouse
[root@jenkins target]# cd /root/repo/
[root@jenkins repo]# ls
[root@jenkins repo]# cd com
[root@jenkins com]# cd tensquare/
At this time, the parent project directory is passed in
Continue packaging zuul service gateway
Continue to package permission Center Service
Finally, package the activity micro service
3, Using docker made plugin to build docker image
1. In the POM of each microservice project Add dockerfile Maven plugin to XML
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin </artifactId> <version>1.3.6</version> <configuration> <repository>${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin>
2. Create a dockerfile file in the root directory of each microservice project
#FROM java:8 FROM openjdk:8-jdk-alpine ARG JAR_FILE COPY ${JAR_FILE} app.jar EXPOSE 10086 ENTRYPOINT ["java","-jar","/app.jar"]
Note that each project exposes different ports
3. Modify Jenkinsfile build script
def git_auth="e6ede7de-abef-449e-be67-a9a72feb5fb2" def git_url="git@192.168.199.141:major/tensquare_back_New.git" node { stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '${git_auth}', url: '${git_url}']]]) } stage('check code') { //Define the SonarQubeScanner tool def scannerHome = tool 'sonar-scanner' //Reference SonarQube system environment withSonarQubeEnv('sonarqube') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } //Add public subproject stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //Packaged microservice project stage('make package') { sh "mvn -f ${project_name} clean package dockerfile:build" } }
Submit Jenkinsfile
Build eureka service and build all four
4. Upload warehouse
4.1. Modify Jenkinsfile
Add tagging steps for uploading images to harbor warehouse
def git_auth="e6ede7de-abef-449e-be67-a9a72feb5fb2" def git_url="git@192.168.199.141:major/tensquare_back_New.git" /Mirror label def tag="latest" //url address of harbor def harbor_url="192.168.199.139:85" //Image warehouse name def harbor_name="tensquare" node { stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '${git_auth}', url: '${git_url}']]]) } stage('check code') { //Define the SonarQubeScanner tool def scannerHome = tool 'sonar-scanner' //Reference SonarQube system environment withSonarQubeEnv('sonarqube') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } //Add public subproject stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //Package micro services and make images stage('make package') { sh "mvn -f ${project_name} clean package dockerfile:build" //Define image name def imageName="${project_name}:${tag}" //Label images sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}" } }
Submit Jenkinsfile and pass Eureka test results
4.2. Use vouchers to manage Harbor private server account and password
def git_auth="e6ede7de-abef-449e-be67-a9a72feb5fb2" def git_url="git@192.168.199.141:major/tensquare_back_New.git" def tag="latest" //url address of harbor def harbor_url="192.168.199.139:85" //Image warehouse name def harbor_name="tensquare" //Certificate of harbor def harbor_auth="4fafa4e1-46ad-4c24-824b-15ba0bacbf03" node { stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '${git_auth}', url: '${git_url}']]]) } stage('check code') { //Define the SonarQubeScanner tool def scannerHome = tool 'sonar-scanner' //Reference SonarQube system environment withSonarQubeEnv('sonarqube') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } //Add public subproject stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //Package micro services, create images and upload images stage('make package images,push images') { sh "mvn -f ${project_name} clean package dockerfile:build" //Define image name def imageName="${project_name}:${tag}" //Label images sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}" //Push image to harbor withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) { //Log in to harbor sh "docker login -u ${username} -p ${password} ${harbor_url}" //Image upload sh "docker push ${harbor_url}/${harbor_name}/${imageName}" sh "echo Image upload succeeded" } } }
Jenkins submits, tests the image, and uploads all four microservices
4, Install the publish Over SSH plug-in
Remember to restart
Configure remote deployment server
Copy public key from Jenkins server to production server
ssh-copy-id 192.168.199.140
def git_auth="e6ede7de-abef-449e-be67-a9a72feb5fb2" def git_url="git@192.168.199.141:major/tensquare_back_New.git" def tag="latest" //url of harbor def harbor_url="192.168.199.139:85" //Image warehouse name def harbor_name="tensquare" //Certificate of harbor def harbor_auth="4fafa4e1-46ad-4c24-824b-15ba0bacbf03" node { stage('pull code') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '${git_auth}', url: '${git_url}']]]) } stage('check code') { //Define the SonarQubeScanner tool def scannerHome = tool 'sonar-scanner' //Reference SonarQube system environment withSonarQubeEnv('sonarqube') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } //Add public subproject stage('make install public sub project') { sh "mvn -f tensquare_common clean install" } //Package micro services, create images and upload images stage('make package images,push images') { sh "mvn -f ${project_name} clean package dockerfile:build" //Define image name def imageName="${project_name}:${tag}" //Label images sh "docker tag ${imageName} ${harbor_url}/${harbor_name}/${imageName}" //Push image to harbor withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) { //Log in to harbor sh "docker login -u ${username} -p ${password} ${harbor_url}" //Image upload sh "docker push ${harbor_url}/${harbor_name}/${imageName}" sh "echo Image upload succeeded" } //Deploy application sshPublisher(publishers: [sshPublisherDesc(configName: 'master_server', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deploy.sh ${harbor_url} ${harbor_name} ${project_name} ${tag} ${port}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } }
Add port parameters
Deploy in production environment script deploy sh
#! /bin/sh #Receive external parameters harbor_url=$1 harbor_project_name=$2 project_name=$3 tag=$4 port=$5 imageName=$harbor_url/$harbor_project_name/$project_name:$tag echo "$imageName" #Query whether the container exists, and delete it if it exists containerId=`docker ps -a | grep -w ${project_name}:${tag} | awk '{print $1}'` if [ "$containerId" != "" ] ; then #Stop the container docker stop $containerId #Delete container docker rm $containerId echo "Successfully deleted container" fi #Query whether the image exists, and delete it if it exists imageId=`docker images | grep -w $project_name | awk '{print $3}'` if [ "$imageId" != "" ] ; then #delete mirror docker rmi -f $imageId echo "Mirror deleted successfully" fi # Log in to Harbor docker login -u admin -p Harbor12345 $harbor_url # Download Image docker pull $imageName # Start container docker run -di -p $port:$port $imageName echo "Container started successfully"
Create a directory on the production server and put it into the script
Jenkins file build
Check the results on the production server
View services