**1. 編寫`Jenkinsfile`文件**
一個微服務工程只需要一個`Jenkinsfile`文件。

```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"]
```
- 相關概念
- 軟件開發生命周期
- 軟件開發瀑布模型
- 軟件的敏捷開發
- 持續集成
- Jenkins介紹
- Jenkins是什么
- Jenkins的特征
- Jenkins環境搭建
- 搭建架構說明
- Gitlab安裝與配置
- Jenkins安裝與配置
- Tomcat安裝和配置
- Jenkins構建項目
- 自由風格軟件項目構建
- Maven項目構建
- Pipeline流水線項目構建
- Pipeline是什么
- Pipeline語法
- 流水線項目構建演示
- Pipeline Script from SCM
- 構建觸發器
- 觸發遠程構建
- 其他工程構建后觸發
- 定時構建
- 輪詢SCM
- Git hook自動觸發構建
- 參數化構建
- 配置郵箱發送構建結果
- SonarQube代碼審查平臺
- SonarQube是什么
- SonarQube平臺搭建
- 安裝jdk11
- 安裝數據庫PostgreSQL12
- 安裝SonarQube
- SonarQube實現代碼審查
- Jenkins+Docker+SpringCloud(1)
- 流程說明
- 環境搭建
- 服務器列表
- Docker安裝與配置
- Harbor安裝與配置
- Nginx安裝與配置
- 微服務持續集成演示
- Jenkins上配置
- 微服務項目配置
- 部署前端靜態web網站