Publish to Netlify via GitHub Actions
Netlify's 300 build minutes per-month can be a bit limiting if you regularly publish pages ad hoc to share with others. This workflow emulates the Netlify workflow as close as possible:
- Pull requests will get "deploy previews" created, at
p-$git_sha
. To find the URL you'll need to open the checks tab.
First, create a Netlify authentication token by navigating to the user menu -> User settings -> Applications and clicking New access token. Give it a descriptive name and click Generate token, then copy the token. Navigate to Settings -> Actions -> Secrets and add NETLIFY_AUTH_TOKEN
with this value.
Create .github/workflows/publish.yml
with the following content. There are a few things you may need to replace:
- If your "default" branch isn't master, be sure to replace all instances of
master
with its actual name. - In the
env
section, set:NETLIFY_SITE_ID
to the value you see under Site settings -> General -> Site details -> Site information -> API ID.SITE_URL
to something that resembles your Netlify site. Note that DNS subdomains are limited to just 63 characters in length, and a Git SHA is already 40 -- keep it short!SITE_URL_PRODUCTION
to your site's live address.
name: Publish
on:
push:
branches:
- master
pull_request:
branches:
- master
env:
NETLIFY_SITE_ID: 1234abcd-12ab-34cd-56ef-123456abcdef
ALIAS_NON_PRODUCTION: p-${{ github.sha }}
SITE_URL: https://p-${{ github.sha }}--my-site.netlify.app
SITE_URL_PRODUCTION: https://example.com
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
# Make sure the actual branch is checked out when running on pull requests
ref: ${{ github.head_ref }}
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: '16'
cache: npm
- name: Cache Next.js template
uses: actions/cache@v3
with:
path: |
.next/
!.next/node_modules/
key: nextjs-template-${{ runner.os }}
- name: Install npm dependencies
run: npm install
- name: Set site URL (production)
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
run: |
set -eu
echo "SITE_URL=${SITE_URL_PRODUCTION}" >>$GITHUB_ENV
- name: Build
run: |
set -e
npx -y -- dendron publish init
npx -- dendron publish export --overrides="siteUrl=${SITE_URL}"
- name: Publish (non-production)
if: github.ref != 'refs/heads/master' || github.event_name != 'push'
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
run: npm exec -- netlify deploy --alias "$ALIAS_NON_PRODUCTION" --dir .next/out/
- name: Publish (production)
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
run: npm exec -- netlify deploy --prod --dir .next/out/
Backlinks