First build breaks for imported projects that require environment variables

Probably due to the recent changes regarding environment variables, it is no longer possible to import new project from Github, and have the first build complete successfully, if they require environment variables to work.

Steps:

  • Add new site > Import an existing project
  • Link to a Github repository
  • Show advanced
  • Add the required environment variable
  • Press the “Deploy site” button

The first build will fail. If you hit the “Retry with latest branch commit”, the second build will work.

Again this is probably because the createSite endpoint (Netlify API documentation) does no longer support setting environment variables. The build starts without them, and calling the createEnvVars (Netlify API documentation) endpoint immediately after the createSite call is already too late, as the build already started.

We use this exact flow at DatoCMS, and this is currently impacting all of our users trying to run our demos. It seems that also your “Deploy to Netlify” button is having the same issues.

Please let us know how to proceed, thanks!

The solution is to launch the createSite method with the (undocumented) no_initial_build: true parameter (so that it doesn’t start the build YET), then adding the env variables with the createEnvVars method, and finally manually triggering the build with the (undocumented) createBuild method.

thank you for sharing your solution! This is definitely helpful for other users