How to retry preview build after branch environment variables have been set

I have an application where we sometimes create staging/preview environments based on GitHub branches. During the setup, we run a script that creates environment variables for the site using the Netlify API. The problem I’m running into is that I have to manually click “Retry with latest branch commit” in the Netlify UI in order for the preview site to have the correct environment variables in the build.

It’s easy to forget to do this, so I was hoping there was a way to automate this last part. I didn’t see anything in the Netlify API that seemed related to “Retry with latest branch commit” and I don’t see any way to automatically rebuild a Netlify deploy preview when the environment variable changes.

Is there a way to automatically retry a deploy after setting environment variables for the associated branch?

You simply need to make a POST request to:

/deploys/<deploy-id>/retry

I’ll give that a try. Am I looking at the right docs? I don’t see that mentioned here Netlify API documentation or here Get started with the Netlify API | Netlify Docs