Jenkinsfile 3.6 KB

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