Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Library("smrt-jenkins-lib@deploy_pipeline") _
- pipeline {
- agent {
- node {
- label 'jenkins-slave'
- customWorkspace "/home/ec2-user/workspace/${JOB_NAME}/${ENV}"
- }
- }
- // Options
- options {
- timeout(time: 20, unit: 'MINUTES')
- // disableConcurrentBuilds()
- timestamps()
- buildDiscarder(logRotator(daysToKeepStr: '14', numToKeepStr: '10'))
- ansiColor colorMapName: 'XTerm'
- }
- environment {
- def WORKSPACE = pwd()
- def SERVICES = "smrt nps inventory transfer"
- }
- stages {
- stage('Checkout code') {
- steps {
- echo 'Entering stage \u27A1 code checkout'
- script {
- getArtifactVersion()
- echo "ENV = ${ENV}"
- echo "VERSION = ${VERSION}"
- echo "DB_DROP = ${DB_DROP}"
- echo "GIT_REV = ${GIT_REV}"
- echo "WORKSPACE = ${WORKSPACE}"
- }
- // cleanWs()
- // checkout scm
- checkout([
- $class : 'GitSCM',
- branches : [[
- name: "${GIT_REV}"]],
- doGenerateSubmoduleConfigurations: false,
- extensions : [],
- submoduleCfg : [],
- userRemoteConfigs : [[credentialsId: 'SMRT-Git-Key',
- url : 'ssh://git@git.nordstrom.net/mim/smrt.git']]
- ])
- script { currentBuild.displayName = "#" + currentBuild.number + "_${VERSION}" + "_${ENV}" }
- }
- }
- stage('Setup') {
- steps {
- echo 'Entering stage \u27A1 Setup'
- script {
- dir("${WORKSPACE}/devops/Terraform/SMRT") {
- sh "terraform init >/dev/null 2>&1"
- try {
- sh "terraform workspace select ${ENV} >/dev/null 2>&1"
- new_env = 0
- echo 'this is existing env'
- }
- catch (e) {
- sh "terraform workspace new ${ENV} >/dev/null 2>&1"
- new_env = 1
- echo 'New env is created in terraform workspace'
- }
- setupVariables(
- WORKSPACE: "${WORKSPACE}",
- ENV: "${ENV}"
- )
- echo "New env? = ${new_env}"
- }
- }
- }
- }
- stage('Migrating DB') {
- steps {
- echo 'Entering stage \u27A1 Migrating DB'
- script {
- // echo "inventory_url = ${inventory_url}"
- // echo "profile = ${profile}"
- // echo "smrt_db_password = ${smrt_db_password}"
- if ("${DB_DROP}") {
- echo "Database is dropped"
- // dropMySQLDatabase(
- // smrt_db_name: "smrt",
- // smrt_db_username: "${smrt_db_username}",
- // smrt_db_password: "${smrt_db_password}",
- // endpoint_database: "${endpoint_database}")
- echo "Migrationg database"
- // migratedMySQLDatabase(
- // VERSION: "${VERSION}",
- // smrt_db_username: "${smrt_db_username}",
- // smrt_db_password: "${smrt_db_password}",
- // smrt_db_url: "${smrt_db_url}",
- // new_env: "${new_env}")
- } else {
- echo "Database will not be dropped and no migration"
- }
- }
- }
- }
- stage('Deploying') {
- steps {
- echo 'Entering stage \u27A1 Deploying'
- dir("devops/Terraform/SMRT") {
- script {
- echo "Running Deployment...."
- // echo "endpoint_database = ${endpoint_database}"
- // deployEnv()
- }
- }
- }
- }
- stage('PostDeploy Check') {
- steps {
- echo 'Entering stage \u27A1 PostDeploy Check'
- script {
- echo "Running post deploying checks"
- // postDeployCheck(
- // VERSION: "${VERSION}",
- // ENV: "${ENV}",
- // SERVICES: "${SERVICES}"
- // )
- }
- }
- }
- }
- post {
- // always {
- // junit allowEmptyResults: true, testResults: 'fake-inventory/**/target/**/*.xml'
- // cleanWs()
- // }
- success {
- // archiveArtifacts artifacts: "**/*.log.txt, **/terraform-*.vars", onlyIfSuccessful: true
- script {
- currentBuild.result = 'SUCCESS'
- }
- // step([$class: 'StashNotifier'])
- }
- }
- }
- // def setupVariables() {
- // sh '''
- sed -e /\\{/d -e /\\}/d ${WORKSPACE}/devops/Terraform/SMRT/${ENV}.tfvars > ${WORKSPACE}/tmp.tfvars
- terraform output | sed -e s/\\$/\\"/g -e s/'= '/'= "'/g 1>> ${WORKSPACE}/tmp.tfvars
- '''
- load("${WORKSPACE}/tmp.tfvars")
- // }
- // def dropMySQLDatabase(Map config) {
- // 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;"
- // def SHELL_CMD = "mysql -u \"${config.smrt_db_username}\" --password=\"${config.smrt_db_password}\" -h \"${config.endpoint_database}\" <<-EOF\n${DROP_SQL}\nEOF"
- // sh "${SHELL_CMD}"
- // }
- // def migratedMySQLDatabase(Map config) {
- // script() {
- // // Fetch migration artifact
- // sh "aws s3api get-object --bucket smrt-liquibase-migrations --key smrt-liquibase-${config.VERSION}.jar ./smrt-liquibase-${config.VERSION}.jar"
- // sh "md5sum ./smrt-liquibase-${config.VERSION}.jar"
- // if ("${new_env}" != "1") {
- // echo "Starting liquibase DB migration"
- // // Apply migration
- // echo "**** APPLYING MIGRATION smrt-liquibase-${config.VERSION}.jar TO ${config.smrt_db_url} ****"
- // 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"
- // echo "Finished liquibase DB migration"
- // } else {
- // echo "Liquibase migration will be executed after terraform invocation"
- // }
- // }
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement