Skip to content

Commit 827136a

Browse files
committed
Using github workflows to automate
1 parent 4138e4a commit 827136a

File tree

3 files changed

+136
-71
lines changed

3 files changed

+136
-71
lines changed

.github/workflows/deploy.yml

Lines changed: 118 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
name: Deploy to EC2
1+
name: EC2 Deploy
22

33
on:
44
push:
55
branches:
66
- devops/a3
77
tags:
8-
- 'deploy-*' # Matches tags like deploy-dev, deploy-qa, deploy-prod
8+
- deploy-dev
9+
- deploy-qa
10+
- deploy-prod
11+
912
workflow_dispatch:
1013
inputs:
1114
stage:
12-
description: 'Deployment stage (dev, qa, prod)'
15+
description: "Select stage to deploy"
1316
required: true
14-
default: 'dev'
17+
default: dev
1518
type: choice
1619
options:
1720
- dev
@@ -20,67 +23,135 @@ on:
2023

2124
env:
2225
AWS_REGION: ap-south-1
26+
TF_WORKING_DIR: ./terraform
2327

2428
jobs:
2529
deploy:
2630
runs-on: ubuntu-latest
2731

2832
steps:
29-
- name: Determine Stage
30-
id: set_stage
31-
run: |
32-
STAGE_INPUT="${{ github.event.inputs.stage }}"
33-
STAGE=""
34-
35-
if [[ "${GITHUB_REF}" == refs/tags/deploy-* ]]; then
36-
STAGE="${GITHUB_REF#refs/tags/deploy-}"
37-
echo "Tag trigger detected. Stage set to: $STAGE"
38-
elif [[ -n "$STAGE_INPUT" ]]; then
39-
STAGE="$STAGE_INPUT"
40-
echo "Manual trigger detected. Stage set to: $STAGE"
41-
else
42-
echo "Branch trigger detected (main). Defaulting stage to dev."
43-
STAGE="dev"
44-
fi
45-
46-
# Validate stage
47-
if [[ "$STAGE" != "dev" && "$STAGE" != "qa" && "$STAGE" != "prod" ]]; then
48-
echo "Invalid stage: $STAGE. Must be dev, qa, or prod."
49-
exit 1
50-
fi
51-
52-
echo "STAGE=$STAGE" >> $GITHUB_ENV
53-
54-
# Checkout Code
33+
# Checkout Repository
5534
- name: Checkout repository
5635
uses: actions/checkout@v4
5736

5837
# Configure AWS Credentials
59-
- name: Configure AWS Credentials
60-
uses: aws-actions/configure-aws-credentials@v4
38+
- name: Configure AWS credentials
39+
uses: aws-actions/configure-aws-credentials@v2
6140
with:
6241
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
6342
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
6443
aws-region: ${{ env.AWS_REGION }}
44+
aws-output: json
45+
46+
# Install Terraform
47+
- name: Setup Terraform
48+
uses: hashicorp/setup-terraform@v3
49+
50+
# Determin Stage - dev/prod/qa defaults to dev
51+
# - name: Determine Stage
52+
# id: set_stage
53+
# run: |
54+
# if [[ "${GITHUB_REF}" == "refs/tags/deploy-dev" ]]; then
55+
# echo "STAGE=dev" >> $GITHUB_ENV
56+
# elif [[ "${GITHUB_REF}" == "refs/tags/deploy-qa" ]]; then
57+
# echo "STAGE=qa" >> $GITHUB_ENV
58+
# elif [[ "${GITHUB_REF}" == "refs/tags/deploy-prod" ]]; then
59+
# echo "STAGE=prod" >> $GITHUB_ENV
60+
# elif [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
61+
# echo "STAGE=${{ github.event.inputs.stage }}" >> $GITHUB_ENV
62+
# else
63+
# echo "STAGE=dev" >> $GITHUB_ENV # default fallback
64+
# fi
65+
66+
# echo "🛠️ Deployment stage: $STAGE"
67+
68+
# Terraform Init & Workspace
69+
- name: Terraform Init & Workspace
70+
working-directory: ${{ env.TF_WORKING_DIR }}
71+
run: |
72+
terraform init
73+
terraform workspace select ${{ github.event.inputs.stage }} || terraform workspace new ${{ github.event.inputs.stage }}
74+
75+
# Terraform Apply
76+
- name: Terraform Apply
77+
working-directory: ${{ env.TF_WORKING_DIR }}
78+
run: |
79+
terraform apply -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
80+
-var "stage=${{ github.event.inputs.stage }}"
6581
66-
# Install Dependencies
67-
- name: Install dependencies
82+
# Output and inject EC2 IPs & S3 Bucket name to Github Env
83+
- name: Get EC2s Public IPs & S3 Bucket Name
84+
working-directory: ${{ env.TF_WORKING_DIR }}
6885
run: |
69-
sudo apt update
70-
sudo apt install -y unzip curl terraform
86+
echo "Injecting terraform outputs to github environment"
87+
# echo "INSTANCE_IP=$(terraform output -raw instance_public_ip)" >> $GITHUB_ENV
88+
# echo "VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)" >> $GITHUB_ENV
89+
# echo "S3_BUCKET=$(terraform output -raw s3_log_bucket)" >> $GITHUB_ENV
90+
# Assign to local shell variables
91+
INSTANCE_IP=$(terraform output -raw instance_public_ip)
92+
VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
93+
S3_BUCKET=$(terraform output -raw s3_log_bucket)
7194
72-
# Setup SSH Private Key
73-
- name: Setup SSH Private Key
95+
# Write to GitHub Actions ENV for later steps
96+
echo "INSTANCE_IP=$INSTANCE_IP" >> $GITHUB_ENV
97+
echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
98+
echo "S3_BUCKET=$S3_BUCKET" >> $GITHUB_ENV
99+
100+
# Echo both for current step & confirmation
101+
echo "📦 App IP (Shell): $INSTANCE_IP"
102+
echo "🔑 Verifier IP (Shell): $VERIFIER_IP"
103+
echo "🪣 S3 Bucket (Shell): $S3_BUCKET"
104+
105+
106+
# Wait for App Initialization
107+
- name: Wait for App Initialization
108+
run: |
109+
echo "⏳ Waiting 90 seconds for EC2 instances to initialize..."
110+
sleep 90
111+
112+
# Validate App Health
113+
- name: Validate App Health
74114
run: |
75-
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ec2_key.pem
76-
chmod 400 ec2_key.pem
115+
echo "Checking app health at http://${{ env.INSTANCE_IP }}:80"
116+
for i in {1..10}; do
117+
STATUS=$(curl -o /dev/null -s -w "%{http_code}" http://$INSTANCE_IP:80)
118+
if [[ "$STATUS" == "200" ]]; then
119+
echo "✅ App is healthy (HTTP 200)"
120+
exit 0
121+
else
122+
echo "Attempt $i: got HTTP $STATUS"
123+
sleep 10
124+
fi
125+
done
126+
echo "❌ App failed health check"
127+
exit 1
77128
78-
# Make deploy.sh executable
79-
- name: Make deploy.sh executable
80-
run: chmod +x scripts/deploy.sh
129+
echo -e "\n📦 Full Response from App:\n"
130+
curl -s http://${{ env.INSTANCE_IP }}:80 || echo "❌ Failed to get response"
131+
echo -e "\n"
132+
133+
# Verify Logs in S3
134+
- name: Verify Logs in S3
135+
run: |
136+
echo "📦 Checking for logs in S3 bucket: $S3_BUCKET"
137+
aws s3 ls s3://$S3_BUCKET/system/cloud-init.log || { echo "❌ system logs missing"; exit 1; }
138+
aws s3 ls s3://$S3_BUCKET/app/my-app.log || { echo "❌ app logs missing"; exit 1; }
139+
echo "✅ Logs found in S3 bucket"
140+
141+
# Destroy (disabled by default)
142+
- name: Destroy infrastructure
143+
if: always()
144+
working-directory: ${{ env.TF_WORKING_DIR }}
145+
run: |
146+
echo "🗑️ Destroying infrastructure for stage: ${{ github.event.inputs.stage }}"
147+
sleep 60
148+
terraform destroy -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
149+
-var "stage=${{ github.event.inputs.stage }}"
81150
82-
# Run deploy.sh with detected stage
83-
- name: Run deploy.sh
151+
# Cleanup Terraform Workspace
152+
- name: Cleanup Terraform Workspace
153+
if: always()
154+
working-directory: ${{ env.TF_WORKING_DIR }}
84155
run: |
85-
export PRIVATE_KEY_PATH="./ec2_key.pem"
86-
./scripts/deploy.sh $STAGE
156+
terraform workspace select default
157+
terraform workspace delete ${{ github.event.inputs.stage }}

scripts/deploy.sh

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ terraform init
2424
echo "[+] Applying configuration for environment: $ENV"
2525
terraform apply -var-file="$CONFIG_FILE" -auto-approve
2626

27-
echo "[+] Waiting 30 seconds for app to deploy in ec2 instance"
28-
sleep 30
27+
echo "[+] Waiting 200 seconds for app to deploy in ec2 instance"
28+
sleep 200
2929

3030
# Get the public IP from Terraform output
3131
RAW_INSTANCE_IP=$(terraform output -raw instance_public_ip)
@@ -49,20 +49,14 @@ VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
4949
echo "Verified Public IP: $VERIFIER_IP"
5050

5151

52-
#To verify and pull logs from ec2 to local.
53-
echo "Wait 100 seconds for verifier ec2 (read only) to pull the logs from s3 to local environment"
54-
sleep 100
55-
cd .. # to save logs at root level
56-
if [ -n "$GITHUB_ACTIONS" ]; then
57-
PRIVATE_KEY_PATH="./ec2_key.pem"
58-
else
59-
PRIVATE_KEY_PATH="/Users/default/CS/DevOps/AWS/ssh-key-ec2.pem"
60-
fi
61-
52+
# #To verify and pull logs from ec2 to local.
53+
# echo "Wait 2min for verifier ec2 (read only) to pull the logs from s3 to local environment"
54+
# sleep 120
55+
# cd .. # to save logs at root level
6256
# PRIVATE_KEY_PATH="/Users/default/CS/DevOps/AWS/ssh-key-ec2.pem" #change this to your ssh private key path, also make sure to use `chmod 400` on your key before using
63-
echo "trying to scp logs to local"
64-
scp -r -i "$PRIVATE_KEY_PATH" -o StrictHostKeyChecking=no ubuntu@$VERIFIER_IP:/mylogs/ . #to pull logs from readonly ec2 to your local directory /mylogs/
65-
cd $TERRAFORM_DIR # to run destroy need to go to terraform directory
57+
# echo "trying to scp logs to local"
58+
# scp -r -i "$PRIVATE_KEY_PATH" -o StrictHostKeyChecking=no ubuntu@$VERIFIER_IP:/mylogs/ . #to pull logs from readonly ec2 to your local directory /mylogs/
59+
# cd $TERRAFORM_DIR # to run destroy need to go to terraform directory
6660

6761
echo -e "\n"
6862
echo "[+] Using curl on app at http://$RAW_INSTANCE_IP:80"
@@ -71,8 +65,8 @@ curl "http://$RAW_INSTANCE_IP:80"
7165
echo -e "\n"
7266
echo -e "\n"
7367

74-
echo "Terraform destroy will run after 5 minutes..."
68+
echo "Terraform destroy will run after 2 minutes..."
7569
echo "You can press ctrl+c and do it earlier as well"
76-
sleep 300
70+
sleep 120
7771

78-
TF_LOG=DEBUG terraform destroy -var-file="$CONFIG_FILE" -auto-approve
72+
terraform destroy -var-file="$CONFIG_FILE" -auto-approve

scripts/prod_script.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/bash
22

33
# Update system and install dependencies
4-
apt-get update -y
5-
apt-get install -y unzip git openjdk-21-jdk maven
4+
# apt-get update -y
5+
# apt-get install -y unzip git openjdk-21-jdk maven
66

7-
# Install AWS CLI v2
8-
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
9-
unzip awscliv2.zip
10-
sudo ./aws/install
7+
# # Install AWS CLI v2
8+
# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
9+
# unzip awscliv2.zip
10+
# sudo ./aws/install
1111

1212

1313
# Set JAVA_HOME

0 commit comments

Comments
 (0)