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.
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]]
โโโโโโ };
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"
โโโโ npm run deploy
Same as Run Terminal approac. here
THIS IS NECESSARY when you want deploy your nextjs project on github-pages
<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
โโโโ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: