Deploy with CLI to branch

I’d like to use the Netlify CLI to deploy my site to a specific branch. Is this actually possible?

Nope! Branch based builds require a git build. Your options for CLI and API and ZIP deploys are draft, or production.

@fool Are there any plans to allow this through the cli or api?

I have the following use case. We want to automatically deploy release branches when a new branch is pushed to our Github repo. Our application has a backend API, and each release branch needs to be configured to talk to the appropriate backend (deployed outside of Netlify). For example, branch release/1-0-0 needs to point to a backend API_URL which lives at a location like this: https://1-0-0.backend.ourdomain.com.

We set this value through the APP_URL environment variable. Because environment variables set through Netlify are static, we can’t build through the Netlify CI system. Instead, we run our build outside of Netlify so we can dynamically set the API_URL. Then we deploy with the CLI. It looks something like this:

export API_URL=https://1-0-0.backend.ourdomain.com
yarn generate
netlify link --id
netlify deploy

This is done within Github Actions. The problem is netlify deploy deploys a draft. The draft has a Netlify URL, but we’d like to use a custom subdomain, which isn’t supported for drafts. As a result, I started looking at how to do this through branch deploys. My plan was something like the following:

  • Build within Github Actions and do a branch deploy (we have all the git information present during the build and can pass whatever is necessary to the API)
  • Call the Netlify API to create the DNS record (We’re using Netlify DNS for our subdomain)
  • Add a custom wildcard certificate so Netlify doesn’t need to regenerate the cert for each new branch.

It sounds like since there is no way to initiate a branch deploy from outside the Netlify CI process (cli or api), we’re stuck. Is there another way people are accomplishing this?

Because environment variables set through Netlify are static, we can’t build through the Netlify CI system.

Sorry, what makes you think that? You can change an environment variable’s settings at build time to whatever you want. For instance:

#!/bin/sh
# the script we use to build at Netlify, works automatically for all branches that follow the specified convention
export API_URL="https://${BRANCH}.backend.ourdomain.com"
yarn generate

Of course after build, it is THEN locked in place - but that isn’t “different” between builds from different locations - CLI and builds we do both have that limitation/condition :slight_smile:

I think you can solve this within the current system so let me know how my proposal falls short of meeting your needs, and I’ll try to come up with a workflow that can work within our CI.

I do not know the answer to your “are there plans…” but as a pragmatic person, I just want to get this working for you today, since the presently-available-features are already here and any potential features are not yet :slight_smile:

Assuming we can’t find a solution, I’ll see if we have roadmap plans around that. I don’t think there are any, or I would have started there!

@fool Ugh, that completely makes sense and would solve my issue! I’ll give it a try and circle back here later today with an update.

Thanks.

Thanks, that solved my problem. Not sure why that didn’t occur to me, but I appreciate the quick assistance.