Skip to content

Commit 676b625

Browse files
committed
Verify previous Terraform state can be reused
1 parent c48d3d3 commit 676b625

6 files changed

Lines changed: 192 additions & 11 deletions

jenkins_pipelines/environments/common/pipeline-build-validation.groovy

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def run(params) {
2828
}
2929
try {
3030
stage('Clone terracumber, susemanager-ci and sumaform') {
31-
// Create a directory for to place the directory with the build results (if it does not exist)
31+
// Create a directory in which to place the build results (if it does not exist)
3232
sh "mkdir -p ${resultdir}"
3333
git url: params.terracumber_gitrepo, branch: params.terracumber_ref
3434
dir("susemanager-ci") {
@@ -39,7 +39,37 @@ def run(params) {
3939

4040
// Restore Terraform states from artifacts
4141
if (params.use_previous_terraform_state) {
42-
copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}")
42+
def previousBuild = currentBuild.previousBuild
43+
def terraformStatePath = "results/sumaform/terraform.tfstate"
44+
def found = false
45+
46+
// Loop through previous builds until we find one for which a terraform state was stored
47+
while (previousBuild != null) {
48+
found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}")
49+
if (found){
50+
echo "Found previous Terraform state in build ${previousBuild.number}."
51+
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}")
52+
53+
// Validate the restored Terraform state
54+
dir(terraformDir) {
55+
sh "terraform init"
56+
def planOutput = sh(script: "terraform plan", returnStatus: true)
57+
58+
if (planOutput == 0) {
59+
echo "Terraform state from build ${previousBuild.number} is valid."
60+
break
61+
} else {
62+
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
63+
foundState = false
64+
}
65+
}
66+
}
67+
previousBuild = previousBuild.previousBuild
68+
}
69+
70+
if (!found) {
71+
echo "No previous Terraform state to restore. Starting from scratch."
72+
}
4373
}
4474
}
4575

jenkins_pipelines/environments/common/pipeline-reference-new.groovy

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,39 @@ def run(params) {
2020
// Clone sumaform
2121
sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync"
2222

23-
// Restore Terraform states from artifacts
23+
// Restore Terraform states from artifacts
2424
if (params.use_previous_terraform_state) {
25-
copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}")
25+
def previousBuild = currentBuild.previousBuild
26+
def terraformStatePath = "results/sumaform/terraform.tfstate"
27+
def found = false
28+
29+
// Loop through previous builds until we find one for which a terraform state was stored
30+
while (previousBuild != null) {
31+
found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}")
32+
if (found){
33+
echo "Found previous Terraform state in build ${previousBuild.number}."
34+
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}")
35+
36+
// Validate the restored Terraform state
37+
dir(terraformDir) {
38+
sh "terraform init"
39+
def planOutput = sh(script: "terraform plan", returnStatus: true)
40+
41+
if (planOutput == 0) {
42+
echo "Terraform state from build ${previousBuild.number} is valid."
43+
break
44+
} else {
45+
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
46+
foundState = false
47+
}
48+
}
49+
}
50+
previousBuild = previousBuild.previousBuild
51+
}
52+
53+
if (!found) {
54+
echo "No previous Terraform state to restore. Starting from scratch."
55+
}
2656
}
2757
}
2858
stage('Deploy') {

jenkins_pipelines/environments/common/pipeline-reference.groovy

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,39 @@ def run(params) {
2020
// Clone sumaform
2121
sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync"
2222

23-
// Restore Terraform states from artifacts
23+
// Restore Terraform states from artifacts
2424
if (params.use_previous_terraform_state) {
25-
copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}")
25+
def previousBuild = currentBuild.previousBuild
26+
def terraformStatePath = "results/sumaform/terraform.tfstate"
27+
def found = false
28+
29+
// Loop through previous builds until we find one for which a terraform state was stored
30+
while (previousBuild != null) {
31+
found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}")
32+
if (found){
33+
echo "Found previous Terraform state in build ${previousBuild.number}."
34+
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}")
35+
36+
// Validate the restored Terraform state
37+
dir(terraformDir) {
38+
sh "terraform init"
39+
def planOutput = sh(script: "terraform plan", returnStatus: true)
40+
41+
if (planOutput == 0) {
42+
echo "Terraform state from build ${previousBuild.number} is valid."
43+
break
44+
} else {
45+
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
46+
foundState = false
47+
}
48+
}
49+
}
50+
previousBuild = previousBuild.previousBuild
51+
}
52+
53+
if (!found) {
54+
echo "No previous Terraform state to restore. Starting from scratch."
55+
}
2656
}
2757
}
2858
stage('Deploy') {

jenkins_pipelines/environments/common/pipeline-salt-shaker.groovy

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,39 @@ def run(params) {
2020
}
2121
// Clone sumaform
2222
sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync"
23-
// Restore Terraform states from artifacts
23+
// Restore Terraform states from artifacts
2424
if (params.use_previous_terraform_state) {
25-
copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}")
25+
def previousBuild = currentBuild.previousBuild
26+
def terraformStatePath = "results/sumaform/terraform.tfstate"
27+
def found = false
28+
29+
// Loop through previous builds until we find one for which a terraform state was stored
30+
while (previousBuild != null) {
31+
found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}")
32+
if (found){
33+
echo "Found previous Terraform state in build ${previousBuild.number}."
34+
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}")
35+
36+
// Validate the restored Terraform state
37+
dir(terraformDir) {
38+
sh "terraform init"
39+
def planOutput = sh(script: "terraform plan", returnStatus: true)
40+
41+
if (planOutput == 0) {
42+
echo "Terraform state from build ${previousBuild.number} is valid."
43+
break
44+
} else {
45+
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
46+
foundState = false
47+
}
48+
}
49+
}
50+
previousBuild = previousBuild.previousBuild
51+
}
52+
53+
if (!found) {
54+
echo "No previous Terraform state to restore. Starting from scratch."
55+
}
2656
}
2757
}
2858
stage('Deploy') {

jenkins_pipelines/environments/common/pipeline.groovy

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,39 @@ def run(params) {
6363
sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync"
6464

6565
// Restore Terraform states from artifacts
66+
// Restore Terraform states from artifacts
6667
if (params.use_previous_terraform_state) {
67-
copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}")
68+
def previousBuild = currentBuild.previousBuild
69+
def terraformStatePath = "results/sumaform/terraform.tfstate"
70+
def found = false
71+
72+
// Loop through previous builds until we find one for which a terraform state was stored
73+
while (previousBuild != null) {
74+
found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}")
75+
if (found){
76+
echo "Found previous Terraform state in build ${previousBuild.number}."
77+
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}")
78+
79+
// Validate the restored Terraform state
80+
dir(terraformDir) {
81+
sh "terraform init"
82+
def planOutput = sh(script: "terraform plan", returnStatus: true)
83+
84+
if (planOutput == 0) {
85+
echo "Terraform state from build ${previousBuild.number} is valid."
86+
break
87+
} else {
88+
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
89+
foundState = false
90+
}
91+
}
92+
}
93+
previousBuild = previousBuild.previousBuild
94+
}
95+
96+
if (!found) {
97+
echo "No previous Terraform state to restore. Starting from scratch."
98+
}
6899
}
69100

70101
// run minima sync on mirror

jenkins_pipelines/environments/uyuni-master-dev-acceptance-tests-code-coverage

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,39 @@ node('sumaform-cucumber-provo') {
5151
// Clone sumaform
5252
sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync"
5353

54-
// Restore Terraform states from artifacts
54+
// Restore Terraform states from artifacts
5555
if (params.use_previous_terraform_state) {
56-
copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}")
56+
def previousBuild = currentBuild.previousBuild
57+
def terraformStatePath = "results/sumaform/terraform.tfstate"
58+
def found = false
59+
60+
// Loop through previous builds until we find one for which a terraform state was stored
61+
while (previousBuild != null) {
62+
found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}")
63+
if (found){
64+
echo "Found previous Terraform state in build ${previousBuild.number}."
65+
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}")
66+
67+
// Validate the restored Terraform state
68+
dir(terraformDir) {
69+
sh "terraform init"
70+
def planOutput = sh(script: "terraform plan", returnStatus: true)
71+
72+
if (planOutput == 0) {
73+
echo "Terraform state from build ${previousBuild.number} is valid."
74+
break
75+
} else {
76+
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
77+
foundState = false
78+
}
79+
}
80+
}
81+
previousBuild = previousBuild.previousBuild
82+
}
83+
84+
if (!found) {
85+
echo "No previous Terraform state to restore. Starting from scratch."
86+
}
5787
}
5888
}
5989

0 commit comments

Comments
 (0)