-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
156 lines (129 loc) · 4.05 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
pipeline {
agent any
parameters {
booleanParam(name: 'buildImage', defaultValue: false, description: 'Build docker image?')
booleanParam(name: 'destroy', defaultValue: false, description: 'Destroy Terraform build?')
}
environment {
dockerhub=credentials('dockerhub')
AWS_ACCESS_KEY_ID=credentials('aws_access_key_id')
AWS_SECRET_ACCESS_KEY=credentials('aws_secret_access_key')
}
stages {
stage("verify tooling") {
steps {
sh '''
docker version
docker info
terraform -help
ansible --version
'''
}
}
stage("build docker image") {
when {
equals expected: true, actual: params.buildImage
}
steps {
sh '''
docker build -t jpmontoya19/cisco_demo:flask-app .
'''
}
}
stage("push image to docker-hub") {
when {
equals expected: true, actual: params.buildImage
}
steps {
sh '''
echo $dockerhub_PSW | docker login -u $dockerhub_USR --password-stdin
docker push jpmontoya19/cisco_demo:flask-app
'''
}
}
stage("initialize terraform") {
steps {
dir('terraform') {
sh "terraform init"
}
}
}
stage("plan infrastructure") {
when {
not {
equals expected: true, actual: params.destroy
}
}
steps {
dir('terraform') {
sh '''
terraform plan
'''
}
}
}
stage("build infrastructure") {
when {
not {
equals expected: true, actual: params.destroy
}
}
steps {
dir('terraform') {
sh "terraform apply --auto-approve"
}
}
}
stage("output infrastructure") {
when {
not {
equals expected: true, actual: params.destroy
}
}
steps {
dir('terraform') {
sh "terraform output"
}
}
}
stage("Use ansible") {
when {
not {
equals expected: true, actual: params.destroy
}
}
steps {
dir('ansible') {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId:'aws_credentials',
accessKeyVariable: 'AWS_ACCESS_KEY',
secretKeyVariable: 'AWS_SECRET_KEY'
]]) {
ansiblePlaybook(
credentialsId: 'aws_ec2_key',
disableHostKeyChecking: true,
installation: 'ansible',
inventory: 'aws_ec2.yml',
playbook: 'playbook.yml',
extraVars: [
aws_access_key: [value: '${AWS_ACCESS_KEY_ID}', hidden: true],
aws_secret_key: [value: '${AWS_SECRET_ACCESS_KEY}', hidden: true]
]
)
}
}
}
}
stage("destroy infrastructure") {
when {
equals expected: true, actual: params.destroy
}
steps {
dir('terraform') {
sh "terraform destroy --auto-approve"
}
}
}
}
}