Skip to content

Commit 6e30089

Browse files
Merge pull request #2 from CloudNinjaDev/infra
Refactor Terraform configuration for MongoDB deployment: modularize E…
2 parents 5313ae5 + add1924 commit 6e30089

16 files changed

Lines changed: 361 additions & 244 deletions

File tree

.github/workflows/cd.yml

Lines changed: 31 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ on:
1010

1111
workflow_dispatch:
1212
inputs:
13-
plan_only:
14-
description: 'Run plan only (no apply)'
15-
required: false
16-
default: 'false'
13+
environment:
14+
description: 'Environment to deploy'
15+
required: true
16+
default: 'production'
1717
type: choice
1818
options:
19-
- 'true'
20-
- 'false'
19+
- 'production'
20+
- 'staging'
2121

2222
destroy:
23-
description: 'Destroy all infrastructure (DANGEROUS)'
23+
description: 'Destroy infrastructure'
2424
required: false
2525
default: 'false'
2626
type: choice
@@ -31,27 +31,22 @@ on:
3131
permissions:
3232
contents: read
3333
id-token: write
34-
issues: write
3534

3635
env:
3736
AWS_REGION: us-west-2
38-
TF_WORKING_DIR: terraform
3937
TERRAFORM_VERSION: 1.6.0
4038

4139
jobs:
42-
terraform-plan:
43-
name: Plan Infrastructure Changes
40+
deploy:
41+
name: Deploy to ${{ github.event.inputs.environment || 'production' }}
4442
runs-on: ubuntu-latest
4543

4644
environment:
47-
name: production
45+
name: ${{ github.event.inputs.environment || 'production' }}
4846

4947
defaults:
5048
run:
51-
working-directory: ${{ env.TF_WORKING_DIR }}
52-
53-
outputs:
54-
has_changes: ${{ steps.plan.outputs.exitcode == 2 }}
49+
working-directory: terraform/stacks/${{ github.event.inputs.environment || 'production' }}
5550

5651
steps:
5752
- name: Checkout Code
@@ -60,174 +55,51 @@ jobs:
6055
- name: Configure AWS Credentials
6156
uses: aws-actions/configure-aws-credentials@v4
6257
with:
63-
aws-access-key-id: ${{ secrets.AWS_SECRET_ACCESS_ID }}
64-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
58+
aws-access-key-id: ${{ secrets.AWS_SECRET_ACCESS_ID }} # DO NOT CHANGE THIS LINE
59+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # DO NOT CHANGE THIS LINE
6560
aws-region: ${{ env.AWS_REGION }}
6661

6762
- name: Setup Terraform
6863
uses: hashicorp/setup-terraform@v3
6964
with:
7065
terraform_version: ${{ env.TERRAFORM_VERSION }}
71-
terraform_wrapper: false
72-
73-
- name: Terraform Format Check
74-
run: terraform fmt -check -recursive
7566

7667
- name: Terraform Init
77-
run: terraform init -upgrade
78-
79-
- name: Terraform Validate
80-
run: terraform validate
68+
run: terraform init
8169

8270
- name: Terraform Plan
83-
id: plan
84-
run: |
85-
terraform plan -detailed-exitcode -out=tfplan -input=false || exit_code=$?
86-
echo "exitcode=$exit_code" >> $GITHUB_OUTPUT
87-
terraform show tfplan -no-color
88-
continue-on-error: true
89-
90-
- name: Upload Plan Artifact
91-
if: steps.plan.outputs.exitcode == 2
92-
uses: actions/upload-artifact@v4
93-
with:
94-
name: terraform-plan-${{ github.sha }}
95-
path: ${{ env.TF_WORKING_DIR }}/tfplan
96-
retention-days: 1
97-
98-
- name: Create Deployment Summary
99-
if: always()
100-
run: |
101-
echo "## Terraform Plan Summary" >> $GITHUB_STEP_SUMMARY
102-
echo "" >> $GITHUB_STEP_SUMMARY
103-
echo "- **Workflow:** ${{ github.workflow }}" >> $GITHUB_STEP_SUMMARY
104-
echo "- **Triggered by:** ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY
105-
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
106-
echo "- **Plan Status:** ${{ steps.plan.outcome }}" >> $GITHUB_STEP_SUMMARY
107-
echo "- **Has Changes:** ${{ steps.plan.outputs.exitcode == 2 }}" >> $GITHUB_STEP_SUMMARY
108-
109-
terraform-apply:
110-
name: Apply Infrastructure Changes
111-
runs-on: ubuntu-latest
112-
113-
needs: terraform-plan
114-
if: |
115-
needs.terraform-plan.result == 'success' &&
116-
needs.terraform-plan.outputs.has_changes == 'true' &&
117-
github.event.inputs.plan_only != 'true'
118-
119-
defaults:
120-
run:
121-
working-directory: ${{ env.TF_WORKING_DIR }}
122-
123-
steps:
124-
- name: Checkout Code
125-
uses: actions/checkout@v4
126-
127-
- name: Configure AWS Credentials
128-
uses: aws-actions/configure-aws-credentials@v4
129-
with:
130-
aws-access-key-id: ${{ secrets.AWS_SECRET_ACCESS_ID }}
131-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
132-
aws-region: ${{ env.AWS_REGION }}
133-
134-
- name: Setup Terraform
135-
uses: hashicorp/setup-terraform@v3
136-
with:
137-
terraform_version: ${{ env.TERRAFORM_VERSION }}
138-
139-
- name: Terraform Init
140-
run: terraform init -upgrade
141-
142-
- name: Download Plan Artifact
143-
uses: actions/download-artifact@v4
144-
with:
145-
name: terraform-plan-${{ github.sha }}
146-
path: ${{ env.TF_WORKING_DIR }}
71+
if: github.event.inputs.destroy != 'true'
72+
run: terraform plan -var-file=terraform.tfvars
14773

14874
- name: Terraform Apply
149-
id: apply
150-
run: |
151-
echo "Applying Terraform changes..."
152-
terraform apply -auto-approve -input=false tfplan
75+
if: github.event.inputs.destroy != 'true'
76+
run: terraform apply -var-file=terraform.tfvars -auto-approve
15377

154-
- name: Get Terraform Outputs
78+
- name: Terraform Destroy
79+
if: github.event.inputs.destroy == 'true'
80+
run: terraform destroy -var-file=terraform.tfvars -auto-approve
81+
82+
- name: Get Outputs
83+
if: github.event.inputs.destroy != 'true'
15584
id: outputs
156-
if: steps.apply.outcome == 'success'
15785
run: |
15886
echo "instance_id=$(terraform output -raw instance_id)" >> $GITHUB_OUTPUT
15987
echo "instance_public_ip=$(terraform output -raw instance_public_ip)" >> $GITHUB_OUTPUT
16088
echo "instance_private_ip=$(terraform output -raw instance_private_ip)" >> $GITHUB_OUTPUT
89+
continue-on-error: true
16190

162-
- name: Create Deployment Summary
91+
- name: Deployment Summary
16392
if: always()
16493
run: |
165-
echo "## Terraform Apply Summary" >> $GITHUB_STEP_SUMMARY
166-
echo "" >> $GITHUB_STEP_SUMMARY
167-
echo "- **Workflow:** ${{ github.workflow }}" >> $GITHUB_STEP_SUMMARY
94+
echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY
95+
echo "- **Environment:** ${{ github.event.inputs.environment || 'production' }}" >> $GITHUB_STEP_SUMMARY
96+
echo "- **Action:** ${{ github.event.inputs.destroy == 'true' && 'Destroy' || 'Deploy' }}" >> $GITHUB_STEP_SUMMARY
16897
echo "- **Triggered by:** ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY
169-
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
170-
echo "- **Apply Status:** ${{ steps.apply.outcome }}" >> $GITHUB_STEP_SUMMARY
17198
172-
if [ "${{ steps.apply.outcome }}" == "success" ]; then
99+
if [ "${{ github.event.inputs.destroy }}" != "true" ] && [ "${{ steps.outputs.outputs.instance_id }}" != "" ]; then
173100
echo "" >> $GITHUB_STEP_SUMMARY
174-
echo "### EC2 Instance Information" >> $GITHUB_STEP_SUMMARY
101+
echo "### Instance Details" >> $GITHUB_STEP_SUMMARY
175102
echo "- **Instance ID:** ${{ steps.outputs.outputs.instance_id }}" >> $GITHUB_STEP_SUMMARY
176103
echo "- **Public IP:** ${{ steps.outputs.outputs.instance_public_ip }}" >> $GITHUB_STEP_SUMMARY
177104
echo "- **Private IP:** ${{ steps.outputs.outputs.instance_private_ip }}" >> $GITHUB_STEP_SUMMARY
178-
echo "" >> $GITHUB_STEP_SUMMARY
179-
echo "### Connect to Instance" >> $GITHUB_STEP_SUMMARY
180-
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
181-
echo "ssh -i ~/.ssh/ashish-test-kp.pem ec2-user@${{ steps.outputs.outputs.instance_public_ip }}" >> $GITHUB_STEP_SUMMARY
182-
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
183105
fi
184-
185-
- name: Notify on Failure
186-
if: failure()
187-
run: |
188-
echo "::error::Terraform apply failed. Please check the logs and fix the issues."
189-
190-
terraform-destroy:
191-
name: Destroy Infrastructure
192-
runs-on: ubuntu-latest
193-
194-
if: github.event_name == 'workflow_dispatch' && github.event.inputs.destroy == 'true'
195-
196-
environment:
197-
name: destroy-production
198-
199-
defaults:
200-
run:
201-
working-directory: ${{ env.TF_WORKING_DIR }}
202-
203-
steps:
204-
- name: Checkout Code
205-
uses: actions/checkout@v4
206-
207-
- name: Configure AWS Credentials
208-
uses: aws-actions/configure-aws-credentials@v4
209-
with:
210-
aws-access-key-id: ${{ secrets.AWS_SECRET_ACCESS_ID }}
211-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
212-
aws-region: ${{ env.AWS_REGION }}
213-
214-
- name: Setup Terraform
215-
uses: hashicorp/setup-terraform@v3
216-
with:
217-
terraform_version: ${{ env.TERRAFORM_VERSION }}
218-
219-
- name: Terraform Init
220-
run: terraform init -upgrade
221-
222-
- name: Terraform Destroy
223-
run: |
224-
echo "Destroying all infrastructure..."
225-
terraform destroy -auto-approve -input=false
226-
227-
- name: Confirm Destruction
228-
run: |
229-
echo "## Infrastructure Destroyed" >> $GITHUB_STEP_SUMMARY
230-
echo "" >> $GITHUB_STEP_SUMMARY
231-
echo "All resources have been destroyed." >> $GITHUB_STEP_SUMMARY
232-
echo "- **Triggered by:** ${{ github.actor }}" >> $GITHUB_STEP_SUMMARY
233-
echo "- **Timestamp:** $(date)" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)