Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Branch specific environment variables on Netlify

We're using Netlify's automatic deployment for all pushed git branches.

We want to include our analytics scripts (et al) only for the master branch, i.e. the version of the website that our users are visiting.

It's possible to build environment variables on Netlify, but I don't get if it's possible to differentiate variables for certain branches?

like image 397
Fellow Stranger Avatar asked Apr 08 '18 17:04

Fellow Stranger


People also ask

How do I set environment variables in Netlify?

Site settings > Build & deploy > Environment > Environment variables , to be exact, ooh or Team settings > Sites > Global site settings > Shared environment variables if you're the "sharing is caring" type. These can also be set using the Netlify configuration file, netlify. toml .

Are Netlify environment variables secure?

Setting variables in our UI may be considered a bit more secure, since only people with access to the Netlify admin UI for your site can see them, rather than “anyone who can clone your repo!”.

How do you name environment variables?

Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit.

What is base directory in Netlify?

Base directory: optional field for linking monorepos or sites built from a subdirectory of a repository. It specifies the directory that our buildbot changes to before starting a build. It's where our build system checks for dependency management files such as package. json or . nvmrc .


1 Answers

There is a way to setup environment variables based on the deploy context in Netlify in your netlify.toml file. This is being used in a production site using Hugo, but you can use any keys you want for the variables and commands.

An example netlify.toml

# Global settings applied to the whole site.
[build]
  command = "yarn build"
  publish = "public"

# build a preview of the site (Drafts and Future dates also)
[context.deploy-preview]
  command = "yarn build:preview"

[build.environment]
  HUGO_ENV = "development"

[context.production.environment]
  HUGO_VERSION = "0.29"
  HUGO_ENV = "production"
# you can lock a version of hugo for a deploy preview
[context.deploy-preview.environment]
  HUGO_VERSION = "0.29"
  HUGO_ENV = "deploy" # TEST: should be deploy or something other than production
# you can lock a version of hugo for a branch-deploy (other than previews)
[context.branch-deploy.environment]
  HUGO_VERSION = "0.30"
  HUGO_ENV = "development"

Also target a specific branch (example: new-branch)

# build a preview of the site (Drafts and Future dates also)
[context.new-branch]
  command = "yarn build:preview"

# you can also target a specific branch
[context.new-branch.environment]
  HUGO_VERSION = "0.29"
  HUGO_ENV = "deploy" # TEST: should be deploy or something other than production

Solution: Now there will be an environment variable called HUGO_ENV that will have a value to know the context (production, development, deploy) defined. The build language can now access those variables to make decisions on what to include in the build results.

NOTE:

  • Use any env variable name and values you need. The example targets the Hugo static site generator which has a function getenv to retrieve the value.
  • I have not tested how using context.branch-deploy affects targeting a custom branch, so be careful for overrides of those contexts.
  • Any variables specified in the netlify.toml overwrite the environment variables entered into the browser console on the Netlify site.
like image 121
talves Avatar answered Sep 18 '22 14:09

talves