Deploy Portfolio #13
Workflow file for this run
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 Portfolio | |
on: | |
push: | |
branches: | |
- main | |
paths-ignore: | |
- "infrastructure/**" | |
- "**.md" | |
workflow_dispatch: | |
concurrency: | |
group: "deploy" | |
cancel-in-progress: true | |
env: | |
AWS_REGION: us-east-1 | |
NEXT_PUBLIC_BASE_URL: https://bjornmelin.io | |
NEXT_PUBLIC_API_URL: https://api.bjornmelin.io | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
id-token: write | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Setup Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: "20" | |
cache: "yarn" | |
cache-dependency-path: "**/yarn.lock" | |
- 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: ${{ env.AWS_REGION }} | |
# Install root dependencies (i.e., for Next.js) | |
- name: Install Root Dependencies | |
run: yarn install --frozen-lockfile | |
# Install dependencies in the infrastructure directory (if needed) | |
- name: Install Infrastructure Dependencies | |
working-directory: ./infrastructure | |
run: yarn install --frozen-lockfile | |
# Build the Next.js app & export static files | |
- name: Build Next.js App (Static Export) | |
env: | |
NEXT_PUBLIC_BASE_URL: ${{ env.NEXT_PUBLIC_BASE_URL }} | |
NEXT_PUBLIC_API_URL: ${{ env.NEXT_PUBLIC_API_URL }} | |
run: yarn build | |
# Retrieve the S3 bucket name and CloudFront distribution from your CF stack | |
- name: Get Stack Outputs | |
id: stack-outputs | |
continue-on-error: true | |
run: | | |
BUCKET_NAME=$(aws cloudformation describe-stacks \ | |
--stack-name prod-portfolio-storage \ | |
--query 'Stacks[0].Outputs[?OutputKey==`WebsiteBucketName`].OutputValue' \ | |
--output text --region ${{ env.AWS_REGION }}) | |
DIST_ID=$(aws cloudformation describe-stacks \ | |
--stack-name prod-portfolio-storage \ | |
--query 'Stacks[0].Outputs[?OutputKey==`DistributionId`].OutputValue' \ | |
--output text --region ${{ env.AWS_REGION }}) | |
echo "bucket=$BUCKET_NAME" >> $GITHUB_OUTPUT | |
echo "distribution=$DIST_ID" >> $GITHUB_OUTPUT | |
- name: Check if Stack Outputs were retrieved | |
if: steps.stack-outputs.outcome != 'success' | |
run: | | |
echo "Failed to retrieve stack outputs. Check your user permissions and make sure 'prod-portfolio-storage' stack exists." | |
exit 1 | |
# Upload the exported static files to your S3 bucket | |
- name: Upload to S3 | |
run: | | |
echo "Uploading static files to s3://${{ steps.stack-outputs.outputs.bucket }}" | |
aws s3 sync out/ s3://${{ steps.stack-outputs.outputs.bucket }} --delete | |
# Invalidate CloudFront cache so your changes go live | |
- name: Invalidate CloudFront | |
run: | | |
aws cloudfront create-invalidation \ | |
--distribution-id ${{ steps.stack-outputs.outputs.distribution }} \ | |
--paths "/*" | |
# Verify the site is live at your real domain | |
- name: Verify Deployment | |
run: | | |
HTTP_CODE=$(curl -o /dev/null -s -w "%{http_code}" https://bjornmelin.io) | |
if [ "$HTTP_CODE" != "200" ]; then | |
echo "Website verification failed at https://bjornmelin.io with HTTP code: $HTTP_CODE" | |
exit 1 | |
fi | |
echo "Deployment verified. Received HTTP 200 from https://bjornmelin.io" |