Pipeline SpringBoot-deploy-CD
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流程上线发布"
}
}
}
}
}
}
}
}
}
级联变量
def get_tag = [ "bash", "-c", "curl -s -uadmin:syvwub-Gitgif-6rysra -H'Content-Type: application/json' -X GET https://${Harbor_Url}/v2/${Harbor_Pro}/${Image_Name}/tags/list | sed -re 's#(\\{.*\\[)(.*)(\\]\\})#\\2#g' -e 's#,#\\n#g' -e 's#\"##g' | sort -t '_' -k2,3 -r | head -5" ]
return get_tag.execute().text.tokenize("\n")