-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
156 lines (140 loc) · 5.21 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
/*Declarado variavel TARGET vazia pois caso nao escolhido destroyTarget a variavel nao era instanciada*/
def TARGET = ' '
pipeline {
agent {
kubernetes {
label "worker-tf-${UUID.randomUUID().toString()}"
yamlFile './pipeline/JenkinsContainers.yml'
}
}
/*
Variable JOB_BASE_NAME is name of JOB in Jenkins, and this name is the same in the structure of directory in repository below:
*/
stages {
stage('TF plan'){
when {
anyOf {
environment name: 'ACTION', value: 'create'
environment name: 'ACTION', value: 'update'
}
}
steps {
plan(env.ENVIRONMENT, env.REGION, env.ACTION)
}
}
stage('TF plan Destroy all') {
when {
environment name: 'ACTION', value: 'destroy'
}
steps {
planDestroy(env.ENVIRONMENT, env.REGION, env.ACTION)
}
}
stage('TF plan Destroy Target') {
when {
environment name: 'ACTION', value: 'destroyTarget'
}
steps {
show(env.ENVIRONMENT, env.REGION)
timeout(time: 10, unit: "MINUTES") {
script {
TARGET = input message: 'Digite o resource a ser destruido: ', ok: 'confirma', parameters: [string(defaultValue: 'nulo', description: 'ResourceName', name: 'RSName')]
}
}
planDestroy(env.ENVIRONMENT, env.REGION, env.ACTION, "${TARGET}")
}
}
stage('Approval') {
steps {
timeout(time: 10, unit: "MINUTES") {
script {
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
}
}
}
}
stage('TF Apply') {
when {
anyOf {
environment name: 'ACTION', value: 'create'
environment name: 'ACTION', value: 'update'
}
}
steps {
apply(env.ENVIRONMENT)
}
}
stage('TF Destroy') {
when {
anyOf {
environment name: 'ACTION', value: 'destroy'
environment name: 'ACTION', value: 'destroyTarget'
}
}
steps {
destroy(env.ENVIRONMENT, env.REGION, "${TARGET}")
}
}
}
}
def install_dependencies() {
sh """
apk add curl bash groff py-pip jq
curl --silent --location 'https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_Linux_amd64.tar.gz' | tar x -z -C /tmp
mv -v /tmp/eksctl /usr/local/bin
pip install awscli
"""
}
def plan(String environment, String region, String action) {
script {
currentBuild.displayName += " - ${environment} - ${region} - ${action}}"
currentBuild.description = "[${environment}] ${region} - ${action}}"
}
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'terraform_' + environment]]) {
container('terraform') {
install_dependencies()
sh 'cd $JOB_BASE_NAME; terraform init -no-color -backend-config="region=' + region + '" -backend-config=env/' + environment + '.backend'
sh 'cd $JOB_BASE_NAME; terraform plan -no-color -input=false -var "aws_region=' + region + '" -var-file=env/' + environment + '.tfvars -out $JOB_BASE_NAME-plan'
}
}
}
def planDestroy(String environment, String region, String action, String target=null) {
def targetM = (target == null) ? '' : '-target='+ target
script{
currentBuild.displayName += " - ${environment} - ${region} - ${action}}"
currentBuild.description = "[${environment}] ${region} - ${action}}"
}
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'terraform_' + environment]]) {
container('terraform') {
install_dependencies()
sh 'cd $JOB_BASE_NAME; terraform init -no-color -backend-config="region=' + region + '" -backend-config=env/' + environment + '.backend'
sh 'cd $JOB_BASE_NAME; terraform plan -destroy ' + targetM +' -no-color -input=false -var "aws_region=' + region + '" -var-file=env/' + environment + '.tfvars -out $JOB_BASE_NAME-plan'
}
}
}
def apply(String environment) {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'terraform_' + environment]]) {
container('terraform') {
install_dependencies()
sh 'cd $JOB_BASE_NAME;terraform apply -no-color -input=false $JOB_BASE_NAME-plan'
}
}
}
def destroy(String environment, String region, String target=' ') {
def targetM = (target == ' ') ? '' : '-target='+ target
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'terraform_' + environment]]) {
container('terraform') {
install_dependencies()
sh 'cd $JOB_BASE_NAME;terraform destroy ' + targetM + ' -no-color -input=false -var "aws_region=' + region + '" -var-file=env/' + environment + '.tfvars -auto-approve'
}
}
}
def show(String environment, String region) {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'terraform_' + environment]]) {
container('terraform') {
install_dependencies()
sh 'cd $JOB_BASE_NAME; terraform init -no-color -backend-config="region=' + region + '" -backend-config=env/' + environment + '.backend'
sh 'cd $JOB_BASE_NAME; terraform show -no-color'
}
}
}