Advertisement
Guest User

Groovy deploy

a guest
Dec 7th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 5.05 KB | None | 0 0
  1. confFlags = [
  2.     confirmBranchAction: false,
  3.     confirmMasterBuild: false,
  4.     confirmMasterDeploy: false,
  5.     personWhoConfirm: "SYSTEM"
  6. ]
  7.  
  8. deployOptions = [
  9.     applicationStatus: "running"
  10. ]
  11.  
  12.  
  13. stage('Deploy to environment'){
  14.             when {
  15.               expression { confFlags['confirmBranchAction'] != true }
  16.               not {
  17.                 expression {branch_type ==~ /(pr|bugfix|feauture)/ }
  18.               }
  19.               anyOf {
  20.                 expression { confFlags['confirmMasterDeploy'] == true }
  21.                 expression { return env.BRANCH_NAME != 'master';}
  22.               }
  23.             }
  24.             steps {
  25.               script {
  26.                 def sendToChoices = ['restarted', 'started', 'stopped'].join('\n')
  27.                 promoteText = [message: "Please choose service status", description: "Choose service status" ]
  28.                 stageNamePipeline = "Deploy"
  29.                 try {
  30.  
  31.                     timeout(time: timeouts.promoteBranchAction, unit: timeouts.unit) { // change to a convenient timeout for you
  32.                         promote = input(
  33.                         id: 'app_status',
  34.                         submitterParameter: 'submitter',
  35.                         message: "${promoteText.message}",
  36.                         parameters: [choice(choices: sendToChoices, description: "${promoteText.description}", name: 'status')]
  37.                         )
  38.                     }
  39.                     println "${promote}"
  40.                     if (! checkInputPermissions("${env.SUBMITTERS}","${promote.submitter}"))
  41.                     {
  42.                       currentBuild.result = 'FAILURE'
  43.                       advancedNotifyInfo = "User `${promote.submitter}` does not have access rights for this action"
  44.                     }
  45.                     else {
  46.                       deployOptions['applicationStatus'] = "${app_status.status.toString()}"
  47.                       confFlags['personWhoConfirm'] = app_status.submitter
  48.                     }
  49.  
  50.  
  51.                 }
  52.                 catch(err) { // timeout reached or input false
  53.                     def user = err.getCauses()[0].getUser()
  54.                     if('SYSTEM' == user.toString()) { // SYSTEM means timeout.
  55.                         deployOptions['applicationStatus'] = "running"
  56.                         confFlags['personWhoConfirm'] = "SYSTEM"
  57.                         echo "Timeout"
  58.                     } else {
  59.                         deployOptions['applicationStatus'] = "running"
  60.                         confFlags['personWhoConfirm'] = "${user.toString()}"
  61.                         echo "Aborted by: [${user.toString()}]"
  62.                     }
  63.                 }
  64.               }
  65.  
  66.  
  67.               timeout (time: 360, unit: timeouts.unit) {
  68.                 script {
  69.                
  70.                       dir('code') {
  71.                         image_version = sh(script: "make get-image-version", returnStdout: true)
  72.                         image_version = image_version.replace("\n", "").replace("\r", "");
  73.                       }
  74.                       dir('code/.ansible') {
  75.                       echo "Deploy to environment"
  76.                       echo "${deployOptions.applicationStatus}"
  77.                     //sshagent(credentials: ['deployConfigs.deploy.cred_id']) {                  
  78.                       withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${deployConfigs.deploy.vault_cred_id}",
  79.                               usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
  80.                         withEnv(["VAULT_PASSWORD=${PASSWORD}"]) {
  81.                           sh "export VAULT_PASSWORD"
  82.                           echo "app_version=${image_version} deploy_env=${deployConfigs.deploy.env_type}"
  83.                           echo "Deploy service"
  84.                           ansiblePlaybook(
  85.                             playbook: 'deploy.yml',
  86.                             inventory: 'inventory/'+ deployConfigs.deploy.env_type + '/' + deployConfigs.deploy.env_type,
  87.                             credentialsId: deployConfigs.deploy.cred_id,
  88.                             extras: '--extra-vars "rservice_app_version=' + image_version + ' deploy_env=' + deployConfigs.deploy.env_type  + ' service_status=' + deployOptions.applicationStatus + '"' + ' --vault-password-file ./vault.py'
  89.                           )
  90.                         }
  91.                       }              
  92.                     }
  93.                  
  94.                 }
  95.               }
  96.             }
  97.  
  98.             post {
  99.  
  100.               success {
  101.                    notifyBuild(currentBuild.result, "${stageNamePipeline}", false, "${env.TELGRAM_MAINTAINER_USERNAME}","", null)  
  102.               }
  103.               failure {
  104.                    notifyBuild(currentBuild.result, "${stageNamePipeline}", true, "${env.TELGRAM_MAINTAINER_USERNAME}","", null)  
  105.               }
  106.               unstable {
  107.                     notifyBuild(currentBuild.result, "${stageNamePipeline}", true, "${env.TELGRAM_MAINTAINER_USERNAME}","", null)  
  108.               }
  109.            }
  110.  
  111.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement