159 lines
7.8 KiB
Groovy
159 lines
7.8 KiB
Groovy
// Get the build name using the directory structure in Jenkins
|
|
String[] buildElements = env.JOB_NAME.split('/')
|
|
String buildName = buildElements[buildElements.length - 2]
|
|
String release = "${env.RELEASE_VERSION ?: "SNAPSHOT"}"
|
|
String tag = "packages-$release"
|
|
String repository = "ssh://git@collaboration.msi.audi.com:4444/vsdsinf/${buildName}.git"
|
|
|
|
def modules_paths = [
|
|
"modules/aws/dynatrace",
|
|
"packages/eu-west-1/int",
|
|
"packages/eu-west-1/prelive",
|
|
"packages/us-east-1/prelive",
|
|
]
|
|
|
|
def terraform_check(paths) {
|
|
paths.each { path ->
|
|
sh("terraform -chdir=${path} init -backend=false")
|
|
sh("terraform -chdir=${path} fmt -recursive --check")
|
|
}
|
|
}
|
|
|
|
|
|
pipeline {
|
|
agent {
|
|
node {
|
|
label 'vsds-terraform'
|
|
}
|
|
}
|
|
|
|
parameters {
|
|
choice choices: ['none', 'eu-west-1/int', 'eu-west-1/prelive', 'us-east-1/prelive'], description: 'Please choose the environment for this deployment', name: 'TARGET'
|
|
}
|
|
|
|
environment {
|
|
GIT_SSH_COMMAND = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
|
|
}
|
|
|
|
options {
|
|
timestamps()
|
|
ansiColor('xterm')
|
|
disableConcurrentBuilds(abortPrevious: true)
|
|
timeout(time: 1, unit: 'HOURS')
|
|
}
|
|
|
|
stages {
|
|
stage("Check Tag") {
|
|
when { expression { !release.equals("SNAPSHOT") } }
|
|
steps {
|
|
sshagent(credentials: ['sofa-user-automation']) {
|
|
script {
|
|
env.PLAN_STATUS = sh(script: """
|
|
| if [[ "\$(git ls-remote origin 2>/dev/null | grep 'refs/tags/$tag\$')" ]]; then
|
|
| echo "Release $tag exists"
|
|
| exit 1
|
|
| fi
|
|
""".stripMargin('| '), returnStatus: true)
|
|
|
|
if (env.PLAN_STATUS == "1") {
|
|
currentBuild.result = "FAILURE"
|
|
error('Aborting the build.')
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
stage('Terraform validate') {
|
|
steps {
|
|
container('terraform') {
|
|
sshagent(credentials: ['sofa-user-automation']) {
|
|
script {
|
|
terraform_check(modules_paths)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Terraform plan') {
|
|
when { expression { params.TARGET != "none"} }
|
|
steps {
|
|
container('terraform') {
|
|
sshagent(credentials: ['sofa-user-automation']) {
|
|
script {
|
|
if ( params.TARGET == 'eu-west-1/int' ) {
|
|
withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_INT', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_INT', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) {
|
|
sh "terraform -chdir=packages/${params.TARGET} init"
|
|
statusCode = sh(script: "terraform -chdir=packages/${TARGET} plan -detailed-exitcode", returnStatus: true) as String
|
|
}
|
|
} else if ( params.TARGET == 'eu-west-1/prelive' ) {
|
|
withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) {
|
|
sh "terraform -chdir=packages/${params.TARGET} init"
|
|
statusCode = sh(script: "terraform -chdir=packages/${TARGET} plan -detailed-exitcode", returnStatus: true) as String
|
|
}
|
|
} else if ( params.TARGET == 'us-east-1/prelive' ) {
|
|
withCredentials([string(credentialsId: 'APIURL_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) {
|
|
sh "terraform -chdir=packages/${params.TARGET} init"
|
|
statusCode = sh(script: "terraform -chdir=packages/${TARGET} plan -detailed-exitcode", returnStatus: true) as String
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Terraform apply') {
|
|
when { beforeInput true; expression { params.TARGET != "none" }; expression { statusCode == "2" } }
|
|
input {
|
|
message "Applying terraform code ?"
|
|
submitter "cicd_ACDC_administrators"
|
|
}
|
|
steps {
|
|
container('terraform') {
|
|
sshagent(credentials: ['sofa-user-automation']) {
|
|
script {
|
|
if ( params.TARGET == 'eu-west-1/int' ) {
|
|
withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_INT', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_INT', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) {
|
|
sh "terraform -chdir=packages/${params.TARGET} apply"
|
|
}
|
|
if ( params.TARGET == 'eu-west-1/prelive' ) {
|
|
withCredentials([string(credentialsId: 'APIURL_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_EU_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) {
|
|
sh "terraform -chdir=packages/${params.TARGET} apply"
|
|
}
|
|
} else if ( params.TARGET == 'us-east-1/prelive' ) {
|
|
withCredentials([string(credentialsId: 'APIURL_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_ENV_URL'), string(credentialsId: 'APITOKEN_DYNATRACE_US_PRELIVE', variable: 'DYNATRACE_API_TOKEN'), string(credentialsId: 'packer_dynatrace_aws_access_key_id', variable: 'AWS_ACCESS_KEY_ID'), string(credentialsId: 'packer_dynatrace_aws_secret_access_key', variable: 'AWS_SECRET_ACCESS_KEY')]) {
|
|
sh "terraform -chdir=packages/${params.TARGET} apply"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Create tag') {
|
|
when { expression { !release.equals("SNAPSHOT") } }
|
|
steps {
|
|
sshagent(credentials: ['sofa-user-automation']) {
|
|
sh """
|
|
| git tag $tag
|
|
| git push origin $tag
|
|
""".stripMargin('| ')
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
always {
|
|
cleanWs()
|
|
script {
|
|
currentBuild.result = currentBuild.result ?: 'SUCCESS'
|
|
notifyBitbucket(projectKey: 'packages')
|
|
}
|
|
}
|
|
}
|
|
} |