Jenkinsfile 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. pipeline {
  2. agent {
  3. kubernetes {
  4. yamlFile 'deploy/KubernetesPod.yaml'
  5. retries 2
  6. }
  7. }
  8. environment {
  9. IMAGE_REPO = "SXKJ:32775"
  10. IMAGE_REPO_YILI = "registry.cn-hangzhou.aliyuncs.com/sxtest"
  11. TAB = "\n \n "
  12. GIT_LOG = sh (script: "git log --oneline -n 1", returnStdout: true).trim()
  13. BUILD_USER_NAME = sh(script: "git log -1 --pretty=format:'%an'", returnStdout: true).trim()
  14. TITLE = "${JOB_BASE_NAME}-${ENV_APP}"
  15. GIT_BRANCH_NAME = env.GIT_BRANCH.substring(env.GIT_BRANCH.lastIndexOf('/') + 1)
  16. TIMESTAMP = sh(returnStdout: true, script: "TZ=Asia/Shanghai date +'%F_%H-%M-%S'").trim()
  17. TAG = "${ENV_APP}-${GIT_BRANCH_NAME}-${TIMESTAMP}"
  18. }
  19. stages {
  20. stage('git-log') {
  21. steps {
  22. script {
  23. sh 'echo "git log"'
  24. env.BUILD_TASKS = "\n \n "
  25. }
  26. sh 'printenv'
  27. }
  28. }
  29. stage('构建镜像') {
  30. steps {
  31. container('docker') {
  32. echo "构建 Docker 镜像阶段"
  33. sh '''
  34. if [ "$ENV_APP" == "yili-dxy" ]; then
  35. docker build -t ${IMAGE_REPO_YILI}/${JOB_BASE_NAME}:${TAG} --output type=docker .
  36. elif [ "$ENV_APP" == "test" ]; then
  37. docker build -t ${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG} --output type=docker .
  38. else
  39. xxx
  40. fi
  41. '''
  42. echo "build success"
  43. }
  44. script {
  45. env.BUILD_TASKS += env.STAGE_NAME + "✅" + env.TAB
  46. }
  47. }
  48. }
  49. stage('镜像推送') {
  50. steps {
  51. container('docker') {
  52. echo "Push Docker 镜像阶段"
  53. sh '''
  54. if [ "$ENV_APP" == "test" ]; then
  55. docker push ${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG}
  56. elif [ "$ENV_APP" == "yili-dxy" ]; then
  57. docker push ${IMAGE_REPO_YILI}/${JOB_BASE_NAME}:${TAG}
  58. else
  59. xxx
  60. fi
  61. '''
  62. echo "Push image success"
  63. }
  64. script {
  65. env.BUILD_TASKS += env.STAGE_NAME + "✅" + env.TAB
  66. }
  67. }
  68. }
  69. stage('重启服务') {
  70. steps {
  71. container('kubectl') {
  72. sh '''
  73. if [ "$ENV_APP" == "test" ]; then
  74. kubectl set image deployment/ocr-table table=${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG} --namespace yili-ocr --kubeconfig kubeconfig-sxkj || kubectl set image deployment/ocr-table table=${IMAGE_REPO}/${JOB_BASE_NAME}:${TAG} --namespace yili-ocr --kubeconfig kubeconfig-sxkjprod
  75. elif [ "$ENV_APP" == "yili-dxy" ]; then
  76. echo "$ENV_APP"
  77. else
  78. xxx
  79. fi
  80. '''
  81. }
  82. script {
  83. env.BUILD_TASKS += env.STAGE_NAME + "✅" + env.TAB
  84. }
  85. sh 'printenv'
  86. }
  87. }
  88. }
  89. post {
  90. success {
  91. echo 'Congratulations!'
  92. sh """
  93. curl 'https://oapi.dingtalk.com/robot/send?access_token=19195ef5d3ce080a6966db554d03a348b56cd7c42707deebee51a353fdb2cc7d' \
  94. -H 'Content-Type: application/json' \
  95. -d '{
  96. "msgtype": "markdown",
  97. "markdown": {
  98. "title":"${TITLE}",
  99. "text": "😄👍 构建成功 👍😄 \n**项目名称**:${TITLE} \n**Git log**: ${GIT_LOG} \n**构建者**: ${BUILD_USER_NAME} \n**构建分支**: ${GIT_BRANCH} \n**构建地址**:${BUILD_URL} \n**构建任务**: ${BUILD_TASKS}"
  100. }
  101. }'
  102. """
  103. }
  104. failure {
  105. echo 'Oh no!'
  106. sh """
  107. curl 'https://oapi.dingtalk.com/robot/send?access_token=19195ef5d3ce080a6966db554d03a348b56cd7c42707deebee51a353fdb2cc7d' \
  108. -H 'Content-Type: application/json' \
  109. -d '{
  110. "msgtype": "markdown",
  111. "markdown": {
  112. "title":"${TITLE}",
  113. "text": "😖❌ 构建失败 ❌😖 \n**项目名称**:${TITLE} \n**Git log**: ${GIT_LOG} \n**构建者**: ${BUILD_USER_NAME} \n**构建分支**: ${GIT_BRANCH} \n**构建地址**:${BUILD_URL} \n**构建任务**: ${BUILD_TASKS}"
  114. }
  115. }'
  116. """
  117. }
  118. always {
  119. echo 'I will always say Hello again!'
  120. }
  121. }
  122. }