GitHub Action
Netlify Next.js Deploy
This is a simple GitHub Action to deploy a next.js website to Netlify.
To use a GitHub action you can just reference it on your Workflow file (for more info check this article by Github)
name: "My Workflow"
on:
release:
types: [published]
jobs:
deploy:
name: "Deploy to Netlify"
steps:
- uses: veloii/[email protected]
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.MY_TOKEN_SECRET }}
NETLIFY_DEPLOY_TO_PROD: true
As most GitHub actions, this action requires and uses some inputs, that you define in your workflow file.
The inputs this action uses are:
Name | Required | Default | Description |
---|---|---|---|
NETLIFY_AUTH_TOKEN |
true |
N/A | The token needed to deploy your site (generate here) |
NETLIFY_SITE_ID |
true |
N/A | The site to where deploy your site (get it from the API ID on your Site Settings) |
NETLIFY_DEPLOY_MESSAGE |
false |
'' | An optional deploy message |
NETLIFY_DEPLOY_TO_PROD |
false |
false |
Should the site be deployed to production? |
deploy_alias |
false |
'' | (Optional) Deployed site alias |
The outputs for this action are:
NETLIFY_OUTPUT
Full output of the action
NETLIFY_PREVIEW_URL
The url of deployment preview.
NETLIFY_LOGS_URL
The url of the logs.
NETLIFY_LIVE_URL
The url of the live deployed site.
You can setup repo secrets to use in your workflows
name: "Netlify Deploy"
on:
release:
types: ["published"]
jobs:
deploy:
name: "Deploy"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: veloii/[email protected]
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "Prod deploy v${{ github.ref }}"
NETLIFY_DEPLOY_TO_PROD: true
name: "Netlify Preview Deploy"
on:
pull_request:
types: ["opened", "edited", "synchronize"]
jobs:
deploy:
name: "Deploy"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: veloii/[email protected]
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
Will deploy branches as https://${branchName}--${siteName}.netlify.app
.
An action is used to extract the branch name to avoid fiddling with refs/
. Finally, a commit status check is added, linking to the deployed site.
Only the default branch is built for simplicity. Use a similar workflow or standard Netlify integration for the production deployment.
name: "Netlify Previews"
on:
push:
branches-ignore:
- master
jobs:
deploy:
name: "Deploy"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
# Sets the branch name as environment variable
- uses: nelonoel/[email protected]
- uses: veloii/[email protected]
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
deploy_alias: ${{ env.BRANCH_NAME }}
# Creates a status check with link to preview
- name: Status check
uses: Sibz/[email protected]
with:
authToken: ${{ secrets.GITHUB_TOKEN }}
context: Netlify preview
state: success
target_url: ${{ env.NETLIFY_PREVIEW_URL }}
In case of already having the deployment ready data - we can easily skip the nvm, install and build part via passing:
- name: Deploy to Netlify
uses: veloii/[email protected]
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "Deployed from GitHub action"
NETLIFY_DEPLOY_TO_PROD: true