Deploy to Netlify where the first build will fail (by design)

So I have a project the requires the user to have some API credentials/info in an environmental variable for the build to work. This means when deployed via the ‘Deploy to Netlify’ button (via***), the build will fail the first time. That’s not a problem, as I was expecting I’d be able to add the necessary environment in Netlify settings and then manually trigger the build, but this doesn’t seem possible? Once the build fails the first time, the created repository is empty and you can’t trigger the build/deploy again? Is there any way around this?

Edit: Just saw that Netlify lets you add environmental variables when you’re creating a new site from git, but this feature is missing when going the ‘Deploy to Netlify’ button route. Having that feature would’ve been a nice solution and it would complete the build the first time :confused:

Hey @intricatebug, welcome to the forum!

It sounds like you may have already done this, but just in case you haven’t: I think you’d have to skip the “Deploy to Netlify” button and instead do that process manually: fork the repo, create a Netlify site that’s linked to the fork, the first deploy will fail, but you can then add environment variables and deploy again. If you want to share the repo you’re trying to deploy, we can try to help walk you through?

I hear you that it would be nice to incorporate env vars with the button somehow, but I’m not sure how that would work… would you set the vars as query parameters in the browser’s URL bar? Or would the button take you to a screen that asks if you have any env variables? Something else? :face_with_monocle:

Hey @jen! Yes, this. The ‘New site from Git’ flow already has this:

So the same could be added to this screen, which is from the “Deploy to Netlify” flow:
(could be behind an Advanced settings button, like in the other flow)

Thanks for the suggestion! I’ve passed it on to our product team.

This post is old, but since I just discovered it, I thought I’d post a reply, at least for the benefit of future readers.

@intricatebug, since the build relies on certain environment variables, I’m guessing you must know the names of the variables, correct? In that case, you can configure a template for environment variables in the netlify.toml file. This will create form fields for users to complete during the Deploy to Netlify flow.

If you know the variable values, too (for example, if the link is auto-generated from a logged-in user interface), you can pre-fill them by adding them to the hash string of the button URL.

You can give the environment variable UI template a try by using the sample button in the docs:

Hope that helps!

1 Like

Hi @verythorough, this is exactly the type of solution I was looking for, thanks!

1 Like