Jenkinsfile 4.1 KB

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