# 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'
]
)
}
}
}
}
}