forked from 100daysofdevops/100daysofdevops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
80 lines (77 loc) · 2.59 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
78
79
80
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('ec2_pipeline/'){
sh "terraform init -input=false"
sh "echo \$PWD"
sh "whoami"
}
}
}
stage('TerraformFormat'){
steps {
dir('ec2_pipeline/'){
sh "terraform fmt -list=true -write=false -diff=true -check=true"
}
}
}
stage('TerraformValidate'){
steps {
dir('ec2_pipeline/'){
sh "terraform validate"
}
}
}
stage('TerraformPlan'){
steps {
dir('ec2_pipeline/'){
script {
try {
sh "terraform workspace new ${params.WORKSPACE}"
} catch (err) {
sh "terraform workspace select ${params.WORKSPACE}"
}
sh "terraform plan -var 'access_key=$AWS_ACCESS_KEY_ID' -var 'secret_key=$AWS_SECRET_ACCESS_KEY' \
-var-file 'terraform.tfvars' -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('ec2_pipeline/'){
unstash "terraform-plan"
sh 'terraform apply terraform.tfplan'
}
}
}
}
}
}
}