forked from lkravi/kube8aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
178 lines (175 loc) · 7.52 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
pipeline {
agent any
options {
timestamps()
ansiColor('xterm')
}
environment {
// S3_BUCKET_NAME = '<bucket-name>'
// TF_VAR_AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
// TF_VAR_AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
AWS_ACCESS_KEY_ID = credentials('jenkins_aws_access_key_id')
AWS_SECRET_ACCESS_KEY = credentials('jenkins_aws_secret_access_key')
}
// triggers {
// pollSCM '*/5 * * * *'
/* }
options {
skipDefaultCheckout(true)
}
stages{
stage('clean workspace') {
steps {
cleanWs()
}
}
stage('checkout') {
steps {
checkout scm
}
}
*/
stages {
stage('create s3 bucket') {
agent {
docker {
image 'amazon/aws-cli:2.7.29'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '--entrypoint='
}
}
steps {
/* withAWS(credentials: 'cloud_playgroud_aws_cred', region: 'us-east-1') {
sh 'aws s3 mb s3://$S3_BUCKET_NAME --region us-east-1'
} */
sh 'aws s3 mb s3://$S3_BUCKET_NAME --region us-east-1'
}
}
stage('initialize and validate') {
agent {
docker {
image 'hashicorp/terraform:latest'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '--entrypoint='
}
}
steps {
/*withAWS(credentials: 'cloud_playgroud_aws_cred', region: 'us-east-1') {
sh 'terraform init -input=false -backend-config="access_key=$TF_VAR_AWS_ACCESS_KEY_ID" -backend-config="secret_key=$TF_VAR_AWS_SECRET_ACCESS_KEY"'
sh 'terraform validate'
}
sh """#!/bin/bash
cat providers.tf | grep S3_BUCKET_NAME
sed -i "s@S3_BUCKET_NAME@$S3_BUCKET_NAME@g" providers.tf
cat providers.tf | grep S3_BUCKET_NAME
""" */
sh 'terraform init -input=false -backend-config="access_key=$AWS_ACCESS_KEY_ID" -backend-config="secret_key=$AWS_SECRET_ACCESS_KEY"'
sh 'terraform validate'
}
}
stage('plan') {
agent {
docker {
image 'hashicorp/terraform:latest'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '--entrypoint='
}
}
steps {
/* withAWS(credentials: 'cloud_playgroud_aws_cred', region: 'us-east-1') {
sh 'terraform init -input=false -backend-config="access_key=$TF_VAR_AWS_ACCESS_KEY_ID" -backend-config="secret_key=$TF_VAR_AWS_SECRET_ACCESS_KEY"'
sh 'terraform plan -out terraform.plan -input=false'
archiveArtifacts artifacts: 'terraform.plan', fingerprint: true
}*/
sh 'terraform init -input=false -backend-config="access_key=$AWS_ACCESS_KEY_ID" -backend-config="secret_key=$AWS_SECRET_ACCESS_KEY"'
sh 'terraform plan -out terraform.plan -input=false -no-color'
archiveArtifacts artifacts: 'terraform.plan', fingerprint: true
}
}
stage('apply') {
when {
branch 'main'
}
agent {
docker {
image 'hashicorp/terraform:latest'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '--entrypoint='
}
}
steps {
/*withAWS(credentials: 'cloud_playgroud_aws_cred', region: 'us-east-1') {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
input 'Deploy to Production'
sh 'terraform init -input=false -backend-config="access_key=$TF_VAR_AWS_ACCESS_KEY_ID" -backend-config="secret_key=$TF_VAR_AWS_SECRET_ACCESS_KEY"'
sh 'terraform apply -input=false terraform.plan'
}
}*/
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
input 'Deploy to Production'
sh 'terraform init -input=false -backend-config="access_key=$AWS_ACCESS_KEY_ID" -backend-config="secret_key=$AWS_SECRET_ACCESS_KEY"'
sh 'terraform apply -input=false terraform.plan -no-color'
}
}
}
stage('destroy') {
agent {
docker {
image 'hashicorp/terraform:latest'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '--entrypoint='
}
}
steps {
/* withAWS(credentials: 'cloud_playgroud_aws_cred', region: 'us-east-1') {
input 'Destroy!!!'
sh 'terraform init -input=false -backend-config="access_key=$TF_VAR_AWS_ACCESS_KEY_ID" -backend-config="secret_key=$TF_VAR_AWS_SECRET_ACCESS_KEY"'
sh 'terraform destroy --auto-approve'
}*/
input 'Destroy!!!'
sh 'terraform init -input=false -backend-config="access_key=$AWS_ACCESS_KEY_ID" -backend-config="secret_key=$AWS_SECRET_ACCESS_KEY"'
sh 'terraform destroy --auto-approve -no-color'
}
}
stage('delete s3 bucket') {
agent {
docker {
image 'amazon/aws-cli:2.7.29'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
reuseNode true
args '--entrypoint='
}
}
steps {
/* withAWS(credentials: 'cloud_playgroud_aws_cred', region: 'us-east-1') {
input 'Delete S3 Bucket!!!'
sh 'aws s3 rb s3://$S3_BUCKET_NAME --force --region us-east-1'
}*/
input 'Delete S3 Bucket!!!'
sh 'aws s3 rb s3://$S3_BUCKET_NAME --force --region us-east-1'
}
}
}
post {
always {
cleanWs()
}
}
}