多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**1. 编写`Jenkinsfile`文件** 一个微服务工程只需要一个`Jenkinsfile`文件。 ![](https://img.kancloud.cn/76/ee/76ee2aa3488a1e153b50ac79638453bd_1281x386.jpg) ```groovy //gitlab凭证ID def git_auth = "e4e02eb6-f6bb-4040-b842-c1423c397493" //gitlab的url地址 def git_url = "git@gitlab.master.com:itheima_group/tensquare-parent.git" //镜像的版本号 def tag = "latest" //Harbor的url地址 def harbor_url = "reg.myharbor.com:85" //harbor镜像库项目名称 def harbor_project_name = "tensquare" //Harbor的登录凭证ID def harbor_auth = "0d458918-e2ea-4b51-8250-91c3731be288" node { stage('拉取代码') { checkout([$class : 'GitSCM', branches: [[name: '*/${branch}']] , doGenerateSubmoduleConfigurations: false , extensions : [] , submoduleCfg : [] , userRemoteConfigs : [ [credentialsId: "${git_auth}", url: "${git_url}"] ]]) } stage('代码审查') { //定义当前Jenkins的SonarQubeScanner工具 def scannerHome = tool 'sonarqube-scanner' //引用当前JenkinsSonarQube环境 withSonarQubeEnv('sonarqube-8.9.6.50800') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } stage('编译/构建镜像') { //定义镜像名称 def imageName = "${project_name}:${tag}" //编译,安装公共工程 sh "mvn -f tensquare-common clean install" //编译,构建本地镜像 sh "mvn -f ${project_name} clean package dockerfile:build" //给镜像打标签 sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}" //登录harbor并上传镜像 withCredentials([ usernamePassword(credentialsId: "${harbor_auth}" , passwordVariable: 'password' , usernameVariable: 'username')]) { //登录Harbor sh "docker login -u ${username} -p ${password} ${harbor_url}" //推送镜像到Harbor sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}" } //上传完成后删除本地镜像 sh "docker rmi -f ${imageName}" sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}" } stage('部署服务') { sshPublisher( publishers: [ sshPublisherDesc( configName: 'production-server' , transfers: [ sshTransfer( cleanRemote: false , excludes: '' , execCommand: "/opt/jenkins_shell/tensquare/deploy.sh $harbor_url $harbor_project_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) ]) } } ``` **2. 在部署服务器 production-server 机器上编写部署脚本** (1)`/opt/jenkins_shell/tensquare/deploy.sh`。 ```shell #!/bin/sh #接收外部参数 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" #查询容器是否存在,存在则删除 containerId=`docker ps -a | grep -w ${project_name}:${tag} | awk '{print $1}'` if [ "$containerId" != "" ] ; then #停掉容器 docker stop $containerId #删除容器 docker rm $containerId echo "成功删除容器" fi #查询镜像是否存在,存在则删除 imageId=`docker images | grep -w $project_name | awk '{print $3}'` if [ "$imageId" != "" ] ; then #删除镜像 docker rmi -f $imageId echo "成功删除镜像"JAR_FILE fi # 登录Harbor私服 docker login -u harborZhangsan -p harborZhang3 $harbor_url # 下载镜像 docker pull $imageName # 启动容器 docker run -di -p $port:$port $imageName echo "容器启动成功" ``` (2)给该脚本执行权限。 ```shell chmod +x deploy.sh ``` **3.为每个微服务项目编写各自的`sonar-project.properties`文件** 下面是微服务项目 tensquare-eureka-server 的一个例子,记得到Harbor上创建对应的项目哦。 ```properties # must be unique in a given SonarQube instance sonar.projectKey=tensquare-eureka-server # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=tensquare-eureka-server sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # This property is optional if sonar.modules is set. sonar.sources=. sonar.exclusions=**/test/**,**/target/** sonar.java.binaries=. sonar.java.source=1.8 sonar.java.target=1.8 #sonar.java.libraries=**/target/classes/** # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8 ``` **4.为每个微服务项目编写各自的`Dockerfile`文件** (1)每个微服务项目的`pom.xml`添加如下插件。 ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <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> </plugins> </build> ``` (2)tensquare-eureka-server 微服务的`Dockerfile`文件示例。 ```properties #FROM java:8 FROM openjdk:8-jdk-alpine ARG JAR_FILE COPY ${JAR_FILE} app.jar EXPOSE 10086 --注意每个微服务项目的端口都不一样 ENTRYPOINT ["java","-jar","/app.jar"] ```