Merge pull request #31 from meteordefect/Fix-form-cors-6 #177
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. Initial Setup | |
- uses: actions/checkout@v3 | |
- 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 | |
# 2. Install Dependencies | |
- name: Install Next.js Dependencies | |
run: npm ci | |
# 3. Configure AWS | |
- 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 | |
# 4. Deploy Infrastructure | |
- name: Setup CDK Project | |
working-directory: cipher-infra | |
run: | | |
npm ci | |
npm install --save-dev @types/node ts-node | |
npm install aws-cdk-lib | |
echo "CDK Version:" | |
cdk --version | |
- name: Deploy S3 Stack | |
working-directory: cipher-infra | |
run: | | |
echo "Deploying S3 Stack..." | |
cdk deploy cipher-s3-stack --require-approval never --outputs-file s3-outputs.json | |
cat s3-outputs.json | |
- name: Deploy EC2 Stack | |
working-directory: cipher-infra | |
run: | | |
echo "Deploying EC2 Stack..." | |
cdk deploy cipher-ec2-stack --require-approval never --outputs-file ec2-outputs.json | |
cat ec2-outputs.json | |
- name: Deploy Contact Form Stack | |
working-directory: cipher-infra | |
run: | | |
echo "Deploying Contact Form Stack..." | |
cdk deploy cipher-contact-form-stack --require-approval never --outputs-file contact-form-outputs.json | |
# Set up environment variables for Next.js build | |
API_GATEWAY_URL=$(jq -r '."cipher-contact-form-stack".ApiEndpoint' contact-form-outputs.json)contact | |
API_KEY=$(jq -r '."cipher-contact-form-stack".ApiKey' contact-form-outputs.json) | |
echo "API Gateway URL: ${API_GATEWAY_URL}" | |
echo "API Key: ${API_KEY}" | |
echo "NEXT_PUBLIC_API_GATEWAY_URL=${API_GATEWAY_URL}" > ../.env.production | |
echo "NEXT_PUBLIC_API_KEY=${API_KEY}" >> ../.env.production | |
echo "Showing .env.production content:" | |
cat ../.env.production | |
# 5. Build and Package Next.js | |
- name: Build and Package Next.js | |
run: | | |
echo "Building Next.js application..." | |
npm run build | |
echo "Copying .env.production to build directory..." | |
mkdir -p .next/standalone | |
cp .env.production .next/standalone/ | |
echo "Showing contents of .env.production:" | |
cat .env.production | |
echo "Creating deployment package..." | |
zip -r deploy.zip .next package.json package-lock.json public node_modules .env.production | |
echo "Verifying package contents:" | |
unzip -l deploy.zip | grep .env | |
# 6. Upload to S3 | |
- name: Upload to S3 | |
run: | | |
BUCKET_NAME=$(jq -r '."cipher-s3-stack".DeploymentBucketName' cipher-infra/s3-outputs.json) | |
echo "Uploading package to bucket: ${BUCKET_NAME}" | |
aws s3 cp deploy.zip "s3://${BUCKET_NAME}/deploy.zip" | |
# 7. Invalidate CloudFront | |
- name: Invalidate CloudFront Cache | |
run: | | |
CLOUDFRONT_ID=$(jq -r '."cipher-ec2-stack".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" | |
cat cipher-infra/ec2-outputs.json | |
exit 1 | |
fi |