Skip to content

Commit 11e8374

Browse files
committed
Added push to trigger
1 parent 827136a commit 11e8374

File tree

5 files changed

+38
-50
lines changed

5 files changed

+38
-50
lines changed

.github/workflows/deploy.yml

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
name: EC2 Deploy
1+
name: EC2 Deploy
22

33
on:
44
push:
55
branches:
66
- devops/a3
77
tags:
88
- deploy-dev
9-
- deploy-qa
109
- deploy-prod
1110

1211
workflow_dispatch:
@@ -18,7 +17,6 @@ on:
1817
type: choice
1918
options:
2019
- dev
21-
- qa
2220
- prod
2321

2422
env:
@@ -47,62 +45,53 @@ jobs:
4745
- name: Setup Terraform
4846
uses: hashicorp/setup-terraform@v3
4947

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"
48+
# Determine Stage - dev/prod defaults to dev
49+
- name: Determine Stage
50+
id: set_stage
51+
run: |
52+
if [[ "${GITHUB_REF}" == "refs/tags/deploy-dev" ]]; then
53+
echo "STAGE=dev" >> $GITHUB_ENV
54+
elif [[ "${GITHUB_REF}" == "refs/tags/deploy-prod" ]]; then
55+
echo "STAGE=prod" >> $GITHUB_ENV
56+
elif [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
57+
echo "STAGE=${{ github.event.inputs.stage }}" >> $GITHUB_ENV
58+
else
59+
echo "STAGE=dev" >> $GITHUB_ENV # fallback
60+
fi
61+
62+
echo "🛠️ Deployment stage: $STAGE"
6763
6864
# Terraform Init & Workspace
6965
- name: Terraform Init & Workspace
7066
working-directory: ${{ env.TF_WORKING_DIR }}
7167
run: |
7268
terraform init
73-
terraform workspace select ${{ github.event.inputs.stage }} || terraform workspace new ${{ github.event.inputs.stage }}
69+
terraform workspace select ${STAGE} || terraform workspace new ${STAGE}
7470
7571
# Terraform Apply
7672
- name: Terraform Apply
7773
working-directory: ${{ env.TF_WORKING_DIR }}
7874
run: |
79-
terraform apply -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
80-
-var "stage=${{ github.event.inputs.stage }}"
75+
terraform apply -var-file="${STAGE}_config.tfvars" -auto-approve \
76+
-var "stage=${STAGE}"
8177
8278
# Output and inject EC2 IPs & S3 Bucket name to Github Env
8379
- name: Get EC2s Public IPs & S3 Bucket Name
8480
working-directory: ${{ env.TF_WORKING_DIR }}
8581
run: |
8682
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
9183
INSTANCE_IP=$(terraform output -raw instance_public_ip)
9284
VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
9385
S3_BUCKET=$(terraform output -raw s3_log_bucket)
9486
95-
# Write to GitHub Actions ENV for later steps
9687
echo "INSTANCE_IP=$INSTANCE_IP" >> $GITHUB_ENV
9788
echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
9889
echo "S3_BUCKET=$S3_BUCKET" >> $GITHUB_ENV
9990
100-
# Echo both for current step & confirmation
10191
echo "📦 App IP (Shell): $INSTANCE_IP"
10292
echo "🔑 Verifier IP (Shell): $VERIFIER_IP"
10393
echo "🪣 S3 Bucket (Shell): $S3_BUCKET"
10494
105-
10695
# Wait for App Initialization
10796
- name: Wait for App Initialization
10897
run: |
@@ -112,9 +101,12 @@ jobs:
112101
# Validate App Health
113102
- name: Validate App Health
114103
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"
115107
echo "Checking app health at http://${{ env.INSTANCE_IP }}:80"
116108
for i in {1..10}; do
117-
STATUS=$(curl -o /dev/null -s -w "%{http_code}" http://$INSTANCE_IP:80)
109+
STATUS=$(curl -o /dev/null -s -w "%{http_code}" http://${{ env.INSTANCE_IP }}:80)
118110
if [[ "$STATUS" == "200" ]]; then
119111
echo "✅ App is healthy (HTTP 200)"
120112
exit 0
@@ -126,32 +118,28 @@ jobs:
126118
echo "❌ App failed health check"
127119
exit 1
128120
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-
133121
# Verify Logs in S3
134122
- name: Verify Logs in S3
135123
run: |
136124
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; }
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; }
139127
echo "✅ Logs found in S3 bucket"
140128
141-
# Destroy (disabled by default)
129+
# Destroy (automatically always to avoid manual deletion)
142130
- name: Destroy infrastructure
143-
if: always()
131+
if: always()
144132
working-directory: ${{ env.TF_WORKING_DIR }}
145133
run: |
146-
echo "🗑️ Destroying infrastructure for stage: ${{ github.event.inputs.stage }}"
134+
echo "🗑️ Destroying infrastructure for stage: ${STAGE}"
147135
sleep 60
148-
terraform destroy -var-file="${{ github.event.inputs.stage }}_config.tfvars" -auto-approve \
149-
-var "stage=${{ github.event.inputs.stage }}"
136+
terraform destroy -var-file="${STAGE}_config.tfvars" -auto-approve \
137+
-var "stage=${STAGE}"
150138
151139
# Cleanup Terraform Workspace
152140
- name: Cleanup Terraform Workspace
153-
if: always()
141+
if: always()
154142
working-directory: ${{ env.TF_WORKING_DIR }}
155143
run: |
156144
terraform workspace select default
157-
terraform workspace delete ${{ github.event.inputs.stage }}
145+
terraform workspace delete ${STAGE}

scripts/dev_script.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ nohup java -jar target/*.jar --server.port=80 > /var/log/my-app.log 2>&1 &
3030
sleep 5
3131

3232
# Upload Logs to S3
33-
sudo aws s3 cp /var/log/cloud-init.log s3://${s3_bucket_name}/system/
34-
sudo aws s3 cp /var/log/my-app.log s3://${s3_bucket_name}/app/
33+
sudo aws s3 cp /var/log/cloud-init.log s3://${s3_bucket_name}/dev/system/
34+
sudo aws s3 cp /var/log/my-app.log s3://${s3_bucket_name}/dev/app/
3535

3636
# Shutdown after timeout
3737
sudo shutdown -h +${shutdown_minutes}

scripts/prod_script.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ nohup java -jar target/*.jar --server.port=80 > /var/log/my-app.log 2>&1 &
2929
sleep 5
3030

3131
# Upload Logs to S3
32-
aws s3 cp /var/log/cloud-init.log s3://${s3_bucket_name}/system/
33-
aws s3 cp /var/log/my-app.log s3://${s3_bucket_name}/app/
32+
aws s3 cp /var/log/cloud-init.log s3://${s3_bucket_name}/prod/system/
33+
aws s3 cp /var/log/my-app.log s3://${s3_bucket_name}/prod/app/
3434

3535
# Shutdown after timeout
3636
sudo shutdown -h +${shutdown_minutes}

terraform/dev_config.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
instance_type = "t2.micro"
22
key_name = "ssh-key-ec2" #change this to your key-pair name
33
# ami_id = "ami-0f918f7e67a3323f0"
4-
stage = "Dev"
4+
stage = "dev"
55
shutdown_minutes = 30
66
s3_bucket_name = "techeazy-logs-dev-unique123ss" # Change this!
77
aws_region = "ap-south-1"

terraform/prod_config.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ key_name = "ssh-key-ec2" #change this to your key-pair name
33
# ami_id = "ami-0f918f7e67a3323f0"
44
stage = "prod"
55
shutdown_minutes = 25
6-
s3_bucket_name = "techeazy-logs-dev-unique123ss" # Change this!
6+
s3_bucket_name = "techeazy-logs-prod-unique123ss" # Change this!
77
aws_region = "ap-south-1"
88
repo_url = "https://github.com/techeazy-consulting/techeazy-devops"
99
verifier_lifetime = 25

0 commit comments

Comments
 (0)