共享库搭建使用请参考
搭建jenkins共享库使用Jenkins Shared Library_coke-520的博客-CSDN博客_jenkins共享库
流水线脚本发布方式采用的ssh远程命令方式
pipeline模板脚本如下:
#!/usr/bin/env groovy
def call(String type, Map config) {
if (type == "sit") {
pipeline {
agent any
options {
gitLabConnection('Gitlab')
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
}
environment {
branch = "${config.branch}"
git_url = "${config.git_url}"
pom_path = "${config.pom_path}"
remote_ip = "${config.remote_ip}"
remote_user = "${config.remote_user}"
//remote_passwd = 'doHfsrD#'
app_home = "${config.app_home}" //部署发布目录
app_name = "${config.app_name}" //jar包名称
start_cmd = "${config.start_cmd}"
cfg_paths = "${config.cfg_paths}" //配置文件拷贝:原路径文件,目标路径。多个配置换行
}
parameters {
booleanParam(name: 'deployENV', defaultValue: false, description: '勾选按钮重启服务,不需要部署jar包')
booleanParam(name: 'replaceCFG', defaultValue: false, description: '勾选此按钮将进行配置中心文件替换')
//部署文件的相对路径
string(name: 'deployLocation', defaultValue: "${config.target_path}", description: 'jar包的相对路径 ')
}
stages {
stage('拉取代码') {
when {
expression {
params.deployENV == false
}
}
steps {
checkout([$class: 'GitSCM', branches: [[name: "${branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '824deb03-85b2-4c33-ac0f-dff6238f74d8', url: "${git_url}"]]])
}
}
stage('替换配置文件') {
when {
expression {
params.replaceCFG == true
}
}
steps {
echo '拷贝deploy-project-config配置文件'
script {
for (i in cfg_paths.split()) {
def cfg_split = i.split(',')
src_path = cfg_split[0]
des_path = cfg_split[1]
sh "cp -af ${src_path} ${des_path}"
}
}
}
}
stage('Build') {
when {
expression {
params.deployENV == false
}
}
steps {
sh "mvn clean package -Dmaven.test.skip=true -f ${pom_path}/pom.xml"
}
}
stage('归档') {
when {
expression {
params.deployENV == false
}
}
steps {
// 归档文件,使用 archiveArtifacts 命令存档文件。存档的文件会保存到 Jenkins 的 jobs/JOB_NAME/builds/BUILD_NO 目录下
//archiveArtifacts artifacts: 'target/${repoName}.jar', fingerprint: true
archiveArtifacts params.deployLocation
}
}
stage('deploy') {
steps {
script {
for (ServerIP in remote_ip.split(",")) {
//false部署,true为重启服务
if (params.deployENV == false) {
//创建工作目录
sh """
ssh '${ServerIP}' -l '${remote_user}' -i /root/.ssh/id_new \"[[ ! -d ${app_home} ]] && mkdir -p ${app_home} || echo "${app_home}已存在" \"
"""
//清理旧jar包
sh "ssh '${ServerIP}' -l '${remote_user}' -i /root/.ssh/id_new 'rm -rf ${app_home}/${app_name}.jar'"
//拷贝jar包
sh "scp -i /root/.ssh/id_new ${params.deployLocation} ${remote_user}@${ServerIP}:${app_home}"
//停服务
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh """
ssh '${ServerIP}' -l '${remote_user}' -i /root/.ssh/id_new \"ps -ef | grep java | grep ${app_name}.jar |grep -v grep|awk '{print \\\$2}'|xargs kill -9\"
"""
}
//启服务
sh """
ssh '${ServerIP}' -l '${remote_user}' -i /root/.ssh/id_new \"source ~/.bash_profile;source /etc/profile;cd ${app_home};${start_cmd}\"
"""
} else {
//停服务
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh """
ssh '${ServerIP}' -l '${remote_user}' -i /root/.ssh/id_new \"ps -ef | grep java | grep ${app_name}.jar |grep -v grep|awk '{print \\\$2}'|xargs kill -9\"
"""
}
//启服务
sh """
ssh '${ServerIP}' -l '${remote_user}' -i /root/.ssh/id_new \"source ~/.bash_profile;source /etc/profile;cd ${app_home};${start_cmd}\"
"""
}
}
}
}
}
}
}
}
}
Jenkinsfile文件如下:
#!/usr/bin/env groovy
@Library('jenkins-libraries') _
def Git_url = '代码git仓库地址' //git地址
def Branch = 'git分支' //git分支
def Pom_path = 'pom文件路径' //pom文件路径
def Remote_ip = '发布服务器ip' //发布服务器ip,多个逗号分隔开(英文逗号)
def Remote_user = '发布服务器用户' //发布服务器用户
def App_home = '发布目录' //发布目录 (最尾部不要带'/')
def App_name = '发布jar包名' //发布jar包名称
def Target_path = 'jar编译好文件路径' //jar包目录
def Cfg_paths = '''
''' //配置文件拷贝。(原路径文件,目标路径。多个配置路径需换行)
//jar包启动命令
def Start_cmd = "nohup java -jar ${App_name}.jar >> /app/tmp_test/${App_name}.log 2>&1 & </dev/null"
def config = [:]
config.put('git_url', "${Git_url}")
config.put('branch', "${Branch}")
config.put('pom_path', "${Pom_path}")
config.put('remote_ip', "${Remote_ip}")
config.put('remote_user', "${Remote_user}")
config.put('app_home', "${App_home}")
config.put('app_name', "${App_name}")
config.put('start_cmd', "${Start_cmd}")
config.put('cfg_paths', "${Cfg_paths}")
config.put('target_path', "${Target_path}")
test_mvn_jar("sit", config) //这里调用的是pipeline共享库的模板
版权声明:本文为weixin_47415962原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。