5
5
branches :
6
6
- devops/a3
7
7
tags :
8
- - ' deploy-*' # deploy-dev, deploy-qa, deploy-prod
8
+ - ' deploy-*' # deploy-dev, deploy-qa, deploy-prod
9
9
workflow_dispatch :
10
10
inputs :
11
11
stage :
@@ -26,68 +26,107 @@ jobs:
26
26
runs-on : ubuntu-latest
27
27
28
28
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
57
31
uses : actions/checkout@v4
58
32
33
+ # ✅ Configure AWS Credentials
59
34
- name : Configure AWS Credentials
60
35
uses : aws-actions/configure-aws-credentials@v4
61
36
with :
62
37
aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
63
38
aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
64
39
aws-region : ${{ env.AWS_REGION }}
65
40
66
- - name : Install Dependencies
67
- run : |
68
- sudo apt update
69
- sudo apt install -y unzip curl
70
-
41
+ # ✅ Install Terraform
71
42
- name : Setup Terraform
72
43
uses : hashicorp/setup-terraform@v2
73
44
with :
74
45
terraform_version : 1.6.6
75
46
76
- - name : Setup SSH Private Key
47
+ # ✅ Terraform Init & Workspace
48
+ - name : Terraform Init and Workspace
77
49
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 }}
80
53
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 }}"
83
60
84
- - name : Run deploy.sh
61
+ # ✅ Fetch Terraform Outputs (Instance IPs, S3 Bucket)
62
+ - name : Get Terraform Outputs
63
+ id : tf_outputs
85
64
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
88
69
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 }}
0 commit comments