pipeline {
agent {
kubernetes {
cloud 'kubernetes'
yaml '''
apiVersion: v1
Kind: Pod
spec:
imagePullSecrets:
- name: harbor-admin
containers:
- name: kubectl
image: harbor.oldxu.net/ops/kubectl:1.23.15
imagePullPolicy: IfNotPresent
command: ["cat"]
tty: true
'''
}
}
//全局变量
environment{
Harbor_Image_Tag = "${Harbor_Url}/${Harbor_Pro}/${Image_Name}:${Image_Tags}"
NameSpace = "prod"
}
stages{
stage('检查地址'){
steps{
sh 'echo "${Harbor_Image_Tag}"'
}
}
stage('交付应用'){
steps{
container('kubectl'){
withCredentials([file(credentialsId: 'KuberConfig', variable: 'KUBECONFIG')]) {
//必备
sh 'mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config'
sh 'kubectl set image deployment/${Image_Name} ${Image_Name}=${Harbor_Image_Tag} -n ${NameSpace}'
sh 'echo "手动回退到上一个版本命令:kubectl rollout undo deployment ${Image_Name} -n prod"'
}
}
}
}
stage('快速回退'){
steps{
container('kubectl'){
withCredentials([file(credentialsId: 'KuberConfig', variable: 'KUBECONFIG')]) {
script{
timeout(time:1 , unit: 'HOURS'){
def UserInput = input message: '是否回退到上一个版本', parameters: [choice(choices: ['NO', 'YES'], description: '''NO:不进行回退 YES:回退到上一个版本''', name: 'rollback')]
if(UserInput == "YES"){
sh 'mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config && echo "正在执行回退操作..."'
sh 'kubectl rollout undo deployment ${Image_Name} -n prod'
}else{
echo "选择为:NO . 不进行回退操作 . 已完成CD流程上线发布"
}
}
}
}
}
}
}
}
}
标签:kubectl,Pipeline,SpringBoot,Harbor,Image,CD,sh,回退,Name
From: https://www.cnblogs.com/chuyiwang/p/17588769.html