Skip to content

Verify previous Terraform state can be reused#1430

Draft
NamelessOne91 wants to merge 1 commit intoSUSE:masterfrom
NamelessOne91:use_previous_tf_state_improvements
Draft

Verify previous Terraform state can be reused#1430
NamelessOne91 wants to merge 1 commit intoSUSE:masterfrom
NamelessOne91:use_previous_tf_state_improvements

Conversation

@NamelessOne91
Copy link
Copy Markdown
Contributor

Related https://github.com/SUSE/spacewalk/issues/25242

Introduces additional checks when "use_previous_terraform_state"is checked, with the aim to validate a previous Terraform state is indeed present and can be safely reused.

@NamelessOne91 NamelessOne91 self-assigned this Oct 11, 2024
@srbarrios
Copy link
Copy Markdown
Member

Looks a nice improvement.
As suggestion, I would also try to use virsh to collect the actual resources status (for instance VMs and disks created), as the tfstate it might actually don't match the real state.
And if I'm guessing right, terraform plan only compares the information from tfstate and main.tf, without directly checking the real deployed resources.

@NamelessOne91 NamelessOne91 force-pushed the use_previous_tf_state_improvements branch from 676b625 to 877d38e Compare October 12, 2024 13:30
@NamelessOne91
Copy link
Copy Markdown
Contributor Author

And if I'm guessing right, terraform plan only compares the information from tfstate and main.tf, without directly checking the real deployed resources.

IIRC plan does automatically a refresh to check differences between deployed resources and what's in the state file ... but I may not RC 😄
So, it seems there's an option to force it anyway.

I added it and refactored the code a bit to be more robust.
Still needs to be tested properly, as I have a few doubts it will work and do what we expect.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds validation logic to ensure that previous Terraform state files can be safely reused before attempting to restore them. The implementation searches through previous builds to find a valid Terraform state and validates it using terraform plan before proceeding.

Changes:

  • Replaces simple artifact restoration with a validation loop that checks multiple previous builds
  • Validates Terraform state by running terraform init and terraform plan in a temporary directory
  • Falls back gracefully when no valid previous state is found

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
jenkins_pipelines/environments/uyuni-master-dev-acceptance-tests-code-coverage Adds Terraform state validation logic with build history traversal
jenkins_pipelines/environments/common/pipeline.groovy Adds identical Terraform state validation logic
jenkins_pipelines/environments/common/pipeline-salt-shaker.groovy Adds identical Terraform state validation logic
jenkins_pipelines/environments/common/pipeline-reference.groovy Adds identical Terraform state validation logic
jenkins_pipelines/environments/common/pipeline-reference-new.groovy Adds identical Terraform state validation logic
jenkins_pipelines/environments/common/pipeline-build-validation.groovy Adds identical Terraform state validation logic plus fixes a comment typo

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if (planOutput == 0) {
echo "Terraform state from build ${previousBuild.number} is valid."
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"
Copy link

Copilot AI Jan 13, 2026

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 93, but artifacts were already copied on line 82. This duplicate copy operation is unnecessary and inefficient. The line 93 copyArtifacts call should be removed since the artifacts have already been copied and validated.

Suggested change
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"

Copilot uses AI. Check for mistakes.

if (planOutput == 0) {
echo "Terraform state from build ${previousBuild.number} is valid."
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"
Copy link

Copilot AI Jan 13, 2026

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 51, but artifacts were already copied on line 40. This duplicate copy operation is unnecessary and inefficient. The line 51 copyArtifacts call should be removed since the artifacts have already been copied and validated.

Suggested change
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"

Copilot uses AI. Check for mistakes.
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
Copy link

Copilot AI Jan 13, 2026

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.

Suggested change
foundState = false
found = false

Copilot uses AI. Check for mistakes.
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
Copy link

Copilot AI Jan 13, 2026

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.

Suggested change
foundState = false
found = false

Copilot uses AI. Check for mistakes.
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
Copy link

Copilot AI Jan 13, 2026

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.

Suggested change
foundState = false
found = false

Copilot uses AI. Check for mistakes.

if (planOutput == 0) {
echo "Terraform state from build ${previousBuild.number} is valid."
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"
Copy link

Copilot AI Jan 13, 2026

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 51, but artifacts were already copied on line 40. This duplicate copy operation is unnecessary and inefficient. The line 51 copyArtifacts call should be removed since the artifacts have already been copied and validated.

Suggested change
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"

Copilot uses AI. Check for mistakes.
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
Copy link

Copilot AI Jan 13, 2026

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 61. This will cause a runtime error as 'foundState' was never declared.

Suggested change
foundState = false
found = false

Copilot uses AI. Check for mistakes.

if (planOutput == 0) {
echo "Terraform state from build ${previousBuild.number} is valid."
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"
Copy link

Copilot AI Jan 13, 2026

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 82, but artifacts were already copied on line 71. This duplicate copy operation is unnecessary and inefficient. The line 82 copyArtifacts call should be removed since the artifacts have already been copied and validated.

Suggested change
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"

Copilot uses AI. Check for mistakes.
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
Copy link

Copilot AI Jan 13, 2026

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 47. This will cause a runtime error as 'foundState' was never declared.

Suggested change
foundState = false
found = false

Copilot uses AI. Check for mistakes.

if (planOutput == 0) {
echo "Terraform state from build ${previousBuild.number} is valid."
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"
Copy link

Copilot AI Jan 13, 2026

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.

Suggested change
copyArtifacts projectName: currentBuild.projectName, selector: specific("${previousBuild.number}"

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants