From abdf2e1156f74db36b6797555e79b6875a3b14ac Mon Sep 17 00:00:00 2001 From: lenape Date: Wed, 16 Jul 2025 01:21:07 +0000 Subject: [PATCH] automated terminal push --- Jenkinsfile | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index b977878..f290f6b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -51,9 +51,7 @@ pipeline { TF_INPUT = 'false' // Ansible configuration ANSIBLE_HOST_KEY_CHECKING = 'False' - // Fix: Use relative path without leading slash ANSIBLE_CONFIG = './ansible/ansible.cfg' - // Fix: Define log group as variable to avoid shell interpolation issues ECS_LOG_GROUP = "/ecs/nvhi-atsila-cluster" } @@ -453,9 +451,14 @@ pipeline { ).trim() if (count != "0" && count != "null") { echo "✅ ECS agents registered: ${count} instance(s)" + // Fixed: Simplified active count check to avoid backtick escaping issues def activeCount = sh( script: """ - aws ecs describe-container-instances --cluster ${TF_VAR_cluster_name} --container-instances \$(aws ecs list-container-instances --cluster ${TF_VAR_cluster_name} --region ${AWS_REGION} --query 'containerInstanceArns[*]' --output text) --region ${AWS_REGION} --query 'length(containerInstances[?status==\\`ACTIVE\\`])' --output text 2>/dev/null || echo '0' + aws ecs describe-container-instances \\ + --cluster ${TF_VAR_cluster_name} \\ + --container-instances \$(aws ecs list-container-instances --cluster ${TF_VAR_cluster_name} --region ${AWS_REGION} --query 'containerInstanceArns[*]' --output text) \\ + --region ${AWS_REGION} \\ + --output text | grep -c ACTIVE || echo '0' """, returnStdout: true ).trim() @@ -518,7 +521,7 @@ pipeline { // Create Ansible working directory and files sh "mkdir -p ansible/group_vars" - // Fix: Create inventory with safer path handling + // Create dynamic inventory file def inventoryContent = """[inventory_hosts] ec2-instance ansible_host=${publicIp} ansible_user=ec2-user @@ -532,7 +535,7 @@ aws_region=${AWS_REGION} """ writeFile file: 'ansible/hosts', text: inventoryContent - // Fix: Create Ansible config with safer paths + // Create Ansible configuration def ansibleConfig = """[defaults] inventory = hosts host_key_checking = False @@ -548,7 +551,7 @@ pipelining = True """ writeFile file: 'ansible/ansible.cfg', text: ansibleConfig - // Fix: Create group variables with safer variable handling + // Create group variables def groupVarsContent = """--- ecs_cluster_name: ${TF_VAR_cluster_name} service_name: ${TF_VAR_cluster_name}-service @@ -566,7 +569,6 @@ container_port: 8080 accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'] ]) { - // Fix: Use safer shell command construction sh """ cd ansible @@ -656,7 +658,6 @@ container_port: 8080 echo "❌ DEPLOYMENT FAILED - Gathering debug information..." script { - // Fix: Use environment variable for log group to avoid shell interpolation issues sh """ echo "=== ANSIBLE DEBUG INFORMATION ===" cat ansible/ansible.log 2>/dev/null || echo "No Ansible log available" @@ -677,7 +678,6 @@ container_port: 8080 --output json 2>/dev/null || echo "Could not get ECS cluster status" echo "=== RECENT CONTAINER LOGS ===" - # Fix: Use environment variable for log group name LATEST_STREAM=\$(aws logs describe-log-streams \\ --log-group-name "${ECS_LOG_GROUP}" \\ --region "${AWS_REGION}" \\ @@ -761,7 +761,6 @@ container_port: 8080 returnStdout: true ).trim() - // Fix: Use safer URL construction and environment variables sh """ echo "=== APPLICATION HEALTH CHECK ===" curl -f -v "http://${publicIp}:8080/health" @@ -794,7 +793,7 @@ container_port: 8080 fi echo "=== LOG VALIDATION ===" - # Check for any errors in recent logs + # Fixed: Simplified log analysis to avoid complex escaping LATEST_STREAM=\$(aws logs describe-log-streams \\ --log-group-name "${ECS_LOG_GROUP}" \\ --region "${AWS_REGION}" \\ @@ -805,17 +804,26 @@ container_port: 8080 --output text 2>/dev/null) if [ "\$LATEST_STREAM" != "None" ] && [ "\$LATEST_STREAM" != "" ]; then - ERROR_COUNT=\$(aws logs get-log-events \\ + echo "Checking logs for errors in stream: \$LATEST_STREAM" + # Simple approach: get recent log messages and check for errors with grep + aws logs get-log-events \\ --log-group-name "${ECS_LOG_GROUP}" \\ --log-stream-name "\$LATEST_STREAM" \\ --region "${AWS_REGION}" \\ - --query 'events[?contains(message, \`ERROR\`) || contains(message, \`FATAL\`) || contains(message, \`Exception\`)].message' \\ - --output text | wc -l) + --start-from-head \\ + --query 'events[-20:].message' \\ + --output text > /tmp/recent_logs.txt 2>/dev/null || echo "Could not get logs" - if [ "\$ERROR_COUNT" -gt 0 ]; then - echo "⚠️ Found \$ERROR_COUNT potential errors in logs - please review" - else - echo "✅ No errors found in recent application logs" + if [ -f /tmp/recent_logs.txt ]; then + ERROR_COUNT=\$(grep -c -i "error\\|fatal\\|exception" /tmp/recent_logs.txt 2>/dev/null || echo "0") + if [ "\$ERROR_COUNT" -gt 0 ]; then + echo "⚠️ Found \$ERROR_COUNT potential errors in logs - please review" + echo "Recent error lines:" + grep -i "error\\|fatal\\|exception" /tmp/recent_logs.txt | head -5 || true + else + echo "✅ No errors found in recent application logs" + fi + rm -f /tmp/recent_logs.txt fi fi