[S3] Deploying a Next.js SSG App to S3 with GitHub Actions
GHA
What is SSG again?
SSG (Static Site Generation) builds HTML at build time, not per request. It means fast page loads and zero server costs! This is ideal for S3 hosting and hosting static sites affordably.
Update next.config.js
Tell Next.js to export the app as a static site.
1// next.config.js2const nextConfig = {3 output: 'export',4}56module.exports = nextConfig
GitHub Actions Workflow
Automate the deployment with GitHub Actions. This workflow builds the Next.js app and uploads it to S3.
1name: Deploy to S32on:3 push:4 branches: ['main']56permissions:7 actions: write8 contents: read9 id-token: write1011jobs:12 upload:13 runs-on: ubuntu-latest1415 steps:16 - name: Checkout17 uses: actions/checkout@v41819 - name: Configure AWS credentials (OIDC)20 uses: aws-actions/configure-aws-credentials@v421 with:22 role-to-assume: arn:aws:iam::<your-account-id>:role/<your-gha-role>23 aws-region: ap-northeast-12425 - name: Setup Node.js26 uses: actions/setup-node@v427 with:28 node-version: 202930 - name: Cache node modules31 id: cache-npm32 uses: actions/cache@v433 env:34 cache-name: cache-node-modules35 with:36 path: ~/.npm37 key: \${{ runner.os }}-build-\${{ env.cache-name }}-\${{ hashFiles('**/package-lock.json') }}38 restore-keys: |39 \${{ runner.os }}-build-\${{ env.cache-name }}-4041 - name: Install Dependencies42 run: npm ci4344 - name: Build45 run: npm run build4647 - name: Deploy to s348 run: |49 aws s3 cp --recursive ./out/ s3://your-static-site-bucket-name --acl public-read
Notes
- Don't use
getServerSideProps()
— onlygetStaticProps()
or plain pages. - Dynamic routes won't work unless they're pre-rendered.
- S3 doesn't handle client-side routing. Might need CloudFront + fallback if routing breaks.