diff --git a/Jenkinsfile b/Jenkinsfile index 0f89908..37fc03c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -5,67 +5,66 @@ pipeline { HARBOR_HOST = "registry.cn-hangzhou.aliyuncs.com" NAMESPACE_NAME = "newrain_wang" REPOSITORY_NAME = "my-web-app" - TAG = "v3.0" + TAG = "v2.0" REPLICAS = 4 YAML_PATH = "k8s/" DEPLOYMENT_NAME = "deploy.yml" STATUS_URL = "http://qf.jenkins.vip:30210/job/test/${BUILD_NUMBER}" CONSOLE_URL = "http://qf.jenkins.vip:30210/job/test/${BUILD_NUMBER}/console" - status = "${currentBuild.result}" } agent { kubernetes { cloud "kubernetes" yaml """ -apiVersion: v1 -kind: Pod -spec: - containers: - - name: jnlp - image: jenkins/inbound-agent:3107.v665000b_51092-15 - args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)'] - imagePullPolicy: IfNotPresent - volumeMounts: - - mountPath: "/etc/localtime" - name: "volume-2" - readOnly: false - - name: kubectl - image: kubesphere/kubectl:v1.22.0 - imagePullPolicy: IfNotPresent - tty: true - command: ["cat"] - volumeMounts: - - mountPath: "/etc/localtime" - name: "volume-2" - readOnly: false - - mountPath: "/var/run/docker.sock" - name: "volume-docker" - readOnly: false - - mountPath: "/root/.kube/config" - subPath: config - name: "kubeconfig" - readOnly: false - - name: docker - image: docker:19.03.15-git - command: ['cat'] - tty: true - volumeMounts: - - mountPath: "/var/run/docker.sock" - name: "volume-docker" - readOnly: false - volumes: - - name: volume-2 - hostPath: - path: "/usr/share/zoneinfo/Asia/Shanghai" - - name: volume-docker - hostPath: - path: "/var/run/docker.sock" - - name: kubeconfig - secret: - secretName: kubeconfig - items: - - key: config - path: config + apiVersion: v1 + kind: Pod + spec: + containers: + - name: jnlp + image: jenkins/inbound-agent:3107.v665000b_51092-15 + args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)'] + imagePullPolicy: IfNotPresent + volumeMounts: + - mountPath: "/etc/localtime" + name: "volume-2" + readOnly: false + - name: kubectl + image: kubesphere/kubectl:v1.22.0 + imagePullPolicy: IfNotPresent + tty: true + command: ["cat"] + volumeMounts: + - mountPath: "/etc/localtime" + name: "volume-2" + readOnly: false + - mountPath: "/var/run/docker.sock" + name: "volume-docker" + readOnly: false + - mountPath: "/root/.kube/config" + subPath: config + name: "kubeconfig" + readOnly: false + - name: docker + image: docker:19.03.15-git + command: ['cat'] + tty: true + volumeMounts: + - mountPath: "/var/run/docker.sock" + name: "volume-docker" + readOnly: false + volumes: + - name: volume-2 + hostPath: + path: "/usr/share/zoneinfo/Asia/Shanghai" + - name: volume-docker + hostPath: + path: "/var/run/docker.sock" + - name: kubeconfig + secret: + secretName: kubeconfig + items: + - key: config + path: config """ } } @@ -83,52 +82,85 @@ spec: } } post { - always { + success { dingtalk ( robot: '4eabbd09-a21a-40a9-84db-6440c0daebec', type: 'MARKDOWN', - title: '镜像推送 ${PROJECT_NAME}', + title: '镜像构建成功 ${PROJECT_NAME}', text: [ '### 项目 ${PROJECT_NAME}', '---', - '- 状态:、$(currentBuild.result) \${currentBuild.result} ${env.status}', + '- 状态:成功', + '- 阶段:构建镜像', '- 版本:${BUILD_NUMBER}', '- [查看部署详情](${STATUS_URL})', '- [查看日志Console](${CONSOLE_URL})' ], - at: [ - '17694993496' - ] + at: ['17694993496'] + ) + } + failure { + dingtalk ( + robot: '4eabbd09-a21a-40a9-84db-6440c0daebec', + type: 'MARKDOWN', + title: '镜像构建失败 ${PROJECT_NAME}', + text: [ + '### 项目 ${PROJECT_NAME}', + '---', + '- 状态:失败', + '- 阶段:构建镜像', + '- 版本:${BUILD_NUMBER}', + '- [查看部署详情](${STATUS_URL})', + '- [查看日志Console](${CONSOLE_URL})' + ], + at: ['17694993496'] ) } } } + stage('部署到kubernetes') { steps { container('kubectl') { sh """ sed -i "s/REPLICAS/${env.REPLICAS}/;s/HARBOR_HOST/${env.HARBOR_HOST}/;s/NAMESPACE_NAME/${env.NAMESPACE_NAME}/;s/REPOSITORY_NAME/${env.REPOSITORY_NAME}/;s/TAG/${env.TAG}/" ${env.YAML_PATH}${env.DEPLOYMENT_NAME} - kubectl apply -f ${env.YAML_PATH} + kubectl apply -f ${env.YAML_PATH}${env.DEPLOYMENT_NAME} """ } } post { - always { + success { + dingtalk ( + robot: '4eabbd09-a21a-40a9-84db-6440c0daebec', + type: 'MARKDOWN', + title: '部署成功 ${PROJECT_NAME}', + text: [ + '### 项目 ${PROJECT_NAME}', + '---', + '- 状态:成功', + '- 阶段:部署到kubernetes', + '- 版本:${BUILD_NUMBER}', + '- [查看部署详情](${STATUS_URL})', + '- [查看日志Console](${CONSOLE_URL})' + ], + at: ['17694993496'] + ) + } + failure { dingtalk ( robot: '4eabbd09-a21a-40a9-84db-6440c0daebec', type: 'MARKDOWN', - title: '项目部署 ${PROJECT_NAME}', + title: '部署失败 ${PROJECT_NAME}', text: [ '### 项目 ${PROJECT_NAME}', '---', - '- 状态:${currentBuild.result}', + '- 状态:失败', + '- 阶段:部署到kubernetes', '- 版本:${BUILD_NUMBER}', '- [查看部署详情](${STATUS_URL})', '- [查看日志Console](${CONSOLE_URL})' ], - at: [ - '17694993496' - ] + at: ['17694993496'] ) } }