Skip to content

Commit d9fd247

Browse files
committed
Implemented multi stage deployment with private repo for prod stage
1 parent 039060d commit d9fd247

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

.github/workflows/deploy.yml

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: EC2 Deploy
33
on:
44
push:
55
branches:
6-
- devops/a3
6+
- devops/a4
77
tags:
88
- deploy-dev
99
- deploy-prod
@@ -60,19 +60,30 @@ jobs:
6060
6161
echo "🛠️ Deployment stage: $STAGE"
6262
63-
# Terraform Init & Workspace
64-
- name: Terraform Init & Workspace
65-
working-directory: ${{ env.TF_WORKING_DIR }}
63+
# Clone private repo for prod config
64+
- name: Clone Private Repo for Prod Config
65+
if: env.STAGE == 'prod'
6666
run: |
67-
terraform init
68-
terraform workspace select ${STAGE} || terraform workspace new ${STAGE}
67+
echo "🔒 Cloning private repo for prod configuration..."
68+
git clone https://${{ secrets.PRIVATE_REPO_KEY }}@${{ secrets.PRIVATE_REPO }} private-config
69+
echo "✅ Cloned private config repo"
70+
71+
# Terraform Init
72+
- name: Terraform Init
73+
working-directory: ${{ env.TF_WORKING_DIR }}
74+
run: terraform init
6975

7076
# Terraform Apply (Full Infra)
7177
- name: Terraform Apply
7278
working-directory: ${{ env.TF_WORKING_DIR }}
7379
run: |
74-
terraform apply -var-file="${STAGE}_config.tfvars" -auto-approve \
75-
-var "stage=${STAGE}"
80+
if [ "${STAGE}" == "prod" ]; then
81+
echo "Applying Terraform with private prod configuration..."
82+
terraform apply -var-file=../private-config/prod_config.tfvars -auto-approve
83+
else
84+
echo "Applying Terraform with public dev configuration..."
85+
terraform apply -var-file="${STAGE}_config.tfvars" -auto-approve
86+
fi
7687
7788
# Get Outputs: App IP, Verifier IP, S3 Bucket
7889
- name: Get Terraform Outputs
@@ -117,8 +128,7 @@ jobs:
117128
working-directory: ${{ env.TF_WORKING_DIR }}
118129
run: |
119130
terraform apply -var-file="${STAGE}_config.tfvars" \
120-
-target=aws_instance.log_verifier -auto-approve \
121-
-var "stage=${STAGE}"
131+
-target=aws_instance.log_verifier -auto-approve
122132
123133
# Get Verifier IP
124134
- name: Get Verifier IP
@@ -145,14 +155,12 @@ jobs:
145155
run: |
146156
echo "🔐 Validating logs in S3 from verifier EC2"
147157
148-
# Retry SSH if EC2 not yet ready
149158
for attempt in {1..5}; do
150159
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} "echo '✅ SSH to verifier successful'" && break
151160
echo "⏳ Verifier not ready, retrying SSH (attempt $attempt)..."
152161
sleep 15
153162
done
154163
155-
# Validate logs in S3
156164
for log in system/cloud-init.log app/my-app.log; do
157165
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} \
158166
"if aws s3 ls s3://${S3_BUCKET}/${STAGE}/$log > /dev/null 2>&1; then
@@ -161,22 +169,19 @@ jobs:
161169
echo '❌ Missing: $log'; exit 1;
162170
fi"
163171
done
164-
165172
echo "🎉 All required logs are present in S3"
166173
167174
# Print Logs from Verifier EC2
168175
- name: Print Logs from Verifier EC2
169176
run: |
170177
echo "📄 Fetching logs from /mylogs/${STAGE} on verifier EC2"
171178
172-
# Retry SSH if EC2 not yet ready
173179
for attempt in {1..5}; do
174180
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} "echo '✅ SSH to verifier successful for log fetch'" && break
175181
echo "⏳ Verifier not ready for log fetch, retrying SSH (attempt $attempt)..."
176182
sleep 15
177183
done
178184
179-
# Print system log
180185
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} \
181186
"if [ -f /mylogs/${STAGE}/system/cloud-init.log ]; then
182187
echo '📄 ====== system/cloud-init.log ======'
@@ -185,7 +190,6 @@ jobs:
185190
echo '❌ system/cloud-init.log not found'
186191
fi"
187192
188-
# Print app log
189193
ssh -o StrictHostKeyChecking=no ubuntu@${VERIFIER_IP} \
190194
"if [ -f /mylogs/${STAGE}/app/my-app.log ]; then
191195
echo '📄 ====== app/my-app.log ======'
@@ -196,28 +200,14 @@ jobs:
196200
197201
echo "✅ Printed last 20 lines of logs from verifier EC2"
198202
199-
# # Verify Logs in S3 using AWS CLI
200-
# - name: Verify Logs in S3
201-
# run: |
202-
# echo "📦 Checking for logs in S3 bucket: $S3_BUCKET"
203-
# aws s3 ls s3://$S3_BUCKET/${STAGE}/system/cloud-init.log || { echo "❌ system logs missing"; exit 1; }
204-
# aws s3 ls s3://$S3_BUCKET/${STAGE}/app/my-app.log || { echo "❌ app logs missing"; exit 1; }
205-
# echo "✅ Logs found in S3 bucket"
206-
207-
208203
# Destroy Infrastructure
209204
- name: Destroy Infrastructure
210205
if: always()
211206
working-directory: ${{ env.TF_WORKING_DIR }}
212207
run: |
213208
echo "🗑️ Destroying infrastructure for stage: ${STAGE}"
214-
terraform destroy -var-file="${STAGE}_config.tfvars" -auto-approve \
215-
-var "stage=${STAGE}"
216-
217-
# Cleanup Terraform Workspace
218-
- name: Cleanup Terraform Workspace
219-
if: always()
220-
working-directory: ${{ env.TF_WORKING_DIR }}
221-
run: |
222-
terraform workspace select default
223-
terraform workspace delete ${STAGE}
209+
if [ "${STAGE}" == "prod" ]; then
210+
terraform destroy -var-file=../private-config/prod_config.tfvars -auto-approve
211+
else
212+
terraform destroy -var-file="${STAGE}_config.tfvars" -auto-approve
213+
fi

0 commit comments

Comments
 (0)