Harbor的安裝也比較簡單,可以查看之前的博客。
http://192.168.1.120:8001 admin/Harbor12345

注意:要在docker的配置文件中加入信任
[root@play bin]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://lara9y80.mirror.aliyuncs.com"],
"insecure-registries":[
"192.168.1.120:8001"
]
}
[root@play bin]#
微服務持續集成(1)-項目代碼上傳到Gitlab
在IDEA操作即可,參考之前的步驟。包括后台微服務和前端web網站代碼

微服務持續集成(2)-從Gitlab拉取項目源碼
1)創建Jenkinsfile文件
//git憑證ID
def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e"
//git的url地址
def git_url = "http://192.168.1.120:88/dalianpai_group/tensquare_back.git"
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 )每個項目的根目錄下添加sonar-project.properties
# must be unique in a given SonarQube instance sonar.projectKey=tensquare_zuul # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=tensquare_zuul 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
注意:修改 sonar.projectKey和sonar.projectName
3)修改Jenkinsfile構建腳本
//git憑證ID
def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e"
//git的url地址
def git_url = "http://192.168.1.120:88/dalianpai_group/tensquare_back.git"
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)在每個微服務項目的pom.xml加入dockerfile-maven-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>
2)在每個微服務項目根目錄下建立Dockerfile文件
#FROM java:8
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 10086
ENTRYPOINT ["java","-jar","/app.jar"]
注意:每個項目公開的端口不一樣
3)修改Jenkinsfile構建腳本
//git憑證ID
def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e"
//git的url地址
def git_url = "http://192.168.1.120:88/dalianpai_group/tensquare_back.git"
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"
}
}

//git憑證ID
def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e"
//git的url地址
def git_url = "http://192.168.1.120:88/dalianpai_group/tensquare_back.git"
//鏡像的版本號
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 鏡像上傳成功"
}
}
}
微服務持續集成(5)-上傳到Harbor鏡像倉庫

安裝 Publish Over SSH 插件
安裝以下插件,可以實現遠程發送Shell命令


//git憑證ID
def git_auth = "f14f1eec-8ba5-44af-a5e1-9714364b256e"
//git的url地址
def git_url = "http://192.168.1.120:88/dalianpai_group/tensquare_back.git"
//鏡像的版本號
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/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)])
}
}
執行的腳本
#! /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 "成功刪除鏡像"
fi
# 登錄Harbor
docker login -u eric -p Eric123456 $harbor_url
# 下載鏡像
docker pull $imageName
# 啟動容器
docker run -di -p $port:$port $imageName
echo "容器啟動成功"
先測試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_font.git']]])
}
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)])
}
}


