Try โ€‚โ€‰HackMD
YAMLException: end of the stream or a document separator is expected at line 8, column 15: - Run Terminal: ^

title: Nextjs deploy on github-pages
tags: App Hosting

Nextjs deploy on github-pages

There have two approaches on below

  • Run Terminal:
    Just run npm run deploy, setup in pakage.json.

  • Github Action:
    Add <action-name>.yml, when github detected specificed branch updated will deploy automictically.

Run Terminal

Setup

Add those file under your project root
setting <your-repo-name>

  • <project-root>/.env-config.js

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const prod = process.env.NODE_ENV === 'production';
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  module.exports = {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    'process.env.BACKEND_URL': prod ? '<your-repo-name>' : ''
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  };
    
  • next.config.js

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const debug = process.env.NODE_ENV !== 'production';
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  module.exports = {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    assetPrefix: !debug ? '<your-repo-name>' : ''
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  };
    
  • .babelrc.js

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  const env = require('./env-config');
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  module.exports = {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    presets: ['next/babel'],
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    plugins: [['transform-define', env]]
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  };
    

Add script in package.json

  • package.json

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹"scripts": {
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  "export": "next export",
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  "deploy": "rm -rf node_modules/.cache && next build && next export && touch out/.nojekyll && git add out/ && git commit -m \"Deploy Next.js to gh-pages\" && git subtree push --prefix out origin gh-pages"
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹},
    
  • If you don't want to custom your commit can update your script "deploy"

Run

โ€‹โ€‹โ€‹โ€‹    npm run deploy

Github Action

setup

Same as Run Terminal approac. here
THIS IS NECESSARY when you want deploy your nextjs project on github-pages

Add <action-name>.yml

  • <project-root>/.github/workflows/.<action-name>.yml

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  name: <Your-action-name>
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  on:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    push:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      branches: <The branch>    # <The branch> you want to dected
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  jobs:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹    deploy:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      runs-on: ubuntu-latest
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      steps:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - name: Checkout ๐Ÿ›Ž๏ธ
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          uses: actions/checkout@v2
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - name: Cache  ๐Ÿ’พ
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          uses: actions/cache@v2
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          with:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            path: ${{ github.workspace }}/.next/cache
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - run: rm -rf node_modules/.cache
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - run: npm ci
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - run: npm run build
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - run: npm run export
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - run: touch out/.nojekyll
    
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        - name: Deploy ๐Ÿš€
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          uses: peaceiris/actions-gh-pages@v3
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          with:
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            github_token: ${{ secrets.GITHUB_TOKEN }}
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            publish_dir: ./out
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹            force_orphan: true
    

Push <The branch>

โ€‹โ€‹โ€‹โ€‹git push origin <The branch>
  • set Link href as={process.env.BACKEND_URL + '/<page-name>'}

    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  import Link from 'next/link';
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  ...
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  return(
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹     <> 
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      <Link href="/about" as={process.env.BACKEND_URL + '/about'}>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        <a className="btn-primary">
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          <p>About</p>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        </a>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      </Link>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      <Link href="/note" as={process.env.BACKEND_URL + '/note'}>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        <a className="btn-primary btn-primary--2">
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹          <p>Note</p>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹        </a>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹      </Link>
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹     </> 
    โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹  )
    

My code: https://github.com/barrystone/barrystone.github.io
example: https://github.com/vercel/next.js/tree/canary/examples/gh-pages

Reference:

Github action Pakages: