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,90 @@ 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
+ APP_IP=$(terraform output -raw instance_public_ip)
67
+ VERIFIER_IP=$(terraform output -raw verifier_instance_public_ip)
68
+ S3_BUCKET=$(terraform output -raw s3_bucket_name)
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" >> $GITHUB_ENV
71
+ echo "VERIFIER_IP=$VERIFIER_IP" >> $GITHUB_ENV
72
+ echo "S3_BUCKET=$S3_BUCKET" >> $GITHUB_ENV
73
+
74
+ # ✅ Wait for App & Logs
75
+ - name : Wait for App & Logs
76
+ run : |
77
+ echo "Waiting 90 seconds for EC2 instances to initialize..."
78
+ sleep 90
79
+
80
+ # ✅ Validate App Health
81
+ - name : Check Application Health
82
+ run : |
83
+ echo "Checking app health on http://$APP_IP:80"
84
+ if curl -fs http://$APP_IP:80; then
85
+ echo "✅ App is running."
86
+ else
87
+ echo "❌ App is not responding."
88
+ exit 1
89
+ fi
90
+
91
+ # ✅ Verify Logs on Read-Only EC2
92
+ - name : Verify Logs on EC2-2 (read-only)
93
+ run : |
94
+ echo "Connecting to verifier EC2 ($VERIFIER_IP)..."
95
+ ssh -i ./ec2_key.pem -o StrictHostKeyChecking=no ubuntu@$VERIFIER_IP "
96
+ if [ -s /mylogs/app/my-app.log ] && [ -s /mylogs/system/cloud-init.log ]; then
97
+ echo '✅ Logs found on EC2-2.'
98
+ else
99
+ echo '❌ Logs missing on EC2-2.'
100
+ exit 1
101
+ fi
102
+ "
103
+
104
+ # # ✅ Download Logs from EC2-2
105
+ # - name: Download Logs from EC2-2
106
+ # run: |
107
+ # mkdir -p mylogs
108
+ # scp -i ./ec2_key.pem -o StrictHostKeyChecking=no -r ubuntu@$VERIFIER_IP:/mylogs/* ./mylogs/
109
+
110
+ # # ✅ Upload Logs as Artifact
111
+ # - name: Upload Logs as Artifact
112
+ # uses: actions/upload-artifact@v4
113
+ # with:
114
+ # name: ec2-logs-${{ github.event.inputs.stage }}
115
+ # path: mylogs/
0 commit comments