准备工作参考本人另外几篇Jenkins相关的文章
-
新建任务
-
添加参数配置
字符串参数:分支名称
多选框:项目名称(Extended Choice Parameter插件必备,插件安装参考我另外的文章)
这个分割规则自定义。只要根据Jenkinsfile脚本处理规则能够对上就可以,此处本人图片是老的,懒得重新替换新的
下面的项目配置才是最新的
travel-system@8201@travel-modules/,travel-auth@8200,travel-gateway@7070,travel-backend@8204@travel-modules/,travel-ticket@8205@travel-modules/
以@分割:
0位标识项目名称
1位标识端口
2位若存在则标识上级目录
如果项目存在依赖情况B依赖A,需要先部署A才能部署B,则可以通过调整此处项目顺序
此处如果项目名称没有转换为中文,检查Choose Source for Value Description中描述直接的分割符号是否与前面的相同,假如前面是英文逗号,后面是中文逗号此处也是会有问题
-
配置代码拉去
应用,报存 -
配置远程部署服务器
- 拷贝公钥到远程服务器
ssh-copy-id 远程服务器IP
- 系统配置->添加远程服务器
-
参数添加
多选框:部署服务器
-
Harbor的登录凭证ID获取
添加凭证
点击确定之后会生成ID,这个ID将作为Jenkinsfile脚本中的 harbor_auth 参数
Jenkinsfile脚本
//git凭证ID
def git_auth = "896fd2e4-623a-41a3-96ac-e664d92d3f94"
//git的url地址
def git_url = "git@127.0.0.1:xxx/xxxx.git"//镜像的版本号
def tag = "latest"
//Harbor的url地址
def harbor_url = "127.0.0.1"
//镜像库项目名称
def harbor_project = "travel"//Harbor的登录凭证ID
def harbor_auth = "31da7f5d-5f34-4267-989e-9edef7844b01"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@10086def projectInfo = selectedProjectNames[i];//当前遍历的项目名称def currentProjectName = "${projectInfo}".split("@")[0]//当前遍历的项目端口def currentProjectPort = "${projectInfo}".split("@")[1]//多级目录下的项目上级目录,格式:项目名@端口@上级项目名//非多级目录格式:项目名@端口def packPathArray = "${projectInfo}".split("@")if(packPathArray.size() == 3){def packPath = packPathArray[2]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}"//把镜像推送到HarborwithCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {//登录到Harborsh "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)])echo "${currentProjectName}完成编译,构建镜像"}}}
}
deployCluster.sh 脚本:
# ! /bin/sh
#接收外部参数
harbor_url=$1
harbor_project_name=$2
project_name=$3
tag=$4
port=$5
profile=$6imageName=$harbor_url/$harbor_project_name/$project_name:$tagecho "$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 $containerIdecho "成功删除容器"
fi#查询镜像是否存在,存在则删除
imageId=`docker images | grep -w $project_name | awk '{print $3}'`
echo "镜像ID=${imageId}"if [ "$imageId" != "" ] ; then#删除镜像docker rmi -f $imageIdecho "成功删除镜像"
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 $imageNameecho "容器启动成功"
问题处理:
问题原因是文本格式问题,windows和Linux文本格式不一致
解放方案:
- 使用vim编辑器,可以执行以下命令来进行转换:
:set ff=unix
:wq
- 批量处理
sed -i 's/[[:cntrl:]]//g' xxx.txt