Advertisement
andreevm

Untitled

Dec 25th, 2019
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.69 KB | None | 0 0
  1. #!/usr/bin/env groovy
  2. @Library('glib') _
  3. def branch = env.branch
  4. def image = 'website-middle'
  5. def imageTag = env.BUILD_TAG
  6. def repo_url = 'https://git.cashwagon.com/pdl/website_middle'
  7. def country = env.country
  8. def helm_name = 'testing-middle'
  9. def k8s_cluster = 'arn:aws:eks:ap-southeast-1:843334049208:cluster/cw-eks-cluster-test'
  10.  
  11. properties([
  12.         gitLabConnection('https://git.cashwagon.com'),
  13.         disableConcurrentBuilds(),
  14.         parameters([
  15.                 choice(
  16.                         name: 'country',
  17.                         choices: ['vn', 'lk', 'ph', "id"],
  18.                         description: 'Choose country' ),
  19.                 gitParameter(branch: '',
  20.                         branchFilter: 'origin/(.*)',
  21.                         defaultValue: 'develop',
  22.                         description: '',
  23.                         name: 'branch',
  24.                         quickFilterEnabled: true,
  25.                         selectedValue: 'DEFAULT',
  26.                         sortMode: 'DESCENDING_SMART',
  27.                         tagFilter: '*',
  28.                         useRepository: repo_url,
  29.                         listSize: '10',
  30.                         type: 'PT_BRANCH_TAG')
  31.         ])
  32. ])
  33.  
  34. currentBuild.description = "Deploy and test site: ${country} (branch: ${branch})"
  35.  
  36. try {
  37.     slackNotifierStart(branch, env.gitlabUserName)
  38.     node('gitlab') {
  39.         stage("Build ${image} docker image") {
  40.             git branch: branch, credentialsId: '75b6d7cb-3de1-4ea1-9c47-f02129759b19', url: repo_url
  41.             withDockerRegistry(credentialsId: 'harbor-registry', url: 'https://harbor.cashwagon.com') {
  42.                 def app = docker.build("harbor.cashwagon.com/ci-images/${image}", '.')
  43.                 app.push("${imageTag}")
  44.             }
  45.         }
  46.         stage("DB migrate") {
  47.             withCredentials([
  48.                     string(credentialsId: 'CONSUL_TOKEN', variable: 'CONSUL_TOKEN'),
  49.                     string(credentialsId: 'CONSUL_AUTH', variable: 'CONSUL_AUTH')
  50.             ]){
  51.                 withDockerRegistry(credentialsId: 'harbor-registry', url: 'https://harbor.cashwagon.com') {
  52.                     sh "docker run --rm -e CONSUL_ADDR=https://consul.cashwagon.com -e ENV=k8s" +
  53.                             " -e APPLICATION=website_middle_${country} -e CONSUL_TOKEN=${CONSUL_TOKEN} " +
  54.                             " -e CONSUL_AUTH=${CONSUL_AUTH} harbor.cashwagon.com/ci-images/${image}:${imageTag} " +
  55.                             "'bundle exec rake db:migrate' "
  56.                 }
  57.             }
  58.         }
  59.         stage("DB seed") {
  60.             withCredentials([
  61.                     string(credentialsId: 'CONSUL_TOKEN', variable: 'CONSUL_TOKEN'),
  62.                     string(credentialsId: 'CONSUL_AUTH', variable: 'CONSUL_AUTH')
  63.             ]){
  64.                 withDockerRegistry(credentialsId: 'harbor-registry', url: 'https://harbor.cashwagon.com') {
  65.                     sh "docker run --rm -e CONSUL_ADDR=https://consul.cashwagon.com -e ENV=k8s" +
  66.                             " -e APPLICATION=website_middle_${country} -e CONSUL_TOKEN=${CONSUL_TOKEN} " +
  67.                             " -e CONSUL_AUTH=${CONSUL_AUTH} harbor.cashwagon.com/ci-images/${image}:${imageTag} " +
  68.                             "'bundle exec rake db:seed' "
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     node('master') {
  74.         stage("Deploy helm release ${helm_name}") {
  75.             git branch: branch, credentialsId: '75b6d7cb-3de1-4ea1-9c47-f02129759b19', url: repo_url
  76.             sh "kubectl config use-context ${k8s_cluster}"
  77.             sh "helm upgrade -i ${helm_name} ./charts -f ./charts/values-stage-${country}.yaml " +
  78.                     "--set imageTag=${env.BUILD_TAG},CONSUL_APPLICATION=website_middle_${country} " +
  79.                     "--kube-context ${k8s_cluster} --wait"
  80.         }
  81.     }
  82. } catch (e) {
  83.     currentBuild.result = 'FAILURE'
  84.     throw e
  85. } finally {
  86.     def currentResult = currentBuild.result ?: 'SUCCESS'
  87.     slackNotifier(currentResult)
  88. }
  89.  
  90.  
  91. // Auto-testing
  92. try {
  93.     node('qa-worker') {
  94.         sleep 30
  95.         stage('auto-testing') {
  96.             parallel (
  97.                 "createAppViaAPI" : {
  98.                     build job: 'qa_site_api_create_app', parameters: [string(name: 'country', value: country)]
  99.                 }
  100.             )
  101.         }
  102.     }
  103. } catch (e) {
  104.     currentBuild.result = "UNSTABLE"
  105.     slackSend color: "danger", channel: "#site_deploy_at", message: "Auto-testing after deployment failed on *stage [${country}]* (<https://jenkins.cashwagon.com/blue/organizations/jenkins/website_middle_stage_k8s/detail/website_middle_stage_k8s/${env.BUILD_NUMBER}/pipeline|${env.JOB_NAME} - build #${env.BUILD_NUMBER}>)! :x:"
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement