Merge pull request #40 from meteordefect/new-blog-seo-updates #187
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |