-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile
67 lines (62 loc) · 2.86 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
node {
// Mark the code checkout 'Checkout'....
stage 'Checkout'
// // Get some code from a GitHub repository
git url: '[email protected]:andriikut/test.git'
// Get the Terraform tool.
def tfHome = tool name: 'Terraform', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
env.PATH = "${tfHome}:${env.PATH}"
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) {
// Mark the code build 'plan'....
stage name: 'Plan', concurrency: 1
// Output Terraform version
sh "terraform --version"
//Remove the terraform state file so we always start from a clean state
if (fileExists(".terraform/terraform.tfstate")) {
sh "rm -rf .terraform/terraform.tfstate"
}
if (fileExists("status")) {
sh "rm status"
}
sh "./init"
sh "terraform get"
sh "set +e; terraform plan -out=plan.out -detailed-exitcode; echo \$? > status"
def exitCode = readFile('status').trim()
def apply = false
echo "Terraform Plan Exit Code: ${exitCode}"
if (exitCode == "0") {
currentBuild.result = 'SUCCESS'
}
if (exitCode == "1") {
slackSend channel: '#ci', color: '#0080ff', message: "Plan Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
currentBuild.result = 'FAILURE'
}
if (exitCode == "2") {
stash name: "plan", includes: "plan.out"
slackSend channel: '#ci', color: 'good', message: "Plan Awaiting Approval: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
try {
input message: 'Apply Plan?', ok: 'Apply'
apply = true
} catch (err) {
slackSend channel: '#ci', color: 'warning', message: "Plan Discarded: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
apply = false
currentBuild.result = 'UNSTABLE'
}
}
if (apply) {
stage name: 'Apply', concurrency: 1
unstash 'plan'
if (fileExists("status.apply")) {
sh "rm status.apply"
}
sh 'set +e; terraform apply plan.out; echo \$? > status.apply'
def applyExitCode = readFile('status.apply').trim()
if (applyExitCode == "0") {
slackSend channel: '#ci', color: 'good', message: "Changes Applied ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
} else {
slackSend channel: '#ci', color: 'danger', message: "Apply Failed: ${env.JOB_NAME} - ${env.BUILD_NUMBER} ()"
currentBuild.result = 'FAILURE'
}
}
}
}