1
- name : EC2 Deploy
1
+ name : EC2 Deploy
2
2
3
3
on :
4
4
push :
5
5
branches :
6
6
- devops/a3
7
7
tags :
8
8
- deploy-dev
9
- - deploy-qa
10
9
- deploy-prod
11
10
12
11
workflow_dispatch :
18
17
type : choice
19
18
options :
20
19
- dev
21
- - qa
22
20
- prod
23
21
24
22
env :
@@ -47,62 +45,53 @@ jobs:
47
45
- name : Setup Terraform
48
46
uses : hashicorp/setup-terraform@v3
49
47
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"
67
63
68
64
# Terraform Init & Workspace
69
65
- name : Terraform Init & Workspace
70
66
working-directory : ${{ env.TF_WORKING_DIR }}
71
67
run : |
72
68
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 }
74
70
75
71
# Terraform Apply
76
72
- name : Terraform Apply
77
73
working-directory : ${{ env.TF_WORKING_DIR }}
78
74
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 }"
81
77
82
78
# Output and inject EC2 IPs & S3 Bucket name to Github Env
83
79
- name : Get EC2s Public IPs & S3 Bucket Name
84
80
working-directory : ${{ env.TF_WORKING_DIR }}
85
81
run : |
86
82
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
83
INSTANCE_IP=$(terraform output -raw instance_public_ip)
92
84
VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
93
85
S3_BUCKET=$(terraform output -raw s3_log_bucket)
94
86
95
- # Write to GitHub Actions ENV for later steps
96
87
echo "INSTANCE_IP=$INSTANCE_IP" >> $GITHUB_ENV
97
88
echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
98
89
echo "S3_BUCKET=$S3_BUCKET" >> $GITHUB_ENV
99
90
100
- # Echo both for current step & confirmation
101
91
echo "📦 App IP (Shell): $INSTANCE_IP"
102
92
echo "🔑 Verifier IP (Shell): $VERIFIER_IP"
103
93
echo "🪣 S3 Bucket (Shell): $S3_BUCKET"
104
94
105
-
106
95
# Wait for App Initialization
107
96
- name : Wait for App Initialization
108
97
run : |
@@ -112,9 +101,12 @@ jobs:
112
101
# Validate App Health
113
102
- name : Validate App Health
114
103
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"
115
107
echo "Checking app health at http://${{ env.INSTANCE_IP }}:80"
116
108
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)
118
110
if [[ "$STATUS" == "200" ]]; then
119
111
echo "✅ App is healthy (HTTP 200)"
120
112
exit 0
@@ -126,32 +118,28 @@ jobs:
126
118
echo "❌ App failed health check"
127
119
exit 1
128
120
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
121
# Verify Logs in S3
134
122
- name : Verify Logs in S3
135
123
run : |
136
124
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; }
139
127
echo "✅ Logs found in S3 bucket"
140
128
141
- # Destroy (disabled by default )
129
+ # Destroy (automatically always to avoid manual deletion )
142
130
- name : Destroy infrastructure
143
- if : always()
131
+ if : always()
144
132
working-directory : ${{ env.TF_WORKING_DIR }}
145
133
run : |
146
- echo "🗑️ Destroying infrastructure for stage: ${{ github.event.inputs.stage } }"
134
+ echo "🗑️ Destroying infrastructure for stage: ${STAGE }"
147
135
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 }"
150
138
151
139
# Cleanup Terraform Workspace
152
140
- name : Cleanup Terraform Workspace
153
- if : always()
141
+ if : always()
154
142
working-directory : ${{ env.TF_WORKING_DIR }}
155
143
run : |
156
144
terraform workspace select default
157
- terraform workspace delete ${{ github.event.inputs.stage } }
145
+ terraform workspace delete ${STAGE }
0 commit comments