A GitHub action to build and export a NextJS site statically, allowing the site to be deployed with GitHub pages, or served via a web server such as Nginx
Note: With the very limited information of NextJS that I have, deploying with this method does not allow using all features that NextJS offers. The recommended way of deploying is using Vercel. All caveats are listed in this documentation
This action is heavily plagarized from pranitbauva1997/zola-deploy-action, which builds and deploys websites using the Zola static site generator.
This example will build and export on push to any branch, then deploy to branch specified in PAGES_BRANCH (build
by default)
on: push
name: Build, export and publish to branch on push
jobs:
build-export-and-deploy:
name: Build, export and publish sample site to the `sample-site-build` branch
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build with the latest action rev
uses: tsg-iitkgp/[email protected]
env:
BUILD_DIR: .
PAGES_BRANCH: sample-site-build
TOKEN: ${{ secrets.TOKEN }}
This example will build and deploy on master branch to gh-pages branch. Additionally will build only on pull requests.
on:
push:
branches:
- master
pull_request:
jobs:
build:
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/master'
steps:
- name: 'Checkout'
uses: actions/checkout@v2
- name: 'Build only'
uses: tsg-iitkgp/[email protected]
env:
BUILD_DIR: .
BUILD_ONLY: true
build_and_deploy:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
steps:
- name: 'Checkout'
uses: actions/checkout@v2
- name: 'Build and deploy'
uses: tsg-iitkgp/[email protected]
env:
BUILD_DIR: .
PAGES_BRANCH: sample-site-build
TOKEN: ${{ secrets.TOKEN }}
-
TOKEN
: Personal Access key with the appropriate scope. If the repository is public thepublic_repo
scope suffices, for private repositories the fullrepo
scope is required. We need this to push the site files back to the repo.( Actions already provides a
GITHUB_TOKEN
which is an installation token and does not trigger a GitHub Pages builds hence we need a personal access token )
PAGES_BRANCH
: The git branch of your repo to which the built static files will be pushed. Default isbuild
branchREPOSITORY
: The target repository to push to. Default isGITHUB_REPOSITORY
(current repository). Set this variable if you want to deploy to other repo.BUILD_DIR
: The path from the root of the repo where we should run theyarn build
command. Default is.
(current directory)BUILD_ONLY
: Set to valuetrue
if you don't want to deploy afteryarn build
.GITHUB_HOSTNAME
: The Github hostname to use in your action. This is to account for Enterprise instances where the base URL differs from the default, which isgithub.com
.OUTPUT_DIR
: The output directory for published HTML/CSS/JS files. Defaults toout
for NextJS projects