-
Notifications
You must be signed in to change notification settings - Fork 0
111 lines (94 loc) · 3.54 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Deploy Next.js App and CDK Infrastructure
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
# 1. Checkout Code
- uses: actions/checkout@v3
# 2. Setup Node.js
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install CDK CLI
run: npm install -g aws-cdk
# 3. Install Dependencies for Next.js
- name: Install Dependencies for Next.js
run: npm ci
# 4. Build Next.js Application
- name: Build Next.js Application
run: npm run build
# 5. Package Deployment Files
- name: Package Deployment Files
run: |
echo "Creating deployment package..."
zip -r deploy.zip .next package.json package-lock.json public node_modules
echo "Package created successfully"
# 6. Verify Deployment Package
- name: Verify Deployment Package
run: |
if [ ! -f deploy.zip ]; then
echo "Error: deploy.zip not found"
exit 1
fi
# 7. Configure AWS Credentials
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-2
# 8. Deploy S3 Stack
- name: Deploy S3 Stack
working-directory: cipher-infra
run: |
echo "Installing dependencies for CDK..."
npm ci
npm install --save-dev @types/node ts-node
npm install aws-cdk-lib
echo "Verifying CDK installation..."
cdk --version
echo "Deploying S3 Stack..."
cdk deploy S3Stack --require-approval never --outputs-file s3-outputs.json
# 9. Upload Deployment Package to S3
- name: Upload Deployment Package
run: |
BUCKET_NAME=$(jq -r '.S3Stack.DeploymentBucketName' cipher-infra/s3-outputs.json)
echo "Uploading deployment package to bucket: ${BUCKET_NAME}"
aws s3 cp deploy.zip "s3://${BUCKET_NAME}/deploy.zip"
# 10. Deploy EC2 Stack
- name: Deploy EC2 Stack
working-directory: cipher-infra
run: |
cdk deploy EC2Stack --require-approval never --outputs-file ec2-outputs.json
# 11. Invalidate CloudFront Cache
- name: Invalidate CloudFront Cache
run: |
echo "Checking EC2Stack outputs..."
cat cipher-infra/ec2-outputs.json
CLOUDFRONT_ID=$(jq -r '.EC2Stack.CloudFrontDistributionId' cipher-infra/ec2-outputs.json)
if [ -n "$CLOUDFRONT_ID" ] && [ "$CLOUDFRONT_ID" != "null" ]; then
echo "Invalidating CloudFront distribution: ${CLOUDFRONT_ID}"
aws cloudfront create-invalidation --distribution-id "$CLOUDFRONT_ID" --paths "/*"
echo "CloudFront cache invalidated successfully."
else
echo "Error: CloudFront Distribution ID not found in outputs"
echo "Contents of ec2-outputs.json:"
cat cipher-infra/ec2-outputs.json
exit 1
fi
# 13. Deploy ContactFormStack
- name: Deploy ContactFormStack
working-directory: cipher-infra
run: |
echo "Installing dependencies for ContactFormStack..."
npm ci
echo "Deploying ContactFormStack..."
cdk deploy ContactFormStack --require-approval never