Skip to content

Commit a942650

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

File tree

2 files changed

+96
-58
lines changed

2 files changed

+96
-58
lines changed

.github/workflows/deploy.yml

Lines changed: 86 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,107 @@ 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+
echo "APP_IP=$(terraform output -raw instance_public_ip | head -n1)" >> $GITHUB_ENV
67+
echo "VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip | head -n1)" >> $GITHUB_ENV
68+
echo "S3_BUCKET=$(terraform output -raw s3_log_bucket | head -n1)" >> $GITHUB_ENV
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"
71+
echo "🔑 Verifier IP: $VERIFIER_IP"
72+
echo "🪣 S3 Bucket: $S3_BUCKET"
73+
74+
75+
# ✅ Wait for App & Logs
76+
- name: Wait for App & Logs
77+
run: |
78+
echo "Waiting 90 seconds for EC2 instances to initialize..."
79+
sleep 90
80+
81+
# ✅ Validate App Health
82+
- name: Check Application Health
83+
run: |
84+
echo "Checking app health on http://$APP_IP:80"
85+
if curl -fs http://$APP_IP:80; then
86+
echo "✅ App is running."
87+
else
88+
echo "❌ App is not responding."
89+
exit 1
90+
fi
91+
92+
# ✅ Verify Logs on Read-Only EC2
93+
- name: Verify Logs on EC2-2 (read-only)
94+
run: |
95+
echo "Connecting to verifier EC2 ($VERIFIER_IP)..."
96+
ssh -i ./ec2_key.pem -o StrictHostKeyChecking=no ubuntu@$VERIFIER_IP "
97+
if [ -s /mylogs/app/my-app.log ] && [ -s /mylogs/system/cloud-init.log ]; then
98+
echo '✅ Logs found on EC2-2.'
99+
else
100+
echo '❌ Logs missing on EC2-2.'
101+
exit 1
102+
fi
103+
"
104+
105+
# # ✅ Download Logs from EC2-2
106+
# - name: Download Logs from EC2-2
107+
# run: |
108+
# mkdir -p mylogs
109+
# scp -i ./ec2_key.pem -o StrictHostKeyChecking=no -r ubuntu@$VERIFIER_IP:/mylogs/* ./mylogs/
110+
111+
# # ✅ Upload Logs as Artifact
112+
# - name: Upload Logs as Artifact
113+
# uses: actions/upload-artifact@v4
114+
# with:
115+
# name: ec2-logs-${{ github.event.inputs.stage }}
116+
# path: mylogs/
117+
118+
119+
- name: Destroy infrastructure
120+
if: always() # You can also use `if: ${{ github.event.inputs.destroy == 'true' }}` for toggle
121+
run: |
122+
echo "🔴 Destroying all resources for stage: ${{ github.event.inputs.stage }}"
123+
cd terraform
124+
terraform destroy -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
125+
-var "stage=${{ github.event.inputs.stage }}"
126+
127+
# Optional: Delete the workspace
128+
- name: Cleanup Terraform Workspace
129+
run: |
130+
cd terraform
131+
terraform workspace select default
132+
terraform workspace delete ${{ github.event.inputs.stage }}

.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)