2023年7月7日发(作者:)
Jenkins部署微服务项⽬ Harbor的安装也⽐较简单,可以查看之前的博客。 admin/Harbor12345
注意:要在docker的配置⽂件中加⼊信任[root@play bin]# cat /etc/docker/{"registry-mirrors": [""],"insecure-registries":[ "192.168.1.120:8001" ]}[root@play bin]#微服务持续集成(1)-项⽬代码上传到Gitlab在IDEA操作即可,参考之前的步骤。包括后台微服务和前端web⽹站代码微服务持续集成(2)-从Gitlab拉取项⽬源码1)创建Jenkinsfile⽂件//git凭证IDdef git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e"//git的url地址def git_url = "192.168.1.120:88/dalianpai_group/tensquare_"node { stage('拉取代码') { checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]]) }}微服务持续集成(3)-提交到SonarQube代码审查1)创建项⽬,并设置参数创建tensquare_back项⽬,添加两个参数 2 )每个项⽬的根⽬录下添加ties# must be unique in a given SonarQube tKey=tensquare_zuul# this is the name and version displayed in the SonarQube UI. Was mandatoryprior to SonarQube tName=tensquare_tVersion=1.0# Path is relative to the ties file. Replace "" by "/" onWindows.# This property is optional if s is s=.ions=**/test/**,**/target/**es=.==ies=**/target/classes/**# Encoding of the source code. Default is default system Encoding=UTF-8注意:修改 tKey和tName3)修改Jenkinsfile构建脚本//git凭证ID def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e" //git的url地址 def git_url = "192.168.1.120:88/dalianpai_group/tensquare_" 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('sonarQube6.7.4') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } }}微服务持续集成(4)-使⽤Dockerfile编译、⽣成镜像利⽤dockerfile-maven-plugin插件构建Docker镜像1)在每个微服务项⽬的加⼊dockerfile-maven-plugin插件
安装 Publish Over SSH 插件安装以下插件,可以实现远程发送Shell命令
//git凭证ID def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e" //git的url地址 def git_url = "192.168.1.120:88/dalianpai_group/tensquare_" //镜像的版本号 def tag = "latest"//Harbor的url地址 def harbor_url = "192.168.1.120:8001" //镜像库项⽬名称 def harbor_project = "tensquare" //Harbor的登录凭证ID def harbor_auth = "a45cb8c3-49f5-4394-976b-1d5d9825ca5f" 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('sonarQube6.7.4') { sh """ cd ${project_name} ${scannerHome}/bin/sonar-scanner """ } } stage('编译,安装公共⼦⼯程') { sh "mvn -f tensquare_common clean install" } stage('编译打包微服务,上传镜像') { sh "mvn -f ${project_name} clean package dockerfile:build" //定义镜像名称 def imageName = "${project_name}:${tag}" //对镜像打上标签 sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}" withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) { //登录到Harbor sh "docker login -u ${username} -p ${password} ${harbor_url}" //镜像上传 sh "docker push ${harbor_url}/${harbor_project}/${imageName}" sh "echo 镜像上传成功" } sshPublisher(publishers: [sshPublisherDesc(configName: '121', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/ $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)]) }}执⾏的脚本#! /bin/sh#接收外部参数harbor_url=$1harbor_project_name=$2project_name=$3tag=$4port=$5imageName=$harbor_url/$harbor_project_name/$project_name:$tagecho "$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 "成功删除镜像"fi# 登录Harbordocker login -u eric -p Eric123456 $harbor_url# 下载镜像docker pull $imageName# 启动容器docker run -di -p $port:$port $imageNameecho "容器启动成功"先测试eureka的
其他剩下的微服务组件 新建前端流⽔线项⽬node { stage('拉取代码'){ checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '979cf306-0c46-4ccd-ab6b-8f4f0c7e03da', url: 'git@192.168.1.120:dalianpai_group/tensquare_']]]) } stage('打包,部署⽹站'){ nodejs('121NodeJs') { sh ''' npm i npm run build
''' }
sshPublisher(publishers: [sshPublisherDesc(configName: '121', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/usr/share/nginx/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) }
}
发布者:admin,转转请注明出处:http://www.yc00.com/web/1688702437a163797.html
评论列表(0条)