From 8febe2d80c1adbf18f542bd9fcc5f2056563837b Mon Sep 17 00:00:00 2001 From: lenape Date: Tue, 15 Jul 2025 05:49:28 +0000 Subject: [PATCH] automated terminal push --- Jenkinsfile | 67 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index f8052d6..5f2a1d5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -121,9 +121,11 @@ pipeline { returnStdout: true ).trim() + // Check force parameter first - this overrides everything if (params.FORCE_INFRASTRUCTURE_DEPLOY) { env.DEPLOYMENT_TYPE = "INFRASTRUCTURE" echo "🚨 FORCED: Infrastructure deployment requested via parameter" + echo "✅ Deployment type set to: INFRASTRUCTURE (forced)" } else if (infrastructureFiles == "initial") { env.DEPLOYMENT_TYPE = "INFRASTRUCTURE" echo "✅ First run detected. Deploying infrastructure." @@ -275,37 +277,32 @@ pipeline { withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.AWS_CRED_ID]]) { script { echo "🔍 SECURITY: Checking if infrastructure is ready for deployment..." + echo "🔍 Current deployment type: ${env.DEPLOYMENT_TYPE}" - // Check if infrastructure deployment was already forced - if (params.FORCE_INFRASTRUCTURE_DEPLOY) { - echo "🚨 FORCED: Infrastructure deployment requested via parameter" - env.DEPLOYMENT_TYPE = "INFRASTRUCTURE" - currentBuild.description = "INFRASTRUCTURE (forced) | ${env.IMAGE_TAG}" - return // Skip further checks since we're forcing deployment + // Only check readiness if not forced + if (env.DEPLOYMENT_TYPE == "APPLICATION") { + def serviceExists = sh( + script: """ + aws ecs describe-services --cluster ${TF_VAR_cluster_name} --services ${TF_VAR_cluster_name}-service --region ${AWS_REGION} 2>/dev/null | grep -q 'ACTIVE' && echo 'true' || echo 'false' + """, + returnStdout: true + ).trim() + def instanceCount = sh( + script: """ + aws ecs list-container-instances --cluster ${TF_VAR_cluster_name} --region ${AWS_REGION} --query 'length(containerInstanceArns)' --output text 2>/dev/null || echo '0' + """, + returnStdout: true + ).trim() + if (serviceExists == "false" || instanceCount == "0" || instanceCount == "null") { + echo "🚨 SECURITY NOTICE: Infrastructure not ready - forcing deployment" + echo " Service Exists: ${serviceExists}" + echo " Container Instances: ${instanceCount}" + env.DEPLOYMENT_TYPE = "INFRASTRUCTURE" + currentBuild.description = "INFRASTRUCTURE (auto-detected) | ${env.IMAGE_TAG}" + } } - def serviceExists = sh( - script: """ - aws ecs describe-services --cluster ${TF_VAR_cluster_name} --services ${TF_VAR_cluster_name}-service --region ${AWS_REGION} 2>/dev/null | grep -q 'ACTIVE' && echo 'true' || echo 'false' - """, - returnStdout: true - ).trim() - def instanceCount = sh( - script: """ - aws ecs list-container-instances --cluster ${TF_VAR_cluster_name} --region ${AWS_REGION} --query 'length(containerInstanceArns)' --output text 2>/dev/null || echo '0' - """, - returnStdout: true - ).trim() - if (serviceExists == "false" || instanceCount == "0" || instanceCount == "null") { - echo "🚨 SECURITY NOTICE: Infrastructure not ready - forcing deployment" - echo " Service Exists: ${serviceExists}" - echo " Container Instances: ${instanceCount}" - env.DEPLOYMENT_TYPE = "INFRASTRUCTURE" - currentBuild.description = "INFRASTRUCTURE (auto-detected) | ${env.IMAGE_TAG}" - } echo "📋 SECURITY: Infrastructure readiness assessment completed" - echo " ECS Service Exists: ${serviceExists}" - echo " Container Instances: ${instanceCount}" echo " Final Deployment Type: ${env.DEPLOYMENT_TYPE}" } } @@ -314,7 +311,10 @@ pipeline { stage('Deploy Infrastructure') { when { - expression { env.DEPLOYMENT_TYPE == "INFRASTRUCTURE" } + expression { + echo "🔍 Checking deployment type: ${env.DEPLOYMENT_TYPE}" + return env.DEPLOYMENT_TYPE == "INFRASTRUCTURE" + } } steps { withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: env.AWS_CRED_ID]]) { @@ -331,6 +331,17 @@ pipeline { -backend-config="key=${TF_BACKEND_PREFIX}" \\ -backend-config="region=${AWS_REGION}" \\ -backend-config="dynamodb_table=${TF_DDB_TABLE}" + + echo "🔄 Planning infrastructure changes..." + terraform plan \\ + -var="cluster_name=${TF_VAR_cluster_name}" \\ + -var="vpc_cidr=${TF_VAR_vpc_cidr}" \\ + -var="public_subnets=${TF_VAR_public_subnets}" \\ + -var="instance_type=${TF_VAR_instance_type}" \\ + -var="key_pair_name=${TF_VAR_key_pair_name}" \\ + -var="jenkins_ip_cidr=${TF_VAR_jenkins_ip_cidr}" \\ + -var="aws_region=${TF_VAR_aws_region}" + echo "🔄 Applying infrastructure changes..." terraform apply -auto-approve \\ -var="cluster_name=${TF_VAR_cluster_name}" \\