pipeline {
  agent {
    kubernetes {
      yamlFile 'deploy/KubernetesPod.yaml'
      retries 2
    }
  }

  environment {
    IMAGE_REPO = "SXKJ:32775"
    IMAGE_REPO_YILI = "registry.cn-hangzhou.aliyuncs.com/sxtest"
    TAB = "\n   \n  "
    GIT_LOG = sh (script: "git log --oneline -n 1", returnStdout: true).trim()
    BUILD_USER_NAME = sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim()
    TITLE = "${JOB_BASE_NAME}-${ENV_APP}"
    GIT_BRANCH_NAME = env.GIT_BRANCH.substring(env.GIT_BRANCH.lastIndexOf('/') + 1)
    TIMESTAMP = sh(returnStdout: true, script: "TZ=Asia/Shanghai date  +'%F_%H-%M-%S'").trim()
    TAG = "${ENV_APP}-${GIT_BRANCH_NAME}-${TIMESTAMP}"
  }
  stages {
    stage('git-log') {
      steps {
        script {
            sh 'echo "git log"'
            env.BUILD_TASKS = "\n   \n  "
        }
        sh 'printenv'
       }
    }
    stage('构建镜像') {
      steps {
        container('docker') {
          echo "构建 Docker 镜像阶段"
          sh '''
          if [ "$ENV_APP" == "yili-dxy" ]; then
            docker build  -t ${IMAGE_REPO_YILI}/${JOB_BASE_NAME}:${TAG} --output type=docker .
          elif [ "$ENV_APP" == "test" ]; then
            docker build  -t ${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG} --output type=docker .
          else
            xxx
          fi
          '''
          echo "build success"
        }
        script {
          env.BUILD_TASKS += env.STAGE_NAME + "✅" + env.TAB
        }
      }
    }
    stage('镜像推送') {
      steps {
        container('docker') {
          echo "Push Docker 镜像阶段"
          sh '''
          if [ "$ENV_APP" == "test" ]; then
            docker push ${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG}
          elif [ "$ENV_APP" == "yili-dxy" ]; then
            docker push ${IMAGE_REPO_YILI}/${JOB_BASE_NAME}:${TAG}
          else
            xxx
          fi
          '''
          echo "Push image success"
        }
        script {
          env.BUILD_TASKS += env.STAGE_NAME + "✅" + env.TAB
        }
      }
    }
    stage('重启服务') {
      steps {
        container('kubectl') {
          sh '''
          if [ "$ENV_APP" == "test" ]; then
            kubectl set image deployment/ocr-layout layout=${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG} --namespace yili-ocr --kubeconfig kubeconfig-sxkj ||  kubectl set image deployment/ocr-layout layout=${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG} --namespace yili-ocr  --kubeconfig kubeconfig-sxkjprod
          elif [ "$ENV_APP" == "yili-dxy" ]; then
            echo "$ENV_APP"
          else
            xxx
          fi
          '''
        }
        script {
          env.BUILD_TASKS += env.STAGE_NAME + "✅" + env.TAB
        }
        sh 'printenv'
      }
    }
  }
  post {
    success {
        echo 'Congratulations!'
        sh """
            curl 'https://oapi.dingtalk.com/robot/send?access_token=19195ef5d3ce080a6966db554d03a348b56cd7c42707deebee51a353fdb2cc7d' \
                -H 'Content-Type: application/json' \
                -d '{
                    "msgtype": "markdown",
                    "markdown": {
                        "title":"${TITLE}",
                        "text": "😄👍 构建成功 👍😄  \n**项目名称**：${TITLE}  \n**Git log**: ${GIT_LOG}   \n**构建者**: ${BUILD_USER_NAME}  \n**构建分支**: ${GIT_BRANCH}   \n**构建地址**：${BUILD_URL}  \n**构建任务**: ${BUILD_TASKS}"
                    }
                }'
        """
    }
    failure {
        echo 'Oh no!'
        sh """
            curl 'https://oapi.dingtalk.com/robot/send?access_token=19195ef5d3ce080a6966db554d03a348b56cd7c42707deebee51a353fdb2cc7d' \
                -H 'Content-Type: application/json' \
                -d '{
                    "msgtype": "markdown",
                    "markdown": {
                        "title":"${TITLE}",
                        "text": "😖❌ 构建失败 ❌😖  \n**项目名称**：${TITLE}  \n**Git log**: ${GIT_LOG}   \n**构建者**: ${BUILD_USER_NAME}   \n**构建分支**: ${GIT_BRANCH}  \n**构建地址**：${BUILD_URL}  \n**构建任务**: ${BUILD_TASKS}"
                    }
                }'
        """
    }
    always {
        echo 'I will always say Hello again!'
    }
  }
}