准备工作参考本人另外几篇Jenkins相关的文章
travel-system:8201@travel-modules/,travel-auth:8200,travel-gateway:7070,travel-backend:8204@travel-modules/,travel-ticket:8205@travel-modules/
如果项目存在依赖情况B依赖A,需要先部署A才能部署B,则可以通过调整此处项目顺序
此处如果项目名称没有转换为中文,检查Choose Source for Value Description中描述直接的分割符号是否与前面的相同,假如前面是英文逗号,后面是中文逗号此处也是会有问题
ssh-copy-id 远程服务器IP
Jenkinsfile脚本
//git凭证ID
def git_auth = "896fd2e4-623a-41a3-96ac-e664d92d3f94"
//gitlab项目的url地址
def git_url = "[email protected]:chenp/dx_cl.git"
//镜像的版本号
def tag = "latest"
//Harbor的url地址
def harbor_url = "127.0.0.1"
//镜像库项目名称
def harbor_project = "travel"
//Harbor的登录凭证ID
def harbor_auth = "fb57f314-1c2b-4446-8479-3fac780b2f57"
node {
//获取当前选择的项目名称
def selectedProjectNames = "${project_name}".split(",")
//获取当前选择的服务器名称
def selectedServers = "${publish_server}".split(",")
stage("拉取代码") {
checkout([$class: 'GitSCM', branches: [[name: "*/${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_url}"]]])
}
stage('编译,安装公共子工程') {
sh "mvn -f travel-common clean install"
sh "mvn -f travel-api clean install"
}
stage('编译,打包微服务工程') {
for(int i=0;i<selectedProjectNames.length;i++){
//tensquare_eureka_server@10086
def projectInfo = selectedProjectNames[i];
//当前遍历的项目名称
def currentProjectName = "${projectInfo}".split(":")[0]
//当前遍历的项目端口
def currentProjectPort = "${projectInfo}".split(":")[1]
//多级目录下的项目上级目录,比如travel-system 的打包路径travel-modules
def packPathArray = "${projectInfo}".split("@")
if(packPathArray.size() == 2){
def packPath = packPathArray[1]
sh "mvn -pl ${packPath}${currentProjectName} clean package dockerfile:build"
}else{
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
}
//定义镜像名称
def imageName = "${currentProjectName}:${tag}"
//对镜像打上标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}"
//把镜像推送到Harbor
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 镜像上传成功"
}
//删除本地镜像
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project}/${imageName}"
//遍历所有服务器,分别部署
for(int j=0;j<selectedServers.length;j++){
//获取当前遍历的服务器名称
def currentServerName = selectedServers[j]
//加上的参数格式:--spring.profiles.active=
def activeProfile = "--spring.profiles.active=dev"
//根据不同的服务名称来读取不同的配置信息
//if(currentServerName=="master_server"){
// activeProfile = activeProfile+"eureka-server1"
// }else if(currentServerName=="slave_server"){
// activeProfile = activeProfile+"eureka-server2"
// }
//部署应用
sshPublisher(publishers: [sshPublisherDesc(configName: "${currentServerName}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "/opt/jenkins_shell/deployCluster.sh $harbor_url $harbor_project $currentProjectName $tag $currentProjectPort $activeProfile", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
deployCluster.sh 脚本:
# ! /bin/sh
#接收外部参数
harbor_url=$1
harbor_project_name=$2
project_name=$3
tag=$4
port=$5
profile=$6
imageName=$harbor_url/$harbor_project_name/$project_name:$tag
echo "$imageName"
#查询容器是否存在,存在则删除
#containerId=`docker ps -a | grep -w ${project_name}:${tag} | awk '{print $1}'`
containerId=`docker ps -aqf "name=${project_name}"`
echo "容器ID=${containerId}"
if [ "$containerId" != "" ] ; then
#停掉容器
docker stop $containerId
#删除容器
docker rm $containerId
echo "成功删除容器"
fi
#查询镜像是否存在,存在则删除
imageId=`docker images | grep -w $project_name | awk '{print $3}'`
echo "镜像ID=${imageId}"
if [ "$imageId" != "" ] ; then
#删除镜像
docker rmi -f $imageId
echo "成功删除镜像"
fi
# 登录Harbor
docker login -u admin -p Dx@123456 $harbor_url
echo "Harbor登录成功"
# 下载镜像
docker pull $imageName
# 启动容器
#docker run -di -p $port:$port $imageName $profile
docker run -di --network host --name $project_name -v /var/log/travelLogs/$project_name:/logs/$project_name $imageName
echo "容器启动成功"