Publishing to Netlify from a private, organization-owned repository

Published Aug 19, 2022

Netlify re­cently an­nounced that they’re lim­it­ing the build­ing of pri­vate, organization-​owned GitHub repos­i­to­ries to their Pro plan. Repos­i­to­ries for the free plan must now be ei­ther pub­lic or under a per­sonal GitHub ac­count.

I have a few pri­vate repos­i­to­ries that are hosted in a team on GitHub for col­lab­o­ra­tion pur­poses, but which don’t pro­duce any in­come. Luck­ily there is a rather easy workaround: build­ing using GitHub Ac­tions, and then pub­lish­ing to Netlify af­ter­wards.

Clean­ing up on Netlify

First thing we’ll need to do is dis­con­nect the project from the GitHub repos­i­tory on Netlify. This will leave Netlify happy, as they no longer have to spend build min­utes on the project, and let us stay on the free plan.

Nav­i­gate to the project you want to change on netlify and go to Site settings > Build & deploy > Continuous Deployment. Here you’ll find a “Man­age repos­i­tory” drop­down, from which you can un­link the project:

Dropdown for unlinking the project on Netlify

This should be enough for Netlify to be happy. While you’re here, you might as well find the in­for­ma­tion we’ll need for pub­lish­ing later: the site ID, and an ac­cess token.

Still within the project, head to Site settings > General > Site details and copy down the site ID. Then head to User settings > Applications and cre­ate a new per­sonal ac­cess token. Make sure to copy down the token value as well. We’ll be using both of these val­ues when we set up the GitHub Ac­tion.

Set­ting up the GitHub Ac­tion

The rest of the work will hap­pen in your GitHub repo. Head over to Settings > Secrets > Actions, and cre­ate a new se­cret con­tain­ing the ac­cess token you’ve just copied from Netlify. We’ll be using this se­cret for send­ing the data to Netlify once we’ve built the site.

The GitHub interface after adding the secret

Fi­nally all we have left to do is add the ac­tual GitHub Ac­tion! Cre­ate a new file in .github/workflows in your repos­i­tory (e.g. .github/workflows/release.yml), and paste an ac­tion con­fig­u­ra­tion like the fol­low­ing:

name: Release to Netlify
# I chose to publish on every commit to the primary branch for this project
# See for how to configure this
- master
runs-on: ubuntu-latest
contents: write
# setup
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
node-version: 16
cache: npm
- run: npm ci
- run: npm run build # replace with the way you build your project
# publish to netlify
- name: Publish to Netlify
uses: nwtgck/actions-netlify@v1.2
with: # change these settings as needed; for a full list, see the actions-netlify documentation
publish-dir: ./build
production-branch: master
production-deploy: true
github-token: ${{ secrets.GITHUB_TOKEN }}
deploy-message: "${{github.event.head_commit.message}}"
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_ACCESS_TOKEN }} # rename to whatever you called your secret on GitHub
NETLIFY_SITE_ID: "19c5eafb-aaaa-bbbb-cccc-dddddddddddd" # replace with your site ID

And that’s it! You should now have GitHub Ac­tions set up to build your site and pub­lish the re­sult to Netlify - even if your repos­i­tory wouldn’t nor­mally be sup­ported by your Netlify plan.