Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. @Library("smrt-jenkins-lib@deploy_pipeline") _
  2.  
  3. pipeline {
  4. agent {
  5. node {
  6. label 'jenkins-slave'
  7. customWorkspace "/home/ec2-user/workspace/${JOB_NAME}/${ENV}"
  8. }
  9. }
  10. // Options
  11. options {
  12. timeout(time: 20, unit: 'MINUTES')
  13. // disableConcurrentBuilds()
  14. timestamps()
  15. buildDiscarder(logRotator(daysToKeepStr: '14', numToKeepStr: '10'))
  16. ansiColor colorMapName: 'XTerm'
  17. }
  18.  
  19. environment {
  20. def WORKSPACE = pwd()
  21. def SERVICES = "smrt nps inventory transfer"
  22. }
  23.  
  24. stages {
  25. stage('Checkout code') {
  26. steps {
  27. echo 'Entering stage \u27A1 code checkout'
  28. script {
  29. getArtifactVersion()
  30. echo "ENV = ${ENV}"
  31. echo "VERSION = ${VERSION}"
  32. echo "DB_DROP = ${DB_DROP}"
  33. echo "GIT_REV = ${GIT_REV}"
  34. echo "WORKSPACE = ${WORKSPACE}"
  35. }
  36. // cleanWs()
  37. // checkout scm
  38. checkout([
  39. $class : 'GitSCM',
  40. branches : [[
  41. name: "${GIT_REV}"]],
  42. doGenerateSubmoduleConfigurations: false,
  43. extensions : [],
  44. submoduleCfg : [],
  45. userRemoteConfigs : [[credentialsId: 'SMRT-Git-Key',
  46. url : 'ssh://git@git.nordstrom.net/mim/smrt.git']]
  47. ])
  48. script { currentBuild.displayName = "#" + currentBuild.number + "_${VERSION}" + "_${ENV}" }
  49.  
  50. }
  51. }
  52. stage('Setup') {
  53. steps {
  54. echo 'Entering stage \u27A1 Setup'
  55. script {
  56. dir("${WORKSPACE}/devops/Terraform/SMRT") {
  57. sh "terraform init >/dev/null 2>&1"
  58. try {
  59. sh "terraform workspace select ${ENV} >/dev/null 2>&1"
  60. new_env = 0
  61. echo 'this is existing env'
  62. }
  63. catch (e) {
  64. sh "terraform workspace new ${ENV} >/dev/null 2>&1"
  65. new_env = 1
  66. echo 'New env is created in terraform workspace'
  67. }
  68. setupVariables(
  69. WORKSPACE: "${WORKSPACE}",
  70. ENV: "${ENV}"
  71. )
  72. echo "New env? = ${new_env}"
  73. }
  74. }
  75. }
  76. }
  77. stage('Migrating DB') {
  78. steps {
  79. echo 'Entering stage \u27A1 Migrating DB'
  80. script {
  81. // echo "inventory_url = ${inventory_url}"
  82. // echo "profile = ${profile}"
  83. // echo "smrt_db_password = ${smrt_db_password}"
  84. if ("${DB_DROP}") {
  85. echo "Database is dropped"
  86. // dropMySQLDatabase(
  87. // smrt_db_name: "smrt",
  88. // smrt_db_username: "${smrt_db_username}",
  89. // smrt_db_password: "${smrt_db_password}",
  90. // endpoint_database: "${endpoint_database}")
  91. echo "Migrationg database"
  92. // migratedMySQLDatabase(
  93. // VERSION: "${VERSION}",
  94. // smrt_db_username: "${smrt_db_username}",
  95. // smrt_db_password: "${smrt_db_password}",
  96. // smrt_db_url: "${smrt_db_url}",
  97. // new_env: "${new_env}")
  98. } else {
  99. echo "Database will not be dropped and no migration"
  100. }
  101. }
  102. }
  103. }
  104. stage('Deploying') {
  105. steps {
  106. echo 'Entering stage \u27A1 Deploying'
  107. dir("devops/Terraform/SMRT") {
  108. script {
  109. echo "Running Deployment...."
  110. // echo "endpoint_database = ${endpoint_database}"
  111. // deployEnv()
  112. }
  113. }
  114. }
  115. }
  116. stage('PostDeploy Check') {
  117. steps {
  118. echo 'Entering stage \u27A1 PostDeploy Check'
  119. script {
  120. echo "Running post deploying checks"
  121. // postDeployCheck(
  122. // VERSION: "${VERSION}",
  123. // ENV: "${ENV}",
  124. // SERVICES: "${SERVICES}"
  125. // )
  126. }
  127. }
  128. }
  129. }
  130. post {
  131. // always {
  132. // junit allowEmptyResults: true, testResults: 'fake-inventory/**/target/**/*.xml'
  133. // cleanWs()
  134. // }
  135. success {
  136. // archiveArtifacts artifacts: "**/*.log.txt, **/terraform-*.vars", onlyIfSuccessful: true
  137. script {
  138. currentBuild.result = 'SUCCESS'
  139. }
  140. // step([$class: 'StashNotifier'])
  141. }
  142. }
  143. }
  144.  
  145. // def setupVariables() {
  146. // sh '''
  147. sed -e /\\{/d -e /\\}/d ${WORKSPACE}/devops/Terraform/SMRT/${ENV}.tfvars > ${WORKSPACE}/tmp.tfvars
  148. terraform output | sed -e s/\\$/\\"/g -e s/'= '/'= "'/g 1>> ${WORKSPACE}/tmp.tfvars
  149. '''
  150. load("${WORKSPACE}/tmp.tfvars")
  151. // }
  152.  
  153. // def dropMySQLDatabase(Map config) {
  154. // def DROP_SQL = "DROP DATABASE IF EXISTS ${config.smrt_db_name};\n CREATE DATABASE ${config.smrt_db_name} CHARACTER SET utf8 COLLATE utf8_general_ci;"
  155. // def SHELL_CMD = "mysql -u \"${config.smrt_db_username}\" --password=\"${config.smrt_db_password}\" -h \"${config.endpoint_database}\" <<-EOF\n${DROP_SQL}\nEOF"
  156. // sh "${SHELL_CMD}"
  157. // }
  158.  
  159. // def migratedMySQLDatabase(Map config) {
  160. // script() {
  161. // // Fetch migration artifact
  162. // sh "aws s3api get-object --bucket smrt-liquibase-migrations --key smrt-liquibase-${config.VERSION}.jar ./smrt-liquibase-${config.VERSION}.jar"
  163. // sh "md5sum ./smrt-liquibase-${config.VERSION}.jar"
  164.  
  165. // if ("${new_env}" != "1") {
  166. // echo "Starting liquibase DB migration"
  167.  
  168. // // Apply migration
  169. // echo "**** APPLYING MIGRATION smrt-liquibase-${config.VERSION}.jar TO ${config.smrt_db_url} ****"
  170.  
  171. // sh "java -jar -Dsmrt_db_url=${config.smrt_db_url} -Dsmrt_db_username=${config.smrt_db_username} -Dsmrt_db_password=${config.smrt_db_password} ./smrt-liquibase-${config.VERSION}.jar"
  172.  
  173. // echo "Finished liquibase DB migration"
  174. // } else {
  175. // echo "Liquibase migration will be executed after terraform invocation"
  176. // }
  177. // }
  178. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement