-
Notifications
You must be signed in to change notification settings - Fork 20
/
Jenkinsfile
77 lines (77 loc) · 2.52 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
pipeline {
agent any
parameters {
choice(
choices: ['plan', 'apply', 'destroy'],
description: 'Terraform action to apply',
name: 'action')
choice(
choices: ['dev', 'qa'],
description: 'deployment environment',
name: 'ENVIRONMENT')
}
stages {
stage('init') {
steps {
sh 'rm -rf .terraform'
sh 'terraform init -no-color -backend-config="${ENVIRONMENT}/${ENVIRONMENT}.tfbackend" '
}
}
stage('validate') {
steps {
sh 'terraform validate -no-color'
}
}
stage('plan') {
when {
expression { params.action == 'plan' || params.action == 'apply' }
}
steps {
sh 'terraform plan -no-color -input=false -out=tfplan --var-file=${ENVIRONMENT}/${ENVIRONMENT}.tfvars'
}
}
stage('approval') {
when {
expression { params.action == 'apply'}
}
steps {
sh 'terraform show -no-color tfplan > tfplan.txt'
script {
def plan = readFile 'tfplan.txt'
input message: "Apply the plan?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
}
}
}
stage('apply') {
when {
expression { params.action == 'apply' }
}
steps {
sh 'terraform apply -no-color -input=false tfplan'
}
}
stage('preview-destroy') {
when {
expression {params.action == 'destroy'}
}
steps {
sh 'terraform plan -no-color -destroy -out=tfplan --var-file=${ENVIRONMENT}/${ENVIRONMENT}.tfvars'
sh 'terraform show -no-color tfplan > tfplan.txt'
}
}
stage('destroy') {
when {
expression { params.action == 'destroy' }
}
steps {
script {
def plan = readFile 'tfplan.txt'
input message: "Delete the stack?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
}
sh 'terraform destroy -no-color --auto-approve'
}
}
}
}