-
Notifications
You must be signed in to change notification settings - Fork 29
Verify previous Terraform state can be reused #1430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -28,7 +28,7 @@ def run(params) { | |||||
| } | ||||||
| try { | ||||||
| stage('Clone terracumber, susemanager-ci and sumaform') { | ||||||
| // Create a directory for to place the directory with the build results (if it does not exist) | ||||||
| // Create a directory in which to place the build results (if it does not exist) | ||||||
| sh "mkdir -p ${resultdir}" | ||||||
| git url: params.terracumber_gitrepo, branch: params.terracumber_ref | ||||||
| dir("susemanager-ci") { | ||||||
|
|
@@ -37,9 +37,50 @@ def run(params) { | |||||
| // Clone sumaform | ||||||
| sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync" | ||||||
|
|
||||||
| // Restore Terraform states from artifacts | ||||||
| // Attempt to restore Terraform states from artifacts | ||||||
| if (params.use_previous_terraform_state) { | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}") | ||||||
| def terraformDir = "${env.WORKSPACE}/sumaform/terraform" | ||||||
| def terraformTmpDir = "${terraformDir}/temp/" | ||||||
| def filters = 'results/sumaform/terraform.tfstate, results/sumaform/.terraform/**/*' | ||||||
| def terraformStatePath = "results/sumaform/terraform.tfstate" | ||||||
| def previousBuild = currentBuild.previousBuild | ||||||
| def found = false | ||||||
|
|
||||||
| // Loop through previous builds until we find one for which a terraform state was stored | ||||||
| while (previousBuild != null) { | ||||||
| found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}") | ||||||
| if (found){ | ||||||
| echo "Found previous Terraform state in build ${previousBuild.number}." | ||||||
|
|
||||||
| // Copy just the necessary files (state and Terraform config) from the previous build to a temporary directory | ||||||
| sh "mkdir -p ${terraformTmpDir}" | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"), filter: "${filters}" , target: "${terraformDir}" | ||||||
| // Copy the Terraform configuration files (like main.tf, variables.tf, etc) from the current workspace to the temp dir | ||||||
| sh "cp ${terraformDir}/*.tf ${terraformTmpDir}" | ||||||
|
|
||||||
| // Validate the restored Terraform state | ||||||
| dir(terraformTmpDir) { | ||||||
| sh "terraform init" | ||||||
| def planOutput = sh(script: "terraform plan -refresh=true", returnStatus: true) | ||||||
|
|
||||||
| if (planOutput == 0) { | ||||||
| echo "Terraform state from build ${previousBuild.number} is valid." | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" | ||||||
| break | ||||||
| } else { | ||||||
| echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build." | ||||||
| foundState = false | ||||||
|
||||||
| foundState = false | |
| found = false |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,9 +20,50 @@ def run(params) { | |||||
| // Clone sumaform | ||||||
| sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync" | ||||||
|
|
||||||
| // Restore Terraform states from artifacts | ||||||
| // Attempt to restore Terraform states from artifacts | ||||||
| if (params.use_previous_terraform_state) { | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}") | ||||||
| def terraformDir = "${env.WORKSPACE}/sumaform/terraform" | ||||||
| def terraformTmpDir = "${terraformDir}/temp/" | ||||||
| def filters = 'results/sumaform/terraform.tfstate, results/sumaform/.terraform/**/*' | ||||||
| def terraformStatePath = "results/sumaform/terraform.tfstate" | ||||||
| def previousBuild = currentBuild.previousBuild | ||||||
| def found = false | ||||||
|
|
||||||
| // Loop through previous builds until we find one for which a terraform state was stored | ||||||
| while (previousBuild != null) { | ||||||
| found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}") | ||||||
| if (found){ | ||||||
| echo "Found previous Terraform state in build ${previousBuild.number}." | ||||||
|
|
||||||
| // Copy just the necessary files (state and Terraform config) from the previous build to a temporary directory | ||||||
| sh "mkdir -p ${terraformTmpDir}" | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"), filter: "${filters}" , target: "${terraformDir}" | ||||||
| // Copy the Terraform configuration files (like main.tf, variables.tf, etc) from the current workspace to the temp dir | ||||||
| sh "cp ${terraformDir}/*.tf ${terraformTmpDir}" | ||||||
|
|
||||||
| // Validate the restored Terraform state | ||||||
| dir(terraformTmpDir) { | ||||||
| sh "terraform init" | ||||||
| def planOutput = sh(script: "terraform plan -refresh=true", returnStatus: true) | ||||||
|
|
||||||
| if (planOutput == 0) { | ||||||
| echo "Terraform state from build ${previousBuild.number} is valid." | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" | ||||||
|
||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'foundState' is undefined. This should be 'found' to match the variable declared on line 30. This will cause a runtime error as 'foundState' was never declared.
| foundState = false | |
| found = false |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,9 +20,50 @@ def run(params) { | |||||
| // Clone sumaform | ||||||
| sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync" | ||||||
|
|
||||||
| // Restore Terraform states from artifacts | ||||||
| // Attempt to restore Terraform states from artifacts | ||||||
| if (params.use_previous_terraform_state) { | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}") | ||||||
| def terraformDir = "${env.WORKSPACE}/sumaform/terraform" | ||||||
| def terraformTmpDir = "${terraformDir}/temp/" | ||||||
| def filters = 'results/sumaform/terraform.tfstate, results/sumaform/.terraform/**/*' | ||||||
| def terraformStatePath = "results/sumaform/terraform.tfstate" | ||||||
| def previousBuild = currentBuild.previousBuild | ||||||
| def found = false | ||||||
|
|
||||||
| // Loop through previous builds until we find one for which a terraform state was stored | ||||||
| while (previousBuild != null) { | ||||||
| found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}") | ||||||
| if (found){ | ||||||
| echo "Found previous Terraform state in build ${previousBuild.number}." | ||||||
|
|
||||||
| // Copy just the necessary files (state and Terraform config) from the previous build to a temporary directory | ||||||
| sh "mkdir -p ${terraformTmpDir}" | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"), filter: "${filters}" , target: "${terraformDir}" | ||||||
| // Copy the Terraform configuration files (like main.tf, variables.tf, etc) from the current workspace to the temp dir | ||||||
| sh "cp ${terraformDir}/*.tf ${terraformTmpDir}" | ||||||
|
|
||||||
| // Validate the restored Terraform state | ||||||
| dir(terraformTmpDir) { | ||||||
| sh "terraform init" | ||||||
| def planOutput = sh(script: "terraform plan -refresh=true", returnStatus: true) | ||||||
|
|
||||||
| if (planOutput == 0) { | ||||||
| echo "Terraform state from build ${previousBuild.number} is valid." | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" | ||||||
|
||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'foundState' is undefined. This should be 'found' to match the variable declared on line 30. This will cause a runtime error as 'foundState' was never declared.
| foundState = false | |
| found = false |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,9 +20,51 @@ def run(params) { | |||||
| } | ||||||
| // Clone sumaform | ||||||
| sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync" | ||||||
| // Restore Terraform states from artifacts | ||||||
|
|
||||||
| // Attempt to restore Terraform states from artifacts | ||||||
| if (params.use_previous_terraform_state) { | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}") | ||||||
| def terraformDir = "${env.WORKSPACE}/sumaform/terraform" | ||||||
| def terraformTmpDir = "${terraformDir}/temp/" | ||||||
| def filters = 'results/sumaform/terraform.tfstate, results/sumaform/.terraform/**/*' | ||||||
| def terraformStatePath = "results/sumaform/terraform.tfstate" | ||||||
| def previousBuild = currentBuild.previousBuild | ||||||
| def found = false | ||||||
|
|
||||||
| // Loop through previous builds until we find one for which a terraform state was stored | ||||||
| while (previousBuild != null) { | ||||||
| found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}") | ||||||
| if (found){ | ||||||
| echo "Found previous Terraform state in build ${previousBuild.number}." | ||||||
|
|
||||||
| // Copy just the necessary files (state and Terraform config) from the previous build to a temporary directory | ||||||
| sh "mkdir -p ${terraformTmpDir}" | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"), filter: "${filters}" , target: "${terraformDir}" | ||||||
| // Copy the Terraform configuration files (like main.tf, variables.tf, etc) from the current workspace to the temp dir | ||||||
| sh "cp ${terraformDir}/*.tf ${terraformTmpDir}" | ||||||
|
|
||||||
| // Validate the restored Terraform state | ||||||
| dir(terraformTmpDir) { | ||||||
| sh "terraform init" | ||||||
| def planOutput = sh(script: "terraform plan -refresh=true", returnStatus: true) | ||||||
|
|
||||||
| if (planOutput == 0) { | ||||||
| echo "Terraform state from build ${previousBuild.number} is valid." | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" | ||||||
|
||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'foundState' is undefined. This should be 'found' to match the variable declared on line 31. This will cause a runtime error as 'foundState' was never declared.
| foundState = false | |
| found = false |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -62,9 +62,50 @@ def run(params) { | |||||
| // Clone sumaform | ||||||
| sh "set +x; source /home/jenkins/.credentials set -x; ./terracumber-cli ${common_params} --gitrepo ${params.sumaform_gitrepo} --gitref ${params.sumaform_ref} --runstep gitsync" | ||||||
|
|
||||||
| // Restore Terraform states from artifacts | ||||||
| // Attempt to restore Terraform states from artifacts | ||||||
| if (params.use_previous_terraform_state) { | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${currentBuild.previousBuild.number}") | ||||||
| def terraformDir = "${env.WORKSPACE}/sumaform/terraform" | ||||||
| def terraformTmpDir = "${terraformDir}/temp/" | ||||||
| def filters = 'results/sumaform/terraform.tfstate, results/sumaform/.terraform/**/*' | ||||||
| def terraformStatePath = "results/sumaform/terraform.tfstate" | ||||||
| def previousBuild = currentBuild.previousBuild | ||||||
| def found = false | ||||||
|
|
||||||
| // Loop through previous builds until we find one for which a terraform state was stored | ||||||
| while (previousBuild != null) { | ||||||
| found = fileExists("${WORKSPACE}/${previousBuild.getArtifactsDir()}/${terraformStatePath}") | ||||||
| if (found){ | ||||||
| echo "Found previous Terraform state in build ${previousBuild.number}." | ||||||
|
|
||||||
| // Copy just the necessary files (state and Terraform config) from the previous build to a temporary directory | ||||||
| sh "mkdir -p ${terraformTmpDir}" | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"), filter: "${filters}" , target: "${terraformDir}" | ||||||
| // Copy the Terraform configuration files (like main.tf, variables.tf, etc) from the current workspace to the temp dir | ||||||
| sh "cp ${terraformDir}/*.tf ${terraformTmpDir}" | ||||||
|
|
||||||
| // Validate the restored Terraform state | ||||||
| dir(terraformTmpDir) { | ||||||
| sh "terraform init" | ||||||
| def planOutput = sh(script: "terraform plan -refresh=true", returnStatus: true) | ||||||
|
|
||||||
| if (planOutput == 0) { | ||||||
| echo "Terraform state from build ${previousBuild.number} is valid." | ||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" | ||||||
|
||||||
| copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}" |
Copilot
AI
Jan 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'foundState' is undefined. This should be 'found' to match the variable declared on line 72. This will cause a runtime error as 'foundState' was never declared.
| foundState = false | |
| found = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After validating the Terraform state, copyArtifacts is called again on line 68, but artifacts were already copied on line 57. This duplicate copy operation is unnecessary and inefficient. The line 68 copyArtifacts call should be removed since the artifacts have already been copied and validated.