-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathJenkinsfile
212 lines (209 loc) · 6.54 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def tfCmd(String command, String options = '') {
ACCESS = "export AWS_PROFILE=${PROFILE} && export TF_ENV_profile=${PROFILE}"
sh ("cd $WORKSPACE/main && ${ACCESS} && terraform init") // main
sh ("cd $WORKSPACE/base && ${ACCESS} && terraform init") // base
sh ("cd $WORKSPACE/main && terraform workspace select ${ENV_NAME} || terraform workspace new ${ENV_NAME}")
sh ("echo ${command} ${options}")
sh ("cd $WORKSPACE/main && ${ACCESS} && terraform init && terraform ${command} ${options} && terraform show -no-color > show-${ENV_NAME}.txt")
}
pipeline {
agent { node { label 'tf-slave' } }
environment {
AWS_DEFAULT_REGION = "${params.AWS_REGION}"
PROFILE = "${params.PROFILE}"
ACTION = "${params.ACTION}"
PROJECT_DIR = "terraform/main"
}
options {
buildDiscarder(logRotator(numToKeepStr: '30'))
timestamps()
timeout(time: 30, unit: 'MINUTES')
disableConcurrentBuilds()
}
parameters {
choice (name: 'AWS_REGION',
choices: ['eu-central-1','us-west-1', 'us-west-2'],
description: 'Pick A regions defaults to eu-central-1')
string (name: 'ENV_NAME',
defaultValue: 'tf-customer1',
description: 'Env or Customer name')
choice (name: 'ACTION',
choices: [ 'plan', 'apply', 'destroy'],
description: 'Run terraform plan / apply / destroy')
string (name: 'PROFILE',
defaultValue: 'tikal',
description: 'Optional. Target aws profile defaults to tikal')
string (name: 'EMAIL',
defaultValue: '[email protected]',
description: 'Optional. Email notification')
}
stages {
stage('Checkout & Environment Prep'){
steps {
script {
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) {
withCredentials([
[ $class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: 'Tikal-AWS-access',
]])
{
try {
echo "Setting up Terraform"
def tfHome = tool name: 'terraform-0.12.20',
type: 'org.jenkinsci.plugins.terraform.TerraformInstallation'
env.PATH = "${tfHome}:${env.PATH}"
currentBuild.displayName += "[$AWS_REGION]::[$ACTION]"
sh("""
/usr/local/bin/aws configure --profile ${PROFILE} set aws_access_key_id ${AWS_ACCESS_KEY_ID}
/usr/local/bin/aws configure --profile ${PROFILE} set aws_secret_access_key ${AWS_SECRET_ACCESS_KEY}
/usr/local/bin/aws configure --profile ${PROFILE} set region ${AWS_REGION}
export AWS_PROFILE=${PROFILE}
export TF_ENV_profile=${PROFILE}
mkdir -p /home/jenkins/.terraform.d/plugins/linux_amd64
""")
tfCmd('version')
} catch (ex) {
echo 'Err: Incremental Build failed with Error: ' + ex.toString()
currentBuild.result = "UNSTABLE"
}
}
}
}
}
}
stage('terraform plan') {
when { anyOf
{
environment name: 'ACTION', value: 'plan';
environment name: 'ACTION', value: 'apply'
}
}
steps {
dir("${PROJECT_DIR}") {
script {
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) {
withCredentials([
[ $class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: 'Tikal-AWS-access',
]])
{
try {
tfCmd('plan', '-detailed-exitcode -out=tfplan')
} catch (ex) {
if (ex == 2 && "${ACTION}" == 'apply') {
currentBuild.result = "UNSTABLE"
} else if (ex == 2 && "${ACTION}" == 'plan') {
echo "Update found in plan tfplan"
} else {
echo "Try running terraform again in debug mode"
}
}
}
}
}
}
}
}
stage('terraform apply') {
when { anyOf
{
environment name: 'ACTION', value: 'apply'
}
}
steps {
dir("${PROJECT_DIR}") {
script {
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) {
withCredentials([
[ $class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: 'Tikal-AWS-access',
]])
{
try {
tfCmd('apply', 'tfplan')
} catch (ex) {
currentBuild.result = "UNSTABLE"
}
}
}
}
}
}
post {
always {
archiveArtifacts artifacts: "keys/key-${ENV_NAME}.*", fingerprint: true
archiveArtifacts artifacts: "main/show-${ENV_NAME}.txt", fingerprint: true
}
}
}
stage('terraform destroy') {
when { anyOf
{
environment name: 'ACTION', value: 'destroy';
}
}
steps {
script {
def IS_APPROVED = input(
message: "Destroy ${ENV_NAME} !?!",
ok: "Yes",
parameters: [
string(name: 'IS_APPROVED', defaultValue: 'No', description: 'Think again!!!')
]
)
if (IS_APPROVED != 'Yes') {
currentBuild.result = "ABORTED"
error "User cancelled"
}
}
dir("${PROJECT_DIR}") {
script {
wrap([$class: 'AnsiColorBuildWrapper', colorMapName: 'xterm']) {
withCredentials([
[ $class: 'AmazonWebServicesCredentialsBinding',
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY',
credentialsId: 'Tikal-AWS-access',
]])
{
try {
tfCmd('destroy', '-auto-approve')
} catch (ex) {
currentBuild.result = "UNSTABLE"
}
}
}
}
}
}
}
}
post
{
always{
emailext (
body: """
<p>${ENV_NAME} - Jenkins Pipeline ${ACTION} Summary</p>
<p>Jenkins url: <a href='${env.BUILD_URL}/>link</a></p>
<p>Pipeline Blueocean: <a href='${env.JENKINS_URL}blue/organizations/jenkins/${env.JOB_NAME}/detail/${env.JOB_NAME}/${env.BUILD_NUMBER}/pipeline'>${env.JOB_NAME}(pipeline page)</a></p>
${env.JENKINS_URL}blue/organizations/jenkins/${env.JOB_NAME}/detail/${env.JOB_NAME}/${env.BUILD_NUMBER}/pipeline
<ul>
<li> Branch built: '${env.BRANCH_NAME}' </li>
<li> ACTION: $ACTION</li>
<li> REGION: ${AWS_REGION}</li>
</ul>
""",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
to: "${EMAIL}",
subject: "[${ENV_NAME}] - ${env.JOB_NAME}-${env.BUILD_NUMBER} [$AWS_REGION][$ACTION]",
attachLog: true
)
}
}
}