Skip to content

Commit dcb3cbc

Browse files
committed
testing ssh automation
1 parent 11e8374 commit dcb3cbc

File tree

1 file changed

+61
-27
lines changed

1 file changed

+61
-27
lines changed

.github/workflows/deploy.yml

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,71 +68,105 @@ jobs:
6868
terraform init
6969
terraform workspace select ${STAGE} || terraform workspace new ${STAGE}
7070
71-
# Terraform Apply
71+
# Terraform Apply (Full Infra)
7272
- name: Terraform Apply
7373
working-directory: ${{ env.TF_WORKING_DIR }}
7474
run: |
7575
terraform apply -var-file="${STAGE}_config.tfvars" -auto-approve \
7676
-var "stage=${STAGE}"
7777
78-
# Output and inject EC2 IPs & S3 Bucket name to Github Env
79-
- name: Get EC2s Public IPs & S3 Bucket Name
78+
# Get Outputs: App IP, Verifier IP, S3 Bucket
79+
- name: Get Terraform Outputs
8080
working-directory: ${{ env.TF_WORKING_DIR }}
8181
run: |
82-
echo "Injecting terraform outputs to github environment"
8382
INSTANCE_IP=$(terraform output -raw instance_public_ip)
84-
VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
8583
S3_BUCKET=$(terraform output -raw s3_log_bucket)
86-
8784
echo "INSTANCE_IP=$INSTANCE_IP" >> $GITHUB_ENV
88-
echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
8985
echo "S3_BUCKET=$S3_BUCKET" >> $GITHUB_ENV
90-
91-
echo "📦 App IP (Shell): $INSTANCE_IP"
92-
echo "🔑 Verifier IP (Shell): $VERIFIER_IP"
93-
echo "🪣 S3 Bucket (Shell): $S3_BUCKET"
86+
echo "📦 App IP: $INSTANCE_IP"
87+
echo "🪣 S3 Bucket: $S3_BUCKET"
9488
9589
# Wait for App Initialization
9690
- name: Wait for App Initialization
9791
run: |
98-
echo "⏳ Waiting 90 seconds for EC2 instances to initialize..."
92+
echo "⏳ Waiting 90 seconds for app EC2 to initialize..."
9993
sleep 90
10094
10195
# Validate App Health
10296
- name: Validate App Health
10397
run: |
104-
echo -e "\n📦 Full Response from App:\n"
105-
curl -s http://${{ env.INSTANCE_IP }}:80 || echo "❌ Failed to get response"
106-
echo -e "\n"
107-
echo "Checking app health at http://${{ env.INSTANCE_IP }}:80"
98+
echo "🔎 Checking app health at http://${INSTANCE_IP}:80"
10899
for i in {1..10}; do
109-
STATUS=$(curl -o /dev/null -s -w "%{http_code}" http://${{ env.INSTANCE_IP }}:80)
100+
STATUS=$(curl -o /dev/null -s -w "%{http_code}" http://${INSTANCE_IP}:80)
110101
if [[ "$STATUS" == "200" ]]; then
111102
echo "✅ App is healthy (HTTP 200)"
112103
exit 0
113104
else
114-
echo "Attempt $i: got HTTP $STATUS"
105+
echo "Attempt $i: HTTP $STATUS"
115106
sleep 10
116107
fi
117108
done
118109
echo "❌ App failed health check"
119110
exit 1
120111
121-
# Verify Logs in S3
122-
- name: Verify Logs in S3
112+
# Provision Verifier EC2
113+
- name: Terraform Apply Verifier EC2
114+
working-directory: ${{ env.TF_WORKING_DIR }}
115+
run: |
116+
terraform apply -var-file="${STAGE}_config.tfvars" \
117+
-target=aws_instance.log_verifier -auto-approve \
118+
-var "stage=${STAGE}"
119+
120+
# Get Verifier IP
121+
- name: Get Verifier IP
122+
working-directory: ${{ env.TF_WORKING_DIR }}
123+
run: |
124+
VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
125+
echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
126+
echo "🔑 Verifier IP: $VERIFIER_IP"
127+
128+
# Setup SSH Key for EC2 Access
129+
- name: Setup SSH Key for EC2 Access
130+
uses: webfactory/[email protected]
131+
with:
132+
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
133+
134+
# Wait for Verifier EC2 Initialization
135+
- name: Wait for Verifier EC2 Initialization
136+
run: |
137+
echo "⏳ Waiting 60 seconds for verifier EC2 to initialize..."
138+
sleep 60
139+
140+
# SSH into Verifier EC2 and Validate Logs
141+
- name: Validate Logs from Verifier EC2
123142
run: |
124-
echo "📦 Checking for logs in S3 bucket: $S3_BUCKET"
125-
aws s3 ls s3://$S3_BUCKET/${STAGE}/system/cloud-init.log || { echo "❌ system logs missing"; exit 1; }
126-
aws s3 ls s3://$S3_BUCKET/${STAGE}/app/my-app.log || { echo "❌ app logs missing"; exit 1; }
127-
echo "✅ Logs found in S3 bucket"
143+
echo "🔐 Validating logs in S3 from verifier EC2"
128144
129-
# Destroy (automatically always to avoid manual deletion)
130-
- name: Destroy infrastructure
145+
# Retry SSH if EC2 not yet ready
146+
for attempt in {1..5}; do
147+
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} "echo '✅ SSH to verifier successful'" && break
148+
echo "⏳ Verifier not ready, retrying SSH (attempt $attempt)..."
149+
sleep 15
150+
done
151+
152+
# Validate logs in S3
153+
for log in system/cloud-init.log app/my-app.log; do
154+
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} \
155+
"if aws s3 ls s3://${S3_BUCKET}/${STAGE}/$log > /dev/null 2>&1; then
156+
echo '✅ Found: $log';
157+
else
158+
echo '❌ Missing: $log'; exit 1;
159+
fi"
160+
done
161+
162+
echo "🎉 All required logs are present in S3"
163+
164+
# Destroy Infrastructure
165+
- name: Destroy Infrastructure
131166
if: always()
132167
working-directory: ${{ env.TF_WORKING_DIR }}
133168
run: |
134169
echo "🗑️ Destroying infrastructure for stage: ${STAGE}"
135-
sleep 60
136170
terraform destroy -var-file="${STAGE}_config.tfvars" -auto-approve \
137171
-var "stage=${STAGE}"
138172

0 commit comments

Comments
 (0)