Skip to content

Commit c004445

Browse files
committed
using deploy.yml instead of deploy.sh
1 parent 62605ed commit c004445

File tree

2 files changed

+79
-58
lines changed

2 files changed

+79
-58
lines changed

.github/workflows/deploy.yml

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- devops/a3
77
tags:
8-
- 'deploy-*' # deploy-dev, deploy-qa, deploy-prod
8+
- 'deploy-*' # deploy-dev, deploy-qa, deploy-prod
99
workflow_dispatch:
1010
inputs:
1111
stage:
@@ -26,68 +26,90 @@ jobs:
2626
runs-on: ubuntu-latest
2727

2828
steps:
29-
# ✅ Set Stage
30-
- name: Set Stage
31-
id: set_stage
32-
run: |
33-
if [[ "${GITHUB_REF}" == refs/tags/deploy-* ]]; then
34-
STAGE="${GITHUB_REF#refs/tags/deploy-}"
35-
echo "📦 Tag trigger detected. Stage: $STAGE"
36-
elif [[ -n "${{ github.event.inputs.stage }}" ]]; then
37-
STAGE="${{ github.event.inputs.stage }}"
38-
echo "⚡ Manual trigger. Stage: $STAGE"
39-
else
40-
STAGE="dev"
41-
echo "🌱 Branch push. Defaulting to Stage: $STAGE"
42-
fi
43-
44-
case "$STAGE" in
45-
dev|qa|prod)
46-
echo "✅ Stage validated: $STAGE"
47-
;;
48-
*)
49-
echo "❌ Invalid stage: $STAGE. Must be dev, qa, or prod."
50-
exit 1
51-
;;
52-
esac
53-
54-
echo "STAGE=$STAGE" >> $GITHUB_ENV
55-
56-
- name: Checkout Repository
29+
# ✅ Checkout Code
30+
- name: Checkout repository
5731
uses: actions/checkout@v4
5832

33+
# ✅ Configure AWS Credentials
5934
- name: Configure AWS Credentials
6035
uses: aws-actions/configure-aws-credentials@v4
6136
with:
6237
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
6338
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
6439
aws-region: ${{ env.AWS_REGION }}
6540

66-
- name: Install Dependencies
67-
run: |
68-
sudo apt update
69-
sudo apt install -y unzip curl
70-
41+
# ✅ Install Terraform
7142
- name: Setup Terraform
7243
uses: hashicorp/setup-terraform@v2
7344
with:
7445
terraform_version: 1.6.6
7546

76-
- name: Setup SSH Private Key
47+
# ✅ Terraform Init & Workspace
48+
- name: Terraform Init and Workspace
7749
run: |
78-
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ec2_key.pem
79-
chmod 400 ec2_key.pem
50+
cd terraform
51+
terraform init
52+
terraform workspace select ${{ github.event.inputs.stage }} || terraform workspace new ${{ github.event.inputs.stage }}
8053
81-
- name: Make deploy.sh executable
82-
run: chmod +x scripts/deploy.sh
54+
# ✅ Terraform Apply (Provision EC2)
55+
- name: Apply Terraform configuration
56+
run: |
57+
cd terraform
58+
terraform apply -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
59+
-var "stage=${{ github.event.inputs.stage }}"
8360
84-
- name: Run deploy.sh
61+
# ✅ Fetch Terraform Outputs (Instance IPs, S3 Bucket)
62+
- name: Get Terraform Outputs
63+
id: tf_outputs
8564
run: |
86-
export PRIVATE_KEY_PATH="./ec2_key.pem"
87-
./scripts/deploy.sh $STAGE
65+
cd terraform
66+
APP_IP=$(terraform output -raw instance_public_ip)
67+
VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
68+
S3_BUCKET=$(terraform output -raw s3_bucket_name)
8869
89-
- name: Upload logs as artifact
90-
uses: actions/upload-artifact@v4
91-
with:
92-
name: ec2-logs-${{ env.STAGE }}
93-
path: mylogs/
70+
echo "APP_IP=$APP_IP" >> $GITHUB_ENV
71+
echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
72+
echo "S3_BUCKET=$S3_BUCKET" >> $GITHUB_ENV
73+
74+
# ✅ Wait for App & Logs
75+
- name: Wait for App & Logs
76+
run: |
77+
echo "Waiting 90 seconds for EC2 instances to initialize..."
78+
sleep 90
79+
80+
# ✅ Validate App Health
81+
- name: Check Application Health
82+
run: |
83+
echo "Checking app health on http://$APP_IP:80"
84+
if curl -fs http://$APP_IP:80; then
85+
echo "✅ App is running."
86+
else
87+
echo "❌ App is not responding."
88+
exit 1
89+
fi
90+
91+
# ✅ Verify Logs on Read-Only EC2
92+
- name: Verify Logs on EC2-2 (read-only)
93+
run: |
94+
echo "Connecting to verifier EC2 ($VERIFIER_IP)..."
95+
ssh -i ./ec2_key.pem -o StrictHostKeyChecking=no ubuntu@$VERIFIER_IP "
96+
if [ -s /mylogs/app/my-app.log ] && [ -s /mylogs/system/cloud-init.log ]; then
97+
echo '✅ Logs found on EC2-2.'
98+
else
99+
echo '❌ Logs missing on EC2-2.'
100+
exit 1
101+
fi
102+
"
103+
104+
# # ✅ Download Logs from EC2-2
105+
# - name: Download Logs from EC2-2
106+
# run: |
107+
# mkdir -p mylogs
108+
# scp -i ./ec2_key.pem -o StrictHostKeyChecking=no -r ubuntu@$VERIFIER_IP:/mylogs/* ./mylogs/
109+
110+
# # ✅ Upload Logs as Artifact
111+
# - name: Upload Logs as Artifact
112+
# uses: actions/upload-artifact@v4
113+
# with:
114+
# name: ec2-logs-${{ github.event.inputs.stage }}
115+
# path: mylogs/

.github/workflows/destroy.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Destroy EC2
1+
name: Destroy Infrastructure
22

33
on:
44
workflow_dispatch:
@@ -21,14 +21,7 @@ jobs:
2121
runs-on: ubuntu-latest
2222

2323
steps:
24-
- name: Set Stage
25-
id: set_stage
26-
run: |
27-
STAGE="${{ github.event.inputs.stage }}"
28-
echo "Stage selected: $STAGE"
29-
echo "STAGE=$STAGE" >> $GITHUB_ENV
30-
31-
- name: Checkout Repository
24+
- name: Checkout repository
3225
uses: actions/checkout@v4
3326

3427
- name: Configure AWS Credentials
@@ -43,8 +36,14 @@ jobs:
4336
with:
4437
terraform_version: 1.6.6
4538

46-
- name: Destroy Infra
39+
- name: Terraform Init and Workspace
4740
run: |
4841
cd terraform
4942
terraform init
50-
terraform destroy -var-file="${STAGE}_config.tfvars" -auto-approve
43+
terraform workspace select ${{ github.event.inputs.stage }} || terraform workspace new ${{ github.event.inputs.stage }}
44+
45+
- name: Destroy Terraform Resources
46+
run: |
47+
cd terraform
48+
terraform destroy -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
49+
-var "stage=${{ github.event.inputs.stage }}"

0 commit comments

Comments
 (0)