sockshop/jenkinsfile

120 lines
5.1 KiB
Plaintext

pipeline {
agent {
label 'terraform-slave'
}
parameters {
choice(name: 'mode', choices: ['apply', 'destroy'], description: '')
}
stages {
// stage('Refresh properties') {
// steps {
// script {
// if (Refresh) {
// currentBuild.result = 'ABORTED'
// error('Stopping early…')
// }
// }
// }
// }
stage('deploy Keptn') {
steps {
container('terraform') {
script {
def b = build job: 'shared/keptn deployment' , wait: true, parameters: [string(name: 'mode', value: "apply")]
//build job: 'ANOTHER_JOB_NAME', wait: false, parameters: [string(name: 'HELLO', value: String.valueOf(PARAMETER01))]
env.keptn_api_token = b.getBuildVariables()["KEPTN_API_TOKEN"]
env.TF_VAR_KEPTN_API_TOKEN = b.getBuildVariables()["KEPTN_API_TOKEN"]
env.keptn_bridge_url = b.getBuildVariables()["KEPTN_BRIDGE_URL"]
env.TF_VAR_KEPTN_BRIDGE_URL = b.getBuildVariables()["KEPTN_BRIDGE_URL"]
}
}
}
}
stage('Download & Install Keptn CLI') {
steps {
container('terraform') {
sh "apk add --no-cache curl"
sh "apk add --no-cache bash"
sh "apk add --no-cache gawk"
sh 'wget https://github.com/keptn/keptn/releases/download/1.4.0/keptn-1.4.0-linux-amd64.tar.gz'
sh 'tar -xvf keptn-1.4.0-linux-amd64.tar.gz'
sh 'mv keptn-1.4.0-linux-amd64 /usr/local/bin/keptn'
// sh 'curl -sL https://get.keptn.sh/ | bash'
sh 'keptn set config AutomaticVersionCheck false'
sh 'keptn set config KubeContextCheck false'
sh 'keptn version'
sh "keptn auth --endpoint=${keptn_bridge_url}/api --api-token=${keptn_api_token}"
}
}
}
// stage('terraform init') {
// steps {
// container('terraform') {
// dir("terraform") {
// withCredentials([[
// $class: 'AmazonWebServicesCredentialsBinding',
// credentialsId: "f89b3f7d-23ec-42b9-9687-e4acf01d7507",
// accessKeyVariable: 'AWS_ACCESS_KEY_ID',
// secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
// sh 'terraform init -backend-config="key=keptn-sockshop-demo"'
// }
// }
// }
// }
// }
stage('Create Keptn Project') {
when {
expression {
return params.mode == 'apply'
}
}
steps {
container('terraform') {
dir("terraform") {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: "f89b3f7d-23ec-42b9-9687-e4acf01d7507",
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],
string(credentialsId: 'gitea_demo_api_token', variable: 'TF_VAR_GITEA_TOKEN')]) {
sh 'terraform init -backend-config="key=keptn-sockshop-demo"'
sh 'terraform apply -auto-approve'
}
}
}
}
}
stage('Destroy Keptn Project') {
when {
expression {
return params.mode == 'destroy'
}
}
steps {
container('terraform') {
sh "keptn auth --endpoint=${keptn_bridge_url}/api --api-token=${keptn_api_token}"
dir("terraform") {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: "f89b3f7d-23ec-42b9-9687-e4acf01d7507",
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],
string(credentialsId: 'gitea_demo_api_token', variable: 'TF_VAR_GITEA_TOKEN')]) {
sh 'terraform init -backend-config="key=keptn-sockshop-demo"'
sh 'terraform destroy --target null_resource.seed-unix-destroy -auto-approve'
sh 'terraform destroy --target gitea_repository.demo-repo -auto-approve'
}
}
build job: 'shared/keptn deployment' , wait: true, parameters: [string(name: 'mode', value: "destroy")]
}
}
}
}
}