Skip to content
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

Verify previous Terraform state can be reused #1430

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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
}
}
}
previousBuild = previousBuild.previousBuild
}
// Clean up the temp directory
sh "rm -rf ${terraformTmpDir}"

if (!found) {
echo "No previous Terraform state to restore. Starting from scratch."
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
}
}
}
previousBuild = previousBuild.previousBuild
}
// Clean up the temp directory
sh "rm -rf ${terraformTmpDir}"

if (!found) {
echo "No previous Terraform state to restore. Starting from scratch."
}
}
}
stage('Deploy') {
Expand Down
45 changes: 43 additions & 2 deletions jenkins_pipelines/environments/common/pipeline-reference.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
}
}
}
previousBuild = previousBuild.previousBuild
}
// Clean up the temp directory
sh "rm -rf ${terraformTmpDir}"

if (!found) {
echo "No previous Terraform state to restore. Starting from scratch."
}
}
}
stage('Deploy') {
Expand Down
46 changes: 44 additions & 2 deletions jenkins_pipelines/environments/common/pipeline-salt-shaker.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
}
}
}
previousBuild = previousBuild.previousBuild
}
// Clean up the temp directory
sh "rm -rf ${terraformTmpDir}"

if (!found) {
echo "No previous Terraform state to restore. Starting from scratch."
}
}
}
stage('Deploy') {
Expand Down
45 changes: 43 additions & 2 deletions jenkins_pipelines/environments/common/pipeline.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
break
} else {
echo "Terraform state from build ${previousBuild.number} is invalid. Searching for another build."
foundState = false
}
}
}
previousBuild = previousBuild.previousBuild
}
// Clean up the temp directory
sh "rm -rf ${terraformTmpDir}"

if (!found) {
echo "No previous Terraform state to restore. Starting from scratch."
}
}

// run minima sync on mirror
Expand Down
Loading