声明式pipeline在k8s中完成自动部署,回滚,钉钉通知

12/10/2021 声明式pipeline在k8s中完成自动部署,回滚,钉钉通知

# jenkins gitlab nexus3/harbor 此类清单已有,安装部署不再叙述

Github: https://github.com/alexclownfish/jenkins_pipline 资源清单均在以上github地址

  • 版本发布失败/成功推送钉钉
  • 版本回滚成功/失败推送钉钉
  • 构建stage过程成功/失败推送钉钉

# jenkins 所需插件

网络延迟大下载慢,可以到下边地址下载,再load到jenkins https://updates.jenkins-ci.org/download/plugins/

Docker plugin
Kubernetes plugin
Kubernetes CLI Plugin
DingTalk
GitLab Plugin
Localization: Chinese (Simplified)

# kubernetes clouds 配置

# 添加kubernetes clouds

在这里插入图片描述

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

# Pod Templates

在这里插入图片描述

# jenkins-slave

在这里插入图片描述

# maven-3.6

在这里插入图片描述 在这里插入图片描述

# docker-and-maven 继承maven-3.6,挂载docker.sock以便后续docker.build

在这里插入图片描述 在这里插入图片描述

# kubectl (用于kubectl apply 滚动更新)

在这里插入图片描述

# docker-and-maven-and-kubectl 继承docker-and-maven

在这里插入图片描述

# pipeline to dingding

在这里插入图片描述

# 凭据

在这里插入图片描述

# Job 配置

# 参数化构建 (版本回滚)

在这里插入图片描述

# pipeline as vscode

# 将pipeline 以Jenkinsfile形式存放到代码仓库

在这里插入图片描述

# 配置Pipeline script from SCM (pipeline从代码仓库检出)

在这里插入图片描述

# PIPELINE

pipeline {
    environment {
        appName = "spring-boot-helloword"
        appVersion = "v1.0.3"
        registryCredential = "nexus3-admin"
        registry = "http://sonatype-nexus.cicd.svc.cluster.local:8082"
        STATUS_URL = "http://xxx:31629/job/spring-boot-helloword/${BUILD_NUMBER}/"
        CONSOLE_URL = "http://xxx:31629/job/spring-boot-helloword/${BUILD_NUMBER}/console"
    }
    agent {
        kubernetes {
            inheritFrom 'docker-and-maven-and-kubectl'
        }
    }
    
    stages {
        stage ("PULL CODES FROM CANGKU") {
            when {
                environment name: 'action',value: 'deploy'
            }
            steps {
                git branch: 'master', url: 'http://gitlab.cicd.svc.cluster.local:31080/root/spring-boot-demo.git'
            }
            post {
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}拉取代码失败',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
        stage ("BUILD JAR") {
            when {
                environment name: 'action',value: 'deploy'
            }
            steps {
                container ('maven') {
                    sh 'mvn clean test package'
                }
            }
            post {
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}代码编译失败',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
        stage ("BUILDING APP IMAGE") {
            when {
                environment name: 'action',value: 'deploy'
            }
            steps {
                container ('docker') {
                    script {
                        dockerImage = docker.build appName + ":" + appVersion + "-" + BUILD_NUMBER
                    }
                }
            }
            post {
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}镜像打包失败',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
        stage ("Push App Image") {
            when {
                environment name: 'action',value: 'deploy'
            }
            steps {
                container('docker') {
                    script {
                        docker.withRegistry(registry,registryCredential) {
                            dockerImage.push()
                        }
                    }
                }
            }
            post {
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}镜像推送失败',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
        stage('Pull Source Config File') {
            steps {
                git branch: 'master',url:'http://gitlab.cicd.svc.cluster.local:31080/root/spring-boot-deploy.git'
            }
            post {
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}配置清单拉取失败',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
        stage('Apply App Yaml') {
            when {
                environment name: 'action',value: 'deploy'
            }
            steps {
                container('kubectl') {
                    withKubeConfig([credentialsId: 'k8s-cluster-admin-kubeconfig-file']) {
                        sh '''
                        sed -ri "s@image: .*@image: sonatype-nexus.cicd.svc.cluster.local:8082/${appName}:${appVersion}-${BUILD_NUMBER}@g"  deploy/03-deployment.yaml
                        kubectl apply -f deploy/ --record
                        '''
                    }
                }
            }
            post {
                success {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}滚动更新成功',
                            '版本:${BUILD_NUMBER}',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}滚动更新失败',
                            '版本:${BUILD_NUMBER}',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
        stage('Rollback') {
            when {
                environment name: 'action',value: 'rollback'
            }
            steps {
                container('kubectl') {
                    withKubeConfig([credentialsId: 'k8s-cluster-admin-kubeconfig-file']) {
                        sh '''
                        sed -ri "s@image: .*@image: sonatype-nexus.cicd.svc.cluster.local:8082/${appName}:${appVersion}-${BUILD_NUMBER}@g"  deploy/03-deployment.yaml
                        kubectl apply -f deploy/ --record
                        '''
                    }
                }
            }
            post {
                success {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}回滚成功',
                            '回滚版本:${BUILD_NUMBER}',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
                failure {
                    dingtalk (
                        robot: '1174f280-4f5a-43e7-aa2f-a062c3808993',
                        type: 'MARKDOWN',
                        title: 'spring-boot项目',
                        text: [
                            '### spring-boot项目',
                            '状态:${appName}回滚失败',
                            '回滚版本:${BUILD_NUMBER}',
                            '[查看部署详情](${STATUS_URL})',
                            '[查看日志Console](${CONSOLE_URL})'
                        ],
                        at: [
                            '15737927244'
                        ]
                    )
                }
            }
        }
    }
}