Build: Add site-specific environment variables in local builds

Created on 14 Feb 2020  路  12Comments  路  Source: netlify/build

This issue is part of the bigger issue at #667

Site-specific environment variables (git, build, etc.) are missing in local builds (Netlify CLI and programmatically):

  • BRANCH
  • BUILD_ID
  • CACHED_COMMIT_REF
  • COMMIT_REF
  • CONTEXT
  • DEPLOY_ID
  • DEPLOY_PRIME_URL
  • DEPLOY_URL
  • HEAD
  • NETLIFY_IMAGES_CDN_DOMAIN
  • PULL_REQUEST
  • REPOSITORY_URL
  • REVIEW_ID
  • URL

My first question is whether the list above is correct?

My second question is for @netlify/backend: how to retrieve the information above? I know the getSite API endpoint can be used, but does it include all that information? Can someone pinpoints which property is used for each environment variable? Are some of the environment variables using some conditional logic to find out their value?

Thanks!

enhancement

All 12 comments

Those are special variables created for the CI environment. All but REPOSITORY_URL and NETLIFY_IMAGES_CDN_DOMAIN refer to specific things for a git-triggered build. Won't be applicable for a local build. They'll all be empty.

Yeah a lot of these we would populate with local data from Git, and we can infer others like URL. @ehmicky we should determine which ones have no application for local development, Eg., PULL_REQUEST, and then figure out how to infer the rest.

Sent with GitHawk

I think some information might still make sense though even when the build was triggered locally (Netlify CLI or programmatically)?

Here are some ideas:

  • BRANCH, HEAD: get the local git information using the git binary
  • COMMIT_REF: same as HEAD
  • CACHED_COMMIT_REF: COMMIT_REF's parent commit
  • CONTEXT: use a new context called 'local'
  • NETLIFY_IMAGES_CDN_DOMAIN, URL, REPOSITORY_URL: retrieved from the getSite API endpoint (I would need some help from you to figure out where to find it in the API response :pray:)
  • BUILD_ID, DEPLOY_ID, DEPLOY_PRIME_URL, DEPLOY_URL, REVIEW_ID, PULL_REQUEST: those ones might not make sense locally since we aren't creating a CI build, so probably should be left undefined

Could you please check the list and ensure this would make sense? Thanks!

NETLIFY_IMAGES_CDN_DOMAIN, URL, REPOSITORY_URL: retrieved from the getSite API endpoint (I would need some help from you to figure out where to find it in the API response 馃檹)

URL is currently available as the url field of getSite response.
REPOSITORY_URL is set via buildbot. You should have it locally, assuming it's run from a cloned repo . Otherwise, the HTTP version is available from getSite as build_settings.repo_url. The git:// one is not available.
NETLIFY_IMAGES_CDN_DOMAIN is a bit trickier. I'll have to get back to you on that one. Might not be relevant in local env.

Thanks for the information @kitop!
Do you feel like the other variables make sense otherwise?

Yup! They make sense to me!

Great! Unless @erquhart or @DavidWells have any additional feedback, I think this issue is ready for development.

To-do list:

  • [x] CONTEXT (#784)
  • [x] BRANCH, HEAD (#785)
  • [x] COMMIT_REF, CACHED_COMMIT_REF (#785)
  • [x] NETLIFY_IMAGES_CDN_DOMAIN, URL, REPOSITORY_URL

Most of this issue is done except for NETLIFY_IMAGES_CDN_DOMAIN, URL and REPOSITORY_URL which require calling the getSite API endpoint.

I believe the backend work on the API token passed to Netlify Build (https://github.com/netlify/bitballoon/issues/4980) should be done first to complete that item @DavidWells @kitop?

I believe the backend work on the API token passed to Netlify Build (netlify/bitballoon#4980) should be done first to complete that item @DavidWells @kitop?

Why's that? Isn't this for running build in local CLI (ie, user's machine)? If so, it should have a user token already.

Yes you're right @kitop! :+1:

We don't need NETLIFY_IMAGES_CDN_DOMAIN

URL is important. ( REPOSITORY_URL too but less so)

CLI should be able to pass in this value

We can inject the site ENV variables into the netlify build CLI command like so https://github.com/netlify/cli/blob/master/src/commands/dev/exec.js#L17

This needs to be added into https://github.com/netlify/cli/blob/master/src/commands/build/index.js

I'm unclear on how those values pass into the @netlify/build library

I will leave that up to you.

Was this page helpful?
0 / 5 - 0 ratings