automated terminal push
This commit is contained in:
44
Jenkinsfile
vendored
44
Jenkinsfile
vendored
@@ -51,9 +51,7 @@ pipeline {
|
|||||||
TF_INPUT = 'false'
|
TF_INPUT = 'false'
|
||||||
// Ansible configuration
|
// Ansible configuration
|
||||||
ANSIBLE_HOST_KEY_CHECKING = 'False'
|
ANSIBLE_HOST_KEY_CHECKING = 'False'
|
||||||
// Fix: Use relative path without leading slash
|
|
||||||
ANSIBLE_CONFIG = './ansible/ansible.cfg'
|
ANSIBLE_CONFIG = './ansible/ansible.cfg'
|
||||||
// Fix: Define log group as variable to avoid shell interpolation issues
|
|
||||||
ECS_LOG_GROUP = "/ecs/nvhi-atsila-cluster"
|
ECS_LOG_GROUP = "/ecs/nvhi-atsila-cluster"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,9 +451,14 @@ pipeline {
|
|||||||
).trim()
|
).trim()
|
||||||
if (count != "0" && count != "null") {
|
if (count != "0" && count != "null") {
|
||||||
echo "✅ ECS agents registered: ${count} instance(s)"
|
echo "✅ ECS agents registered: ${count} instance(s)"
|
||||||
|
// Fixed: Simplified active count check to avoid backtick escaping issues
|
||||||
def activeCount = sh(
|
def activeCount = sh(
|
||||||
script: """
|
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
|
returnStdout: true
|
||||||
).trim()
|
).trim()
|
||||||
@@ -518,7 +521,7 @@ pipeline {
|
|||||||
// Create Ansible working directory and files
|
// Create Ansible working directory and files
|
||||||
sh "mkdir -p ansible/group_vars"
|
sh "mkdir -p ansible/group_vars"
|
||||||
|
|
||||||
// Fix: Create inventory with safer path handling
|
// Create dynamic inventory file
|
||||||
def inventoryContent = """[inventory_hosts]
|
def inventoryContent = """[inventory_hosts]
|
||||||
ec2-instance ansible_host=${publicIp} ansible_user=ec2-user
|
ec2-instance ansible_host=${publicIp} ansible_user=ec2-user
|
||||||
|
|
||||||
@@ -532,7 +535,7 @@ aws_region=${AWS_REGION}
|
|||||||
"""
|
"""
|
||||||
writeFile file: 'ansible/hosts', text: inventoryContent
|
writeFile file: 'ansible/hosts', text: inventoryContent
|
||||||
|
|
||||||
// Fix: Create Ansible config with safer paths
|
// Create Ansible configuration
|
||||||
def ansibleConfig = """[defaults]
|
def ansibleConfig = """[defaults]
|
||||||
inventory = hosts
|
inventory = hosts
|
||||||
host_key_checking = False
|
host_key_checking = False
|
||||||
@@ -548,7 +551,7 @@ pipelining = True
|
|||||||
"""
|
"""
|
||||||
writeFile file: 'ansible/ansible.cfg', text: ansibleConfig
|
writeFile file: 'ansible/ansible.cfg', text: ansibleConfig
|
||||||
|
|
||||||
// Fix: Create group variables with safer variable handling
|
// Create group variables
|
||||||
def groupVarsContent = """---
|
def groupVarsContent = """---
|
||||||
ecs_cluster_name: ${TF_VAR_cluster_name}
|
ecs_cluster_name: ${TF_VAR_cluster_name}
|
||||||
service_name: ${TF_VAR_cluster_name}-service
|
service_name: ${TF_VAR_cluster_name}-service
|
||||||
@@ -566,7 +569,6 @@ container_port: 8080
|
|||||||
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
|
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
|
||||||
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']
|
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']
|
||||||
]) {
|
]) {
|
||||||
// Fix: Use safer shell command construction
|
|
||||||
sh """
|
sh """
|
||||||
cd ansible
|
cd ansible
|
||||||
|
|
||||||
@@ -656,7 +658,6 @@ container_port: 8080
|
|||||||
echo "❌ DEPLOYMENT FAILED - Gathering debug information..."
|
echo "❌ DEPLOYMENT FAILED - Gathering debug information..."
|
||||||
|
|
||||||
script {
|
script {
|
||||||
// Fix: Use environment variable for log group to avoid shell interpolation issues
|
|
||||||
sh """
|
sh """
|
||||||
echo "=== ANSIBLE DEBUG INFORMATION ==="
|
echo "=== ANSIBLE DEBUG INFORMATION ==="
|
||||||
cat ansible/ansible.log 2>/dev/null || echo "No Ansible log available"
|
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"
|
--output json 2>/dev/null || echo "Could not get ECS cluster status"
|
||||||
|
|
||||||
echo "=== RECENT CONTAINER LOGS ==="
|
echo "=== RECENT CONTAINER LOGS ==="
|
||||||
# Fix: Use environment variable for log group name
|
|
||||||
LATEST_STREAM=\$(aws logs describe-log-streams \\
|
LATEST_STREAM=\$(aws logs describe-log-streams \\
|
||||||
--log-group-name "${ECS_LOG_GROUP}" \\
|
--log-group-name "${ECS_LOG_GROUP}" \\
|
||||||
--region "${AWS_REGION}" \\
|
--region "${AWS_REGION}" \\
|
||||||
@@ -761,7 +761,6 @@ container_port: 8080
|
|||||||
returnStdout: true
|
returnStdout: true
|
||||||
).trim()
|
).trim()
|
||||||
|
|
||||||
// Fix: Use safer URL construction and environment variables
|
|
||||||
sh """
|
sh """
|
||||||
echo "=== APPLICATION HEALTH CHECK ==="
|
echo "=== APPLICATION HEALTH CHECK ==="
|
||||||
curl -f -v "http://${publicIp}:8080/health"
|
curl -f -v "http://${publicIp}:8080/health"
|
||||||
@@ -794,7 +793,7 @@ container_port: 8080
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "=== LOG VALIDATION ==="
|
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 \\
|
LATEST_STREAM=\$(aws logs describe-log-streams \\
|
||||||
--log-group-name "${ECS_LOG_GROUP}" \\
|
--log-group-name "${ECS_LOG_GROUP}" \\
|
||||||
--region "${AWS_REGION}" \\
|
--region "${AWS_REGION}" \\
|
||||||
@@ -805,17 +804,26 @@ container_port: 8080
|
|||||||
--output text 2>/dev/null)
|
--output text 2>/dev/null)
|
||||||
|
|
||||||
if [ "\$LATEST_STREAM" != "None" ] && [ "\$LATEST_STREAM" != "" ]; then
|
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-group-name "${ECS_LOG_GROUP}" \\
|
||||||
--log-stream-name "\$LATEST_STREAM" \\
|
--log-stream-name "\$LATEST_STREAM" \\
|
||||||
--region "${AWS_REGION}" \\
|
--region "${AWS_REGION}" \\
|
||||||
--query 'events[?contains(message, \`ERROR\`) || contains(message, \`FATAL\`) || contains(message, \`Exception\`)].message' \\
|
--start-from-head \\
|
||||||
--output text | wc -l)
|
--query 'events[-20:].message' \\
|
||||||
|
--output text > /tmp/recent_logs.txt 2>/dev/null || echo "Could not get logs"
|
||||||
|
|
||||||
if [ "\$ERROR_COUNT" -gt 0 ]; then
|
if [ -f /tmp/recent_logs.txt ]; then
|
||||||
echo "⚠️ Found \$ERROR_COUNT potential errors in logs - please review"
|
ERROR_COUNT=\$(grep -c -i "error\\|fatal\\|exception" /tmp/recent_logs.txt 2>/dev/null || echo "0")
|
||||||
else
|
if [ "\$ERROR_COUNT" -gt 0 ]; then
|
||||||
echo "✅ No errors found in recent application logs"
|
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user