jenkins下pipeline 通过用户选择需要回滚的服务,输入回滚的版本号,实现k8s上发布的版本回滚
env.CREDENTIALSID = '5576057b-0a5c-4e95-9b1f-xxxxxxxx'
podTemplate(name: 'jenkins-slave',label: 'jenkins-slave', cloud: 'kubernetes-test',namespace: 'jenkins', containers: [
containerTemplate(
name: 'jnlp',
image: '192.168.1.140/library/jnlp-slave-maven:latest',
ttyEnabled: true,
privileged: false,
alwaysPullImage: false,
)
],
volumes:[
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/etc/localtime', hostPath: '/etc/localtime'),
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'),
//nfsVolume(mountPath: '/usr/local/maven',serverAddress: '192.168.1.110',serverPath: '/var/NHData/maven', readOnly: false),
nfsVolume(mountPath: '/home/jenkins/Deployment',serverAddress: '192.168.1.110',serverPath: '/var/NHData/Deployment', readOnly: false),
//nfsVolume(mountPath: '/root/.m2',serverAddress: '192.168.1.110',serverPath: '/var/NHData/.m2', readOnly: false),
]
) {
node('jenkins-slave') {
def app
stage('准备版本回滚!') {
//println '准备版本回滚!'
script {
env.SERVER_NAME = input message: '选择需要回滚的服务', ok: '确定回滚此服务',
parameters: [choice(name: 'SERVER_NAME', choices: ['无','gateway', 'inst','system'], description: '选择要回滚的服务')]
switch("${env.SERVER_NAME}"){
case 'gateway':
println('回滚gateway')
env.SERVER_PORT = 1111
break;
case 'inst':
println('回滚inst')
env.SERVER_PORT = 2222
break;
case 'system':
println('回滚system')
env.SERVER_PORT = 3333
break;
default:
println('error!!!!!!!')
}
}
script {
env.SERVICE_VERSION = input message: '输入需要回滚的版本号', ok: '回滚到此版本',
parameters: [string(name: '版本号', defaultValue: '无', description: '选择要回滚的服务')]
}
echo "回滚的服务为:${SERVER_NAME},端口号为:${SERVER_PORT}, 版本号为:${SERVICE_VERSION}"
sh "sed -e 's#{IMAGE_TAG}#${SERVICE_VERSION}#g;s#{APP_NAME}#${SERVER_NAME}#g;s#{APP_PORT}#${SERVER_PORT}#g;s#{SPRING_PROFILE}#test#g' /home/jenkins/Deployment/k8s-deployment.tpl > k8s-deployment.yml "
sh 'cat k8s-deployment.yml'
kubernetesDeploy configs: 'k8s-deployment.yml',kubeconfigId: '13a255a8-5048-4e87-8045-xxxxxxxx',enableConfigSubstitution: true
}
}
}