In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use Jenkins packaging micro-services to build Docker image run", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Jenkins packaging micro-services to build Docker image run" this article.
Environmental preparation
1.Jenkins
2.GitLab
3.SonarQube
4.Harbor
5.Docker
6.Maven
7.JDK1.8
8. Micro service project
The above technical part has been written in my previous article.
Here is not too much detailed demonstration, this article is mainly a consistent micro-service project source code-> submit remote repository GitLab- > Jenkins from Gitlab pull replacement code to the server-> SonarQube code review-> Maven compilation package-> build Docker image-> push private Harbor image repository-> other server deployment
Start 1.GitLab remote warehouse to create micro-service project
2.IDEA code is pushed to Gitlab
If the default folder is right, you don't have to change it.
Add local warehouse
Submit to the local warehouse
Push remote warehouse
3.Jenkins creates an assembly line project
4. Parameterized construction
5. Pull the build script for the remote warehouse
Application saving
6. Write a remote review script sonar-project.properties
Build script
1. Pull substitution code
Stage ('pull substitution code') {checkout ([$class: 'GitSCM', branches: [[name: "* / ${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"])}
two。 Review code
Stage ('Code Review') {/ / the SonarQubeScanner tool that defines the current Jenkins def scannerHome = tool 'SonarQube' / / references the current JenkinsSonarQube environment withSonarQubeEnv (' SonarQube') {sh "" cd ${project_name} / / this is the defined option parameter ${scannerHome} / bin/sonar-scanner ""}}
3. Install the common module
Stage ('compile, install common subprojects') {sh "mvn-f tensquare_common clean install"}
View the server
4. Compile and package microservices
Stage ('compile, package microservice project') {sh "mvn-f ${project_name} clean package"}
View the server
Build process so far
5. Build a docker image
Add Dockerfile files to the project
# FROM java:8#FROM openjdk:11-jdk-alpineFROM openjdk:11ARG JAR_FILECOPY ${JAR_FILE} app.jarEXPOSE 9001ENTRYPOINT ["java", "- jar", "/ app.jar"]
Add a dockerfile plug-in to each microservice project Maven
Com.spotify dockerfile-maven-plugin 1.3.6 ${project.artifactId} target/$ {project.build.finalName} .jar
Modify Jenkinsfile file service package and add dockerfile:build "to trigger docker image construction"
Stage ('compile, package microservice project') {sh "mvn-f ${project_name} clean package dockerfile:build"}
And push it to the remote warehouse
Jenkins is rebuilt from scratch
View the server
Docker images
The image was built successfully
6. Image tagging
Public attribute
/ / the version number of the image def tag = "latest" / / the url address of the Harbor def harbor_url = "192.168.0.188 url 9123" / / the name of the image library project def harbor_project = "tensquare" stage ('compile Package microservice project upload image') {/ / compile and package-- build sh "mvn-f ${project_name} clean package dockerfile:build" / / define the image name def imageName = "${project_name}: ${tag}" / / A pair of images are labeled sh "docker tag ${imageName} ${harbor_url} / ${harbor_project} / ${imageName} "}
Push remote server and Jenkins build
View server image
7. Push the image to the harbor private repository
Here we want to withdraw cash on Harbor to create a warehouse.
Jenkins add harbor user credential
Enter the pipeline syntax to generate the syntax of harbor
Stage ('compile Package microservice project upload image') {/ / compile and package-- build sh "mvn-f ${project_name} clean package dockerfile:build" / / define the image name def imageName = "${project_name}: ${tag}" / / A pair of images are labeled sh "docker tag ${imageName} ${harbor_url} / ${harbor_project} / ${imageName} "/ / push the 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_project} / ${imageName}" sh "echo Image upload successfully"}
Push remote repository, Jenkins remote build, view images pushed to private warehouse
Remote push mirror successfully!
8. Pull the image and start the program
Because the image needs to be pulled remotely and the startup program needs to be started, the Publish Over SSH plug-in needs to be installed on Jenkins to send Shell commands remotely.
Configure plug-in
It should be noted here that two servers are required to establish communication and use ssh remote connection, that is, Jenkins uses ssh remote operation to pull the image from the server and start the application.
In this process, the host server of Jenkins is required to generate the public key and private key, and then copy it to the 188 server.
1. Change to the ssh directory
Cd / root/.ssh if prompted: bash: cd: .ssh: there is no such file or directory to execute ssh hostname (your own hostname)
two。 Execute the command to generate the public and private keys on the Jenkins host server
Ssh-keygen-t rsa
Then compare the following
Id_rsa is the private key id_rsa.pub is the public key
3. Copy the public key id_rsa.pub to the 188 server
Ssh-copy-id 192.168.0.188
4. Check the key copied by the 188 server
5. Test the ssh remote connection for Jenkins
The test succeeded
6. Use the pipelined syntax generator to generate ssh commands
Copy to Jenkinsfile file
/ / Application deployment sh "echo application deployment" sshPublisher (publishers: [sshPublisherDesc (configName: '188 applications, transfers: [sshTransfer (cleanRemote: false, excludes:', execCommand: "/ data/Jenkins_shell/deploy.sh $harbor_url $harbor_project $project_name $tag $port", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator:'[,] +', remoteDirectory:', remoteDirectorySDF: false, removePrefix:', sourceFiles:')] UsePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) / data/Jenkins_shell/deploy.sh this path points to a script file Used to manipulate the docker container This script provides the project name in the following $harbor_url image private warehouse address $harbor_project private warehouse $project_name project name $tag pull image version $port this parameter also needs to provide additional parameters in the Jenkins build is to provide parameters which are one-to-one corresponding to the parameters in the line surface file #! / bin/sh# receives the external parameter harbor_url=$1harbor_project=$2project_name=$3tag=$4port=$5imageName=$harbor_url/$harbor _ project/$project_name:$tagecho "$imageName" # query whether the container exists Delete containerId= `docker ps-a | grep-w ${project_name}: ${tag} | awk'{print $1} '`if ["$containerId"! = ""] Then # stop the container docker stop $containerId # Delete the container docker rm $containerId echo successfully delete the container fi# query whether the image exists, delete imageId= `docker images | grep-w $project_name | awk'{print $3} '`if ["$imageId"! = "] Then # Delete image docker rmi-f $imageId echo "delete image" fi# login Harbordocker login-u user-p 877425287User $harbor_url# download image docker pull $imageName# startup container docker run-di-p $port:$port $imageNameecho "container started successfully"
Add Jenkins input parameter
Submit the code and start building
Build successfully
Code Review SonarQube
Image repository Harbor
188 Image pulled by remote server
View container startup status
Access the test container
The above is all the contents of the article "how to use Jenkins packaging microservices to build Docker image operation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.