Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit db55e99

Browse files
committed
Refactor deployment workflow and add S3 and CloudFront stacks for frontend infrastructure
1 parent aa81939 commit db55e99

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

.github/workflows/deploy.yml

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ jobs:
3232
name: build-artifact
3333
path: dist
3434

35-
deploy_cdk:
35+
setup:
3636
runs-on: ubuntu-latest
37+
outputs:
38+
env_name: ${{ steps.set_env.outputs.env_name }}
3739
steps:
3840
- name: Checkout
3941
uses: actions/checkout@v4
@@ -59,6 +61,7 @@ jobs:
5961
run: npm install -g aws-cdk
6062

6163
- name: Determine Environment Name
64+
id: set_env
6265
run: |
6366
if [[ $GITHUB_REF == refs/heads/main ]]; then
6467
ENV_NAME="production"
@@ -67,15 +70,65 @@ jobs:
6770
else
6871
ENV_NAME="dev"
6972
fi
70-
echo "ENV_NAME=$ENV_NAME" >> $GITHUB_ENV
73+
echo "env_name=$ENV_NAME" >> $GITHUB_OUTPUT
74+
75+
deploy_s3:
76+
runs-on: ubuntu-latest
77+
needs: setup
78+
steps:
79+
- name: Checkout
80+
uses: actions/checkout@v4
81+
82+
- name: Configure AWS Credentials
83+
uses: aws-actions/configure-aws-credentials@v4
84+
with:
85+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
86+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
87+
aws-region: ap-south-1
88+
89+
- name: Deploy S3 Stack
90+
working-directory: infra
91+
run: cdk deploy BeejhoFrontendS3Stack-${{ needs.setup.outputs.env_name }} --require-approval never
92+
93+
deploy_certificate:
94+
runs-on: ubuntu-latest
95+
needs: setup
96+
steps:
97+
- name: Checkout
98+
uses: actions/checkout@v4
99+
100+
- name: Configure AWS Credentials
101+
uses: aws-actions/configure-aws-credentials@v4
102+
with:
103+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
104+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
105+
aws-region: us-east-1 # Certificates must be in us-east-1 for CloudFront
106+
107+
- name: Deploy Certificate Stack
108+
working-directory: infra
109+
run: cdk deploy BeejhoFrontendCertificateStack --require-approval never
110+
111+
deploy_infra:
112+
runs-on: ubuntu-latest
113+
needs: [deploy_s3, deploy_certificate] # Ensures infra deploys only after S3 and Certificate are done
114+
steps:
115+
- name: Checkout
116+
uses: actions/checkout@v4
117+
118+
- name: Configure AWS Credentials
119+
uses: aws-actions/configure-aws-credentials@v4
120+
with:
121+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
122+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
123+
aws-region: ap-south-1
71124

72-
- name: Deploy CDK Stack
125+
- name: Deploy Infra Stack
73126
working-directory: infra
74-
run: cdk deploy --all -c env=${{ env.ENV_NAME }} --require-approval never
127+
run: cdk deploy BeejhoFrontendStack-${{ needs.setup.outputs.env_name }} --require-approval never
75128

76129
sync_to_s3:
77130
runs-on: ubuntu-latest
78-
needs: [build, deploy_cdk]
131+
needs: [build, deploy_s3]
79132
steps:
80133
- name: Checkout
81134
uses: actions/checkout@v4

infra/bin/infra.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#!/usr/bin/env node
22
import 'source-map-support/register';
33
import * as cdk from 'aws-cdk-lib';
4-
import { InfraStack } from '../lib/infra-stack';
4+
import { InfraStack } from '../lib/cloudfront-stack';
55
import { CertificateStack } from '../lib/certificate-stack';
6+
import { S3Stack } from '../lib/s3-stack';
67

78
const app = new cdk.App();
89
const env = { account: process.env.CDK_DEFAULT_ACCOUNT, region: 'us-east-1' };
910

1011
const envName = (app.node.tryGetContext('env') || 'dev').toLowerCase();
1112

13+
new S3Stack(app, 'S3Stack', { env, envName, stackName: `BeejhoFrontendS3Stack-${envName}` });
14+
1215
new CertificateStack(app, 'CertificateStack', { env, stackName: `BeejhoFrontendCertificateStack` });
1316

1417
new InfraStack(app, 'InfraStack', { env, envName, stackName: `BeejhoFrontendStack-${envName}` });
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ export class InfraStack extends cdk.Stack {
1515
constructor(scope: Construct, id: string, props: InfraStackProps) {
1616
super(scope, id, props);
1717

18-
const bucket = new Bucket(this, 'BeejhoFrontendBucket', {
19-
websiteIndexDocument: 'index.html',
20-
websiteErrorDocument: 'index.html',
21-
removalPolicy: cdk.RemovalPolicy.DESTROY,
22-
bucketName: `beejho-frontend-${props.envName}`,
23-
publicReadAccess: true,
24-
blockPublicAccess: BlockPublicAccess.BLOCK_ACLS,
25-
autoDeleteObjects: true,
26-
});
27-
2818
const hostedZone = HostedZone.fromLookup(this, 'BeejhoHostedZone', {
2919
domainName: 'beejho.in',
3020
});
@@ -37,6 +27,7 @@ export class InfraStack extends cdk.Stack {
3727
? [`${props.envName}.beejho.in`, `www.${props.envName}.beejho.in`]
3828
: ['beejho.in', 'www.beejho.in'];
3929

30+
const bucket = Bucket.fromBucketName(this, `BeejhoFrontendBucket-${props.envName}`, `beejho-frontend-${props.envName}`);
4031

4132
const distribution = new Distribution(this, 'BeejhoCloudFrontDistribution', {
4233
defaultBehavior: {

infra/lib/s3-stack.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { Certificate, CertificateValidation } from 'aws-cdk-lib/aws-certificatemanager';
3+
import { HostedZone } from 'aws-cdk-lib/aws-route53';
4+
import { BlockPublicAccess, Bucket } from 'aws-cdk-lib/aws-s3';
5+
import { Construct } from 'constructs';
6+
7+
interface S3StackProps extends cdk.StackProps {
8+
envName: string;
9+
}
10+
11+
export class S3Stack extends cdk.Stack {
12+
constructor(scope: Construct, id: string, props: S3StackProps) {
13+
super(scope, id, props);
14+
15+
const bucket = new Bucket(this, 'BeejhoFrontendBucket', {
16+
websiteIndexDocument: 'index.html',
17+
websiteErrorDocument: 'index.html',
18+
removalPolicy: cdk.RemovalPolicy.DESTROY,
19+
bucketName: `beejho-frontend-${props.envName}`,
20+
publicReadAccess: true,
21+
blockPublicAccess: BlockPublicAccess.BLOCK_ACLS,
22+
autoDeleteObjects: true,
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)