Skip to content

Commit 9ef68cf

Browse files
committed
ADE-11 Add GitHub Actions workflows
Add automated deployment pipelines for staging and production environments using GitHub Actions. Set up AWS infrastructure with CDK and configure secure role-based authentication. Key Changes: - Add GitHub Actions workflows for staging and production deployments - Configure environment-specific CDK stack deployments - Set up test jobs to run before deployments - Use AWS IAM role for secure authentication - Update Dockerfile with environment support - Add environment variable handling for different stages Infrastructure: - Configure ECS Fargate service with environment-specific settings - Set up API Gateway integration - Configure environment-specific secrets paths - Add CDK infrastructure tests Testing: - Add test jobs as deployment prerequisites - Run unit tests with Vitest - Run CDK infrastructure tests - Ensure tests must pass before deployment Security: - Use role-based authentication with AWS - Configure environment-specific permissions - Remove hardcoded credentials - Use environment-specific secret paths This change enables automated deployments while maintaining security and environment separation.
1 parent 6f5837c commit 9ef68cf

File tree

4 files changed

+171
-4
lines changed

4 files changed

+171
-4
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Deploy to Production
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
id-token: write
10+
contents: read
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: '20'
22+
23+
- name: Install dependencies
24+
run: |
25+
cd backend
26+
npm ci
27+
28+
- name: Run unit tests
29+
run: |
30+
cd backend
31+
npm test
32+
33+
- name: Run CDK tests
34+
run: |
35+
cd backend
36+
npm run test:cdk
37+
38+
deploy:
39+
needs: test
40+
runs-on: ubuntu-latest
41+
environment: production
42+
43+
steps:
44+
- uses: actions/checkout@v4
45+
46+
- name: Configure AWS credentials
47+
uses: aws-actions/configure-aws-credentials@v4
48+
with:
49+
role-to-assume: arn:aws:iam::841162674562:role/GitHubAction-AssumeRoleWithAction
50+
aws-region: us-east-1
51+
role-session-name: GithubActionsDeployment
52+
53+
- name: Setup Node.js
54+
uses: actions/setup-node@v4
55+
with:
56+
node-version: '20'
57+
58+
- name: Install dependencies
59+
run: |
60+
cd backend
61+
npm ci
62+
63+
- name: Build application
64+
run: |
65+
cd backend
66+
npm run build
67+
68+
- name: Deploy to AWS
69+
run: |
70+
cd backend
71+
npm run cdk deploy -- \
72+
--require-approval never \
73+
--context environment=production
74+
env:
75+
CDK_DEFAULT_ACCOUNT: ${{ secrets.AWS_ACCOUNT_ID }}
76+
CDK_DEFAULT_REGION: us-east-1
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Deploy to Staging
2+
3+
on:
4+
push:
5+
branches:
6+
- staging
7+
8+
permissions:
9+
id-token: write
10+
contents: read
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: '20'
22+
23+
- name: Install dependencies
24+
run: |
25+
cd backend
26+
npm ci
27+
28+
- name: Run unit tests
29+
run: |
30+
cd backend
31+
npm test
32+
33+
- name: Run CDK tests
34+
run: |
35+
cd backend
36+
npm run test:cdk
37+
38+
deploy:
39+
needs: test
40+
runs-on: ubuntu-latest
41+
environment: staging
42+
43+
steps:
44+
- uses: actions/checkout@v4
45+
46+
- name: Configure AWS credentials
47+
uses: aws-actions/configure-aws-credentials@v4
48+
with:
49+
role-to-assume: arn:aws:iam::841162674562:role/GitHubAction-AssumeRoleWithAction
50+
aws-region: us-east-1
51+
role-session-name: GithubActionsDeployment
52+
53+
- name: Setup Node.js
54+
uses: actions/setup-node@v4
55+
with:
56+
node-version: '20'
57+
58+
- name: Install dependencies
59+
run: |
60+
cd backend
61+
npm ci
62+
63+
- name: Build application
64+
run: |
65+
cd backend
66+
npm run build
67+
68+
- name: Deploy to AWS
69+
run: |
70+
cd backend
71+
npm run cdk deploy -- \
72+
--require-approval never \
73+
--context environment=staging
74+
env:
75+
CDK_DEFAULT_ACCOUNT: ${{ secrets.AWS_ACCOUNT_ID }}
76+
CDK_DEFAULT_REGION: us-east-1

backend/Dockerfile.prod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FROM node:20-slim as builder
22

3+
ARG NODE_ENV=production
4+
ENV NODE_ENV=${NODE_ENV}
5+
36
WORKDIR /usr/src/app
47

58
COPY package*.json ./
@@ -10,6 +13,9 @@ RUN npm run build
1013

1114
FROM node:20-slim
1215

16+
ARG NODE_ENV=production
17+
ENV NODE_ENV=${NODE_ENV}
18+
1319
WORKDIR /usr/src/app
1420

1521
COPY package*.json ./

backend/src/iac/backend-stack.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import * as apigateway from 'aws-cdk-lib/aws-apigateway';
66
import * as ecr from 'aws-cdk-lib/aws-ecr';
77
import { Construct } from 'constructs';
88

9+
interface BackendStackProps extends cdk.StackProps {
10+
environment: 'staging' | 'production';
11+
}
12+
913
export class BackendStack extends cdk.Stack {
10-
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
14+
constructor(scope: Construct, id: string, props: BackendStackProps) {
1115
super(scope, id, props);
1216

17+
const isProd = props.environment === 'production';
18+
1319
// VPC
1420
const vpc = new ec2.Vpc(this, 'MedicalReportsVPC', {
1521
maxAzs: 2,
@@ -29,12 +35,15 @@ export class BackendStack extends cdk.Stack {
2935
desiredCount: 2,
3036
taskImageOptions: {
3137
image: ecs.ContainerImage.fromAsset('../backend/', {
32-
file: 'Dockerfile.prod'
38+
file: 'Dockerfile.prod',
39+
buildArgs: {
40+
NODE_ENV: props.environment,
41+
},
3342
}),
3443
containerPort: 3000,
3544
environment: {
36-
NODE_ENV: 'production',
37-
PERPLEXITY_API_KEY_SECRET_NAME: 'medical-reports-explainer/perplexity-api-key',
45+
NODE_ENV: props.environment,
46+
PERPLEXITY_API_KEY_SECRET_NAME: `medical-reports-explainer/${props.environment}/perplexity-api-key`,
3847
PERPLEXITY_MODEL: 'sonar',
3948
PERPLEXITY_MAX_TOKENS: '2048',
4049
},

0 commit comments

Comments
 (0)