Browse Source

add deploy

liweiquan 1 year ago
parent
commit
ac20678418
1 changed files with 175 additions and 0 deletions
  1. 175 0
      deploy/idcprod/Jenkinsfile

+ 175 - 0
deploy/idcprod/Jenkinsfile

@@ -0,0 +1,175 @@
+def label = "slave-${UUID.randomUUID().toString()}"
+
+
+podTemplate(cloud: 'kubernetes', namespace:'devops',label: label, serviceAccount: 'jenkins',containers: [
+  containerTemplate(name: 'docker', image: 'docker:latest', command: 'cat', ttyEnabled: true),
+  containerTemplate(name: 'kubectl', image: 'cnych/kubectl', command: 'cat', ttyEnabled: true)
+],volumes: [
+  hostPathVolume(mountPath: '/home/jenkins/.kube', hostPath: '/root/.kube'),
+  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
+  hostPathVolume(mountPath: '/etc/docker/daemon.json', hostPath: '/etc/docker/daemon.json')
+]
+) {
+
+    node(label) {
+        stage('构建 Docker 镜像') {
+          git url: ' http://10.138.130.21:8001/ai_repos/aihub-backend.git', branch: 'integration'
+          container('docker') {
+            echo "构建 Docker 镜像阶段"
+            retry(2) { sh "docker build --target image-idcprod -t registry.cn-hangzhou.aliyuncs.com/sxtest/datax-admin:idcprod --output type=docker ." }
+            echo "build success"
+          }
+        }
+        stage('Docker Push 镜像') {
+            container('docker') {
+              retry(2) { sh "docker push registry.cn-hangzhou.aliyuncs.com/sxtest/datax-admin:idcprod" }
+              echo "Push success"
+            }
+        }
+        stage('运行 Kubectl') {
+          container('kubectl') {
+            echo "重启 pod"
+            sh "kubectl rollout restart deployments/aihub-dag-backend  -n ns-aihub-dag"
+          }
+        }
+    }
+}
+
+
+//
+// pipeline {
+//     agent { label 'jnlp-slave'}
+//
+//     options {
+//         buildDiscarder(logRotator(numToKeepStr: '10'))
+//         disableConcurrentBuilds()
+//         timeout(time: 20, unit: 'MINUTES')
+//         gitLabConnection('gitlab')
+//     }
+//
+//     environment {
+//         IMAGE_REPO = "112.29.146.236:5000/demo/myblog"
+//         DINGTALK_CREDS = credentials('dingTalk')
+//         TAB_STR = "\n                    \n                    "
+//     }
+//
+//     stages {
+//         stage('git-log') {
+//             steps {
+//                 script{
+//                     sh "git log --oneline -n 1 > gitlog.file"
+//                     env.GIT_LOG = readFile("gitlog.file").trim()
+//                 }
+//                 sh 'printenv'
+//             }
+//         }
+//         stage('checkout') {
+//             steps {
+//                 container('tools') {
+//                     checkout scm
+//                 }
+//                 updateGitlabCommitStatus(name: env.STAGE_NAME, state: 'success')
+//                 script{
+//                     env.BUILD_TASKS = env.STAGE_NAME + "√..." + env.TAB_STR
+//                 }
+//             }
+//         }
+//         stage('CI'){
+//             failFast true
+//             parallel {
+//                 stage('Unit Test') {
+//                     steps {
+//                         echo "Unit Test Stage Skip..."
+//                     }
+//                 }
+//                 stage('Code Scan') {
+//                     steps {
+//                         container('tools') {
+//                             withSonarQubeEnv('sonarqube') {
+//                                 sh 'sonar-scanner -X'
+//                                 sleep 3
+//                             }
+//                             script {
+//                                 timeout(1) {
+//                                     def qg = waitForQualityGate('sonarqube')
+//                                     if (qg.status != 'OK') {
+//                                         error "未通过Sonarqube的代码质量阈检查,请及时修改!failure: ${qg.status}"
+//                                     }
+//                                 }
+//                             }
+//                         }
+//                     }
+//                 }
+//             }
+//         }
+//         stage('build-image') {
+//             steps {
+//                 container('tools') {
+//                     retry(2) { sh 'docker build . -t ${IMAGE_REPO}:${GIT_COMMIT}'}
+//                 }
+//                 updateGitlabCommitStatus(name: env.STAGE_NAME, state: 'success')
+//                 script{
+//                     env.BUILD_TASKS += env.STAGE_NAME + "√..." + env.TAB_STR
+//                 }
+//             }
+//         }
+//         stage('push-image') {
+//             steps {
+//                 container('tools') {
+//                     retry(2) { sh 'docker push ${IMAGE_REPO}:${GIT_COMMIT}'}
+//                 }
+//                 updateGitlabCommitStatus(name: env.STAGE_NAME, state: 'success')
+//                 script{
+//                     env.BUILD_TASKS += env.STAGE_NAME + "√..." + env.TAB_STR
+//                 }
+//             }
+//         }
+//         stage('deploy') {
+//             steps {
+//                 container('tools') {
+//                     sh "sed -i 's#{{IMAGE_URL}}#${IMAGE_REPO}:${GIT_COMMIT}#g' deploy/*"
+//                     timeout(time: 1, unit: 'MINUTES') {
+//                         sh "kubectl apply -f deploy/"
+//                     }
+//                 }
+//                 updateGitlabCommitStatus(name: env.STAGE_NAME, state: 'success')
+//                 script{
+//                     env.BUILD_TASKS += env.STAGE_NAME + "√..." + env.TAB_STR
+//                 }
+//             }
+//         }
+//     }
+//     post {
+//         success {
+//             echo 'Congratulations!'
+//             sh """
+//                 curl 'https://oapi.dingtalk.com/robot/send?access_token=${DINGTALK_CREDS_PSW}' \
+//                     -H 'Content-Type: application/json' \
+//                     -d '{
+//                         "msgtype": "markdown",
+//                         "markdown": {
+//                             "title":"myblog",
+//                             "text": "😄👍 构建成功 👍😄  \n**项目名称**:jairmir  \n**Git log**: ${GIT_LOG}   \n**构建分支**: ${BRANCH_NAME}   \n**构建地址**:${RUN_DISPLAY_URL}  \n**构建任务**:${BUILD_TASKS}"
+//                         }
+//                     }'
+//             """
+//         }
+//         failure {
+//             echo 'Oh no!'
+//             sh """
+//                 curl 'https://oapi.dingtalk.com/robot/send?access_token=${DINGTALK_CREDS_PSW}' \
+//                     -H 'Content-Type: application/json' \
+//                     -d '{
+//                         "msgtype": "markdown",
+//                         "markdown": {
+//                             "title":"myblog",
+//                             "text": "😖❌ 构建失败 ❌😖  \n**项目名称**:jairmir  \n**Git log**: ${GIT_LOG}   \n**构建分支**: ${BRANCH_NAME}  \n**构建地址**:${RUN_DISPLAY_URL}  \n**构建任务**:${BUILD_TASKS}"
+//                         }
+//                     }'
+//             """
+//         }
+//         always {
+//             echo 'I will always say Hello again!'
+//         }
+//     }
+// }