Production and Preview environment for one branch

Hi guys,
I’ve got a static site that uses content from a headless CMS. When the content is published, the webhook is sent to Netlify, and the build trigger runs a new production build from the master branch with my published content.

Also, my headless CMS’s got unpublished content available via a separate API endpoint with a secret API key. I can easily distinguish between those two and use the appropriate command to build my site using production or preview API. Also, I’ve got another webhook request that can trigger build after every content change in this preview environment.

How should I design this in Netlify? Basically, I need two builds/environments for the same master branch but with different build commands (and different production/preview URLs). Should I make two separate sites? One for production and one for preview content environment? Can I somehow design this with just one site - this means different build commands with their respective webhooks trigger with two different URLs (public and preview) but for just one master branch?

Thank you.

2 Likes

Hey @makma,
You won’t be able to have different build commands on the same branch, but would it be possible to have two copies of that same master branch within your repo? If so, a netlify.toml with different deploy contexts would do the trick- that would let you build with different commands. Here are our docs on deploy contexts:

Alternatively, you can set up two different Netlify sites, where both build from the same repo and branch- and you could name one preview-x.netlify.app, the other prod-x.netlify.app. This set up would also let you set up whatever build commands you wanted for each site.

Let us know if this helps or you if you have other questions!

2 Likes

Hey @jen! Thank you for the response. I’m glad you have suggested those two options. I just wanted to make sure I hadn’t missed anything. Thank you!

For a random viewer of this thread - here are my findings and guide on how to set up such an environment - in the tutorial, there is used Kontent.ai as headless CMS, however, all the approaches are applicable in general.

1 Like

Nice, thanks so much for sharing!