diff --git a/infrastructure/services/Jenkinsfile b/infrastructure/services/Jenkinsfile index fc076dd..a807532 100644 --- a/infrastructure/services/Jenkinsfile +++ b/infrastructure/services/Jenkinsfile @@ -436,31 +436,31 @@ EOF string(credentialsId: 'AWS_REGION', variable: 'AWS_REGION') ]) { script { - sh ''' - echo "=== Terraform Plan ===" - - terraform plan \ - -var="project_name=${PROJECT_NAME}" \ - -var="environment=${ENVIRONMENT}" \ - -var="aws_region=$AWS_REGION" \ - -var="image_tag=${IMAGE_TAG}" \ - -out=tfplan \ - -detailed-exitcode - - PLAN_EXIT_CODE=$? - - if [ $PLAN_EXIT_CODE -eq 2 ]; then - echo "📝 Changes detected - plan saved to tfplan" - elif [ $PLAN_EXIT_CODE -eq 0 ]; then - echo "📋 No changes detected" - else - echo "❌ Plan failed" - exit 1 - fi - - echo "=== Plan Summary ===" - terraform show -no-color tfplan | grep -E "(Plan:|No changes|Error:)" || true - ''' + // Use returnStatus to capture exit code properly + def planExitCode = sh( + script: ''' + echo "=== Terraform Plan ===" + + terraform plan \ + -var="project_name=${PROJECT_NAME}" \ + -var="environment=${ENVIRONMENT}" \ + -var="aws_region=$AWS_REGION" \ + -var="image_tag=${IMAGE_TAG}" \ + -out=tfplan \ + -detailed-exitcode + ''', + returnStatus: true + ) + + // Handle exit codes + if (planExitCode == 0) { + echo "📋 No changes detected" + } else if (planExitCode == 2) { + echo "📝 Changes detected - plan saved to tfplan" + sh 'terraform show -no-color tfplan | grep -E "(Plan:|No changes|Error:)" || true' + } else { + error("❌ Terraform plan failed with exit code: ${planExitCode}") + } // Archive the plan archiveArtifacts artifacts: 'tfplan', allowEmptyArchive: true @@ -468,8 +468,7 @@ EOF } } } - } - + } stage('🚦 Deployment Approval') { when { equals expected: 'deploy', actual: params.ACTION