Skip to content

Instantly share code, notes, and snippets.

@100daysofdevops
Created March 17, 2019 02:01
Show Gist options
  • Select an option

  • Save 100daysofdevops/5198cee5282a38e7d2a04eb314f0f105 to your computer and use it in GitHub Desktop.

Select an option

Save 100daysofdevops/5198cee5282a38e7d2a04eb314f0f105 to your computer and use it in GitHub Desktop.
pipeline {
agent any
tools {
"org.jenkinsci.plugins.terraform.TerraformInstallation" "terraform-0.11.8"
}
parameters {
string(name: 'WORKSPACE', defaultValue: 'development', description:'setting up workspace for terraform')
}
environment {
TF_HOME = tool('terraform-0.11.8')
TF_IN_AUTOMATION = "true"
PATH = "$TF_HOME:$PATH"
ACCESS_KEY = credentials('AWS_ACCESS_KEY_ID')
SECRET_KEY = credentials('AWS_SECRET_ACCESS_KEY')
}
stages {
stage('TerraformInit'){
steps {
dir('jenkins-terraform-pipeline/ec2_pipeline/'){
sh "terraform init -input=false"
sh "echo \$PWD"
sh "whoami"
}
}
}
stage('TerraformFormat'){
steps {
dir('jenkins-terraform-pipeline/ec2_pipeline/'){
sh "terraform fmt -list=true -write=false -diff=true -check=true"
}
}
}
stage('TerraformValidate'){
steps {
dir('jenkins-terraform-pipeline/ec2_pipeline/'){
sh "terraform validate"
}
}
}
stage('TerraformPlan'){
steps {
dir('jenkins-terraform-pipeline/ec2_pipeline/'){
script {
try {
sh "terraform workspace new ${params.WORKSPACE}"
} catch (err) {
sh "terraform workspace select ${params.WORKSPACE}"
}
sh "terraform plan -var 'access_key=$ACCESS_KEY' -var 'secret_key=$SECRET_KEY' \
-out terraform.tfplan;echo \$? > status"
stash name: "terraform-plan", includes: "terraform.tfplan"
}
}
}
}
stage('TerraformApply'){
steps {
script{
def apply = false
try {
input message: 'Can you please confirm the apply', ok: 'Ready to Apply the Config'
apply = true
} catch (err) {
apply = false
currentBuild.result = 'UNSTABLE'
}
if(apply){
dir('jenkins-terraform-pipeline/ec2_pipeline/'){
unstash "terraform-plan"
sh 'terraform apply terraform.tfplan'
}
}
}
}
}
}
}
@abgm
Copy link
Copy Markdown

abgm commented Jul 26, 2019

+1
I'm using this project to validate plans on GCP maybe exist something like for AWS :)

https://github.com/GoogleCloudPlatform/terraform-validator

 sh "terraform-validator validate --policy-path=${POLICY_PATH}  terraform.tfplan"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment