1
1
def tf_plan = " Changes"
2
-
3
2
pipeline {
4
3
agent any
5
4
options {
@@ -12,7 +11,6 @@ pipeline {
12
11
def fileContent = readFile " ${ JENKINS_HOME} /jenkins.properties"
13
12
// Split file content into lines
14
13
def lines = fileContent. readLines()
15
-
16
14
// Process each line to extract variable name and value
17
15
def variables = [:]
18
16
lines. each { line ->
@@ -21,9 +19,23 @@ pipeline {
21
19
variables[parts[0 ]. trim()] = parts[1 ]. trim()
22
20
}
23
21
}
24
-
25
22
def variableOds = variables[' outdir_structure' ]. toString(). replaceAll(" \\ [|\\ ]" , ' ' ). replaceAll(' "' , ' ' )
26
23
env. out_str = " ${ variableOds} "
24
+ def jobName = env. JOB_NAME
25
+ def parts = jobName. split(' /' )
26
+ if (env. out_str == ' Multiple_Outdir' ) {
27
+ // Assuming the job name format is <region_name>/job/<service_name>/job/job_name
28
+ env.Region = parts[1 ]
29
+ env.Service = parts[2 ]
30
+ }
31
+ else {
32
+ // Assuming the job name format is <region_name>/job/job_name
33
+ env.Region = parts[1 ]
34
+ env.Service = ' '
35
+ if (env.Region == ' global' ) {
36
+ env.Service = ' rpc'
37
+ }
38
+ }
27
39
}
28
40
}
29
41
}
@@ -38,33 +50,9 @@ pipeline {
38
50
steps {
39
51
catchError(buildResult : ' FAILURE' , stageResult : ' FAILURE' ) {
40
52
script {
41
- def jobName = env. JOB_NAME
42
- def parts = jobName. split(' /' )
43
-
44
- if (env. out_str == ' Multiple_Outdir' ) {
45
- // Assuming the job name format is <region_name>/job/<service_name>/job/job_name
46
- def regionName = parts[1 ]
47
- def serviceName = parts[2 ]
48
- // Set environment variables for reuse in subsequent stages
49
- env.Region = regionName
50
- env.Service = serviceName
51
- } else {
52
- // Assuming the job name format is <region_name>/job/job_name
53
- def regionName = parts[1 ]
54
- def serviceName = ' '
55
- if (regionName == ' global' ) {
56
- serviceName = ' rpc'
57
- }
58
- // Set environment variables for reuse in subsequent stages
59
- env.Region = regionName
60
- env.Service = serviceName
61
- }
62
-
63
53
sh " cd \" ${ WORKSPACE} /${ env.Region} /${ env.Service} \" && terraform init -upgrade"
64
54
// Run Terraform plan and capture the output
65
55
terraformPlanOutput = sh(script : " cd \" ${ WORKSPACE} /${ env.Region} /${ env.Service} \" && terraform plan -out=tfplan.out" , returnStdout : true ). trim()
66
-
67
-
68
56
// Check if the plan contains any changes
69
57
if (terraformPlanOutput. contains(' No changes.' )) {
70
58
echo ' No changes in Terraform plan. Skipping further stages.'
@@ -92,18 +80,10 @@ pipeline {
92
80
steps {
93
81
catchError(buildResult : ' FAILURE' , stageResult : ' FAILURE' ) {
94
82
script {
95
- if (env. out_str == ' Multiple_Outdir' ) {
96
- // Run Terraform show and capture the output
97
- sh " set +x && cd \" ${ WORKSPACE} /${ env.Region} /${ env.Service} \" && terraform show -json tfplan.out > tfplan.json"
98
- // Run OPA eval
99
- opaOutput = sh(script : " opa eval -f pretty -b /cd3user/oci_tools/cd3_automation_toolkit/user-scripts/OPA/ -i \" ${ WORKSPACE} /${ env.Region} /${ env.Service} /tfplan.json\" data.terraform.deny" , returnStdout : true ). trim()
100
- } else {
101
- // Run Terraform show and capture the output
102
- sh " set +x && cd \" ${ WORKSPACE} /${ env.Region} \" && terraform show -json tfplan.out > tfplan.json"
103
- // Run OPA eval
104
- opaOutput = sh(script : " opa eval -f pretty -b /cd3user/oci_tools/cd3_automation_toolkit/user-scripts/OPA/ -i \" ${ WORKSPACE} /${ env.Region} /tfplan.json\" data.terraform.deny" , returnStdout : true ). trim()
105
- }
106
-
83
+ // Run Terraform show and capture the output
84
+ sh " set +x && cd \" ${ WORKSPACE} /${ env.Region} /${ env.Service} \" && terraform show -json tfplan.out > tfplan.json"
85
+ // Run OPA eval
86
+ opaOutput = sh(script : " opa eval -f pretty -b /cd3user/oci_tools/cd3_automation_toolkit/user-scripts/OPA/ -i \" ${ WORKSPACE} /${ env.Region} /${ env.Service} /tfplan.json\" data.terraform.deny" , returnStdout : true ). trim()
107
87
if (opaOutput == ' []' ) {
108
88
echo " No OPA rules are violated. Proceeding with the next stage."
109
89
} else {
@@ -152,11 +132,8 @@ pipeline {
152
132
steps {
153
133
catchError(buildResult : ' FAILURE' , stageResult : ' FAILURE' ) {
154
134
script {
155
- if (env. out_str == ' Multiple_Outdir' ) {
156
- sh " cd \" ${ WORKSPACE} /${ env.Region} /${ env.Service} \" && terraform apply --auto-approve tfplan.out"
157
- } else {
158
- sh " cd \" ${ WORKSPACE} /${ env.Region} \" && terraform apply --auto-approve tfplan.out"
159
- }
135
+ sh " cd \" ${ WORKSPACE} /${ env.Region} /${ env.Service} \" && terraform apply --auto-approve tfplan.out"
136
+
160
137
}
161
138
}
162
139
}
@@ -174,7 +151,6 @@ pipeline {
174
151
steps {
175
152
catchError(buildResult : ' FAILURE' , stageResult : ' FAILURE' ) {
176
153
script {
177
- if (env. out_str == ' Multiple_Outdir' ) {
178
154
try {
179
155
sh '''
180
156
set +x
@@ -184,9 +160,7 @@ pipeline {
184
160
repo_name=${GIT_URL##*/}
185
161
cd ${WORKSPACE}/../${BUILD_NUMBER}/${repo_name}
186
162
git checkout main
187
- reg=`echo ${JOB_NAME}| cut -d "/" -f2`
188
- service=`echo ${JOB_NAME}| cut -d "/" -f3`
189
- copy_path=${reg}/${service}
163
+ copy_path=${env.Region}/${env.Service}
190
164
cp -r ${WORKSPACE}/${copy_path}/* ${copy_path}/
191
165
git add ${copy_path}*
192
166
'''
@@ -201,39 +175,14 @@ pipeline {
201
175
sh '''
202
176
set +x
203
177
repo_name=${GIT_URL##*/}
204
- reg=`echo ${JOB_NAME}| cut -d "/" -f2`
205
- service=`echo ${JOB_NAME}| cut -d "/" -f3`
206
178
cd ${WORKSPACE}/../${BUILD_NUMBER}/${repo_name}
207
179
git_status=`git status --porcelain`
208
180
if [[ $git_status ]]; then
209
- git commit -m "commit for terraform-apply build - ${BUILD_NUMBER} for "${reg }"/"${service }
181
+ git commit -m "commit for terraform-apply build - ${BUILD_NUMBER} for "${env.Region }"/"${env.Service }
210
182
else
211
183
echo "Nothing to commit"
212
184
fi
213
185
'''
214
- } else {
215
- try {
216
- sh '''
217
- mkdir -p ${WORKSPACE}/../${BUILD_NUMBER}
218
- cd ${WORKSPACE}/../${BUILD_NUMBER}
219
- git clone ${GIT_URL}
220
- repo_name=${GIT_URL##*/}
221
- cd ${WORKSPACE}/../${BUILD_NUMBER}/${repo_name}
222
- git checkout main
223
- reg=`echo ${JOB_NAME}| cut -d "/" -f2`
224
- copy_path=${reg}
225
- cp -r ${WORKSPACE}/${copy_path}/* ${copy_path}/
226
- git add ${copy_path}*
227
- '''
228
- } catch (Exception e1) {
229
- println (e1)
230
- sh '''
231
- cd ${WORKSPACE}/..
232
- rm -rf ${WORKSPACE}/../${BUILD_NUMBER}
233
- exit 1
234
- '''
235
- }
236
- }
237
186
}
238
187
}
239
188
}
0 commit comments