Jenkinsfile 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. @Library('devops') _
  2. pipeline {
  3. agent {label 'jenkins-slave-jnlp'}
  4. environment { // https://stackoverflow.com/questions/53541489/updating-environment-global-variable-in-jenkins-pipeline-from-the-stage-level
  5. NAME="admin-tmpl"
  6. NAMESPACE="oppc-rcyj-web"
  7. SERVER_PORT="80"
  8. }
  9. options {
  10. // timestamps() //日志会有时间
  11. skipDefaultCheckout() //删除隐式checkout scm语句
  12. // disableConcurrentBuilds() //禁止并行,会列队式等前面的任务完才进行下一个任务
  13. timeout(time: 1, unit: 'HOURS') //流水线超时设置1h
  14. }
  15. stages {
  16. stage('Init'){
  17. steps {
  18. script{
  19. // 【测试环境】变量
  20. env.INGRESS_HOST='admin-tmpl-test.rencaiyoujia.cn'
  21. env.INGRESS_TLS_SECRET="rencaiyoujia-tls-secret"
  22. env.REPLICAS="1" //deployment的副本数
  23. KUBECONTEXT = "k8s-test-admin" //测试环境k8s
  24. NPM_RUN_BUILD_CMD="npm run build:test"
  25. DOCKERFILE_NAME="Dockerfile"
  26. // 【生产环境】变量
  27. if( "${BRANCH_NAME}"== "master"){
  28. env.INGRESS_HOST='admin-tmpl.rencaiyoujia.com'
  29. env.INGRESS_TLS_SECRET="rencaiyoujia-tls-secret"
  30. env.REPLICAS="2"
  31. KUBECONTEXT="k8s-prod-admin" //生产环境k8s
  32. NPM_RUN_BUILD_CMD="npm run build:prod"
  33. DOCKERFILE_NAME="Dockerfile"
  34. }
  35. // 【开发环境】变量
  36. if( "${BRANCH_NAME}"== "dev"){
  37. env.INGRESS_HOST='admin-tmpl-dev.rencaiyoujia.cn'
  38. env.INGRESS_TLS_SECRET="rencaiyoujia-tls-secret"
  39. env.REPLICAS="1"
  40. KUBECONTEXT="k8s-dev-admin" //开发环境k8s
  41. NPM_RUN_BUILD_CMD="npm run build:dev"
  42. DOCKERFILE_NAME="Dockerfile"
  43. }
  44. }
  45. }
  46. }
  47. stage('Checkout') {
  48. steps {
  49. checkout scm
  50. }
  51. }
  52. stage('npm打包') {
  53. steps {
  54. script{
  55. // sh 'npm config set registry https://registry.npm.taobao.org'
  56. sh 'npm install --registry=https://registry.npm.taobao.org'
  57. sh "${NPM_RUN_BUILD_CMD}"
  58. }
  59. }
  60. }
  61. stage('DockerBuild') {
  62. steps {
  63. script{
  64. sh "rm -rf docker/dist;mv dist docker"
  65. devops.docker(env.NAME,dockerfile:"docker/${DOCKERFILE_NAME}",context:"docker").build().push()
  66. }
  67. }
  68. }
  69. stage('Test') {
  70. steps {
  71. echo 'Testing..'
  72. }
  73. }
  74. stage('Deploy') {
  75. parallel {
  76. stage('deploy ingress') {
  77. when {
  78. expression { env.INGRESS_HOST != null }
  79. }
  80. steps{
  81. sh """envsubst < k8smanifests/svc.yaml | kubectl --context ${KUBECONTEXT} apply -f - ;\
  82. envsubst < k8smanifests/ingress.yaml | kubectl --context ${KUBECONTEXT} apply -f - ;\
  83. """
  84. }
  85. }
  86. stage('Deploy') {
  87. steps{
  88. sh """envsubst < k8smanifests/deploy.yaml ;\
  89. envsubst < k8smanifests/deploy.yaml | kubectl --context ${KUBECONTEXT} apply -f - ;\
  90. """
  91. }
  92. }
  93. }
  94. }
  95. }
  96. post{
  97. success{
  98. script{
  99. devops.dingtalk(env.NAME,env.DOCKERIMAGE,"构建成功 ✅")
  100. }
  101. }
  102. failure{
  103. script{
  104. devops.dingtalk(env.NAME,env.DOCKERIMAGE,"构建失败 ❌")
  105. }
  106. }
  107. unstable{
  108. script{
  109. devops.dingtalk(env.NAME,env.DOCKERIMAGE,"不稳定构建 ✅")
  110. }
  111. }
  112. aborted{
  113. script{
  114. devops.dingtalk(env.NAME,env.DOCKERIMAGE,"暂停或中断 ❌")
  115. }
  116. }
  117. }
  118. }