Skip to content

Commit dc44091

Browse files
committed
Automation Toolkit Release v2024.2.2
1 parent 3953af5 commit dc44091

File tree

2 files changed

+42
-150
lines changed

2 files changed

+42
-150
lines changed

jenkins_install/tf-apply.groovy

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
def tf_plan = "Changes"
2-
32
pipeline {
43
agent any
54
options {
@@ -12,7 +11,6 @@ pipeline {
1211
def fileContent = readFile "${JENKINS_HOME}/jenkins.properties"
1312
// Split file content into lines
1413
def lines = fileContent.readLines()
15-
1614
// Process each line to extract variable name and value
1715
def variables = [:]
1816
lines.each { line ->
@@ -21,9 +19,23 @@ pipeline {
2119
variables[parts[0].trim()] = parts[1].trim()
2220
}
2321
}
24-
2522
def variableOds = variables['outdir_structure'].toString().replaceAll("\\[|\\]", '').replaceAll('"', '')
2623
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+
}
2739
}
2840
}
2941
}
@@ -38,33 +50,9 @@ pipeline {
3850
steps {
3951
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
4052
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-
6353
sh "cd \"${WORKSPACE}/${env.Region}/${env.Service}\" && terraform init -upgrade"
6454
// Run Terraform plan and capture the output
6555
terraformPlanOutput = sh(script: "cd \"${WORKSPACE}/${env.Region}/${env.Service}\" && terraform plan -out=tfplan.out", returnStdout: true).trim()
66-
67-
6856
// Check if the plan contains any changes
6957
if (terraformPlanOutput.contains('No changes.')) {
7058
echo 'No changes in Terraform plan. Skipping further stages.'
@@ -92,18 +80,10 @@ pipeline {
9280
steps {
9381
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
9482
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()
10787
if (opaOutput == '[]') {
10888
echo "No OPA rules are violated. Proceeding with the next stage."
10989
} else {
@@ -152,11 +132,8 @@ pipeline {
152132
steps {
153133
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
154134
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+
160137
}
161138
}
162139
}
@@ -174,7 +151,6 @@ pipeline {
174151
steps {
175152
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
176153
script {
177-
if (env.out_str == 'Multiple_Outdir') {
178154
try {
179155
sh '''
180156
set +x
@@ -184,9 +160,7 @@ pipeline {
184160
repo_name=${GIT_URL##*/}
185161
cd ${WORKSPACE}/../${BUILD_NUMBER}/${repo_name}
186162
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}
190164
cp -r ${WORKSPACE}/${copy_path}/* ${copy_path}/
191165
git add ${copy_path}*
192166
'''
@@ -201,39 +175,14 @@ pipeline {
201175
sh '''
202176
set +x
203177
repo_name=${GIT_URL##*/}
204-
reg=`echo ${JOB_NAME}| cut -d "/" -f2`
205-
service=`echo ${JOB_NAME}| cut -d "/" -f3`
206178
cd ${WORKSPACE}/../${BUILD_NUMBER}/${repo_name}
207179
git_status=`git status --porcelain`
208180
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}
210182
else
211183
echo "Nothing to commit"
212184
fi
213185
'''
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-
}
237186
}
238187
}
239188
}

jenkins_install/tf-destroy.groovy

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ pipeline {
2525

2626
def variableOds = variables['outdir_structure'].toString().replaceAll("\\[|\\]", '').replaceAll('"', '')
2727
env.out_str = "${variableOds}"
28+
def jobName = env.JOB_NAME
29+
def parts = jobName.split('/')
30+
if (env.out_str == 'Multiple_Outdir') {
31+
// Assuming the job name format is <region_name>/job/<service_name>/job/job_name
32+
env.Region = parts[1]
33+
env.Service = parts[2]
34+
}
35+
else {
36+
// Assuming the job name format is <region_name>/job/job_name
37+
env.Region = parts[1]
38+
env.Service = ''
39+
if (env.Region == 'global') {
40+
env.Service = 'rpc'
41+
}
42+
}
2843
}
2944
}
3045
}
@@ -37,33 +52,10 @@ pipeline {
3752
steps {
3853
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
3954
script {
40-
def jobName = env.JOB_NAME
41-
def parts = jobName.split('/')
42-
43-
if (env.out_str == 'Multiple_Outdir') {
44-
// Assuming the job name format is <region_name>/job/<service_name>/job/job_name
45-
def regionName = parts[1]
46-
def serviceName = parts[2]
47-
48-
// Set environment variables for reuse in subsequent stages
49-
env.Region = regionName
50-
env.Service = serviceName
51-
52-
} else {
53-
// Assuming job name format is <region_name>/job/job_name
54-
def regionName = parts[1]
55-
def serviceName = ''
56-
if (regionName == 'global') {
57-
serviceName = 'rpc'
58-
}
59-
// Set environment variables for reuse
60-
env.Region = regionName
61-
env.Service = serviceName
6255

63-
}
64-
sh "cd \"${WORKSPACE}/${env.Region}\" && terraform init -upgrade"
56+
sh "cd \"${WORKSPACE}/${env.Region}/${env.Service}\" && terraform init -upgrade"
6557
// Run Terraform plan
66-
terraformPlanOutput = sh(script: "cd \"${WORKSPACE}/${env.Region}\" && terraform plan -destroy", returnStdout: true).trim()
58+
terraformPlanOutput = sh(script: "cd \"${WORKSPACE}/${env.Region}/${env.Service}\" && terraform plan -destroy", returnStdout: true).trim()
6759

6860
// Check if the plan contains any changes
6961
if (terraformPlanOutput.contains('No changes.')) {
@@ -107,12 +99,8 @@ pipeline {
10799
steps {
108100
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
109101
script {
110-
if (env.out_str == 'Multiple_Outdir') {
111-
sh "cd \"${WORKSPACE}/${env.Region}/${env.Service}\" && terraform destroy --auto-approve"
112-
} else {
113-
sh "cd \"${WORKSPACE}/${env.Region}\" && terraform destroy --auto-approve"
114-
}
115-
}
102+
sh "cd \"${WORKSPACE}/${env.Region}/${env.Service}\" && terraform destroy --auto-approve"
103+
}
116104
}
117105
}
118106
}
@@ -129,18 +117,15 @@ pipeline {
129117
steps {
130118
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
131119
script {
132-
if (env.out_str == 'Multiple_Outdir') {
133120
def buildDir = "${WORKSPACE}/${BUILD_NUMBER}"
134121
// Create directory with build number
135122
sh "mkdir -p ${buildDir}"
136-
137123
// Commit changes to the main branch
138124
dir(buildDir) {
139125
sh """
140126
git clone ${GIT_URL}
141127
cd \$(ls -d */|head -n 1)
142128
git checkout main
143-
ls -lrtha
144129
cd "${env.Region}/${env.Service}"
145130
git pull --no-edit origin main
146131
rm -f *.tfvars
@@ -170,48 +155,6 @@ pipeline {
170155
}
171156
}
172157
}
173-
} else {
174-
def buildDir = "${WORKSPACE}/${BUILD_NUMBER}"
175-
// Create a directory with the build number
176-
sh "mkdir -p ${buildDir}"
177-
178-
// Commit the changes to the main branch
179-
dir(buildDir) {
180-
sh """
181-
git clone ${GIT_URL}
182-
cd \$(ls -d */|head -n 1)
183-
git checkout main
184-
cd "${env.Region}"
185-
git pull --no-edit origin main
186-
rm -f *.tfvars
187-
git rm *.tfvars
188-
git status
189-
git add --all .
190-
"""
191-
192-
def git_status = false
193-
while (!git_status) {
194-
// Execute the git commands using shell
195-
def gitResult = sh(script: """
196-
cd "\$(ls -d */|head -n 1)"
197-
cd "${env.Region}"
198-
ls -lrtha
199-
git fetch origin main
200-
git merge origin/main
201-
git commit -m "commit for terraform-destroy build - ${BUILD_NUMBER} for "${env.Region}
202-
203-
git push --porcelain origin main
204-
""", returnStatus: true)
205-
206-
if (gitResult == 0) {
207-
git_status = true
208-
} else {
209-
echo "Git operation failed, retrying...."
210-
sleep 3 // 3 seconds before retrying
211-
}
212-
}
213-
}
214-
}
215158
}
216159
}
217160
}

0 commit comments

Comments
 (0)