Hello there,
For some reason my environment variables are not being picked up based on their appropriate context. The echoed $CONTEXT in my build command is showing the correct value but the scoped environment variable are never applied.
Based on the code given above, the only environmental variable that is successfully applied is build.environment. Any help would be greatly appreciated!
Do you have some specific deploys that we can look at for each of the above contexts? We could investigate with regards to those instead of trying to find out the deploys with the problem.
For sure, below is an example of a build where the environmental variables failed Netlify App
As you can see, the context is being determined properly but the scoped environmental variable was not being applied. I also updated my description with new information.
11:29:44 PM: Build ready to start
11:29:46 PM: build-image version: 3bcb38c35508b42e9121d4badfe6d8c66fd7a3f0
11:29:46 PM: build-image tag: v4.3.2
11:29:46 PM: buildbot version: fdfb4991fe9d90ac67dcc6a49b9c69b58441874a
11:29:46 PM: Fetching cached dependencies
11:29:46 PM: Starting to download cache of 253.7MB
11:29:49 PM: Finished downloading cache in 3.066656484s
11:29:49 PM: Starting to extract cache
11:29:59 PM: Finished extracting cache in 10.169972542s
11:29:59 PM: Finished fetching cache in 13.272196077s
11:29:59 PM: Starting to prepare the repo for build
11:29:59 PM: Preparing Git Reference refs/heads/master
11:30:00 PM: Parsing package.json dependencies
11:30:01 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'echo 'toml context & context.production' && echo $CONTEXT && REACT_APP_TEST_VALUE=special npm run build' versus 'npm run build' in the Netlify UI
11:30:01 PM: Starting build script
11:30:01 PM: Installing dependencies
11:30:01 PM: Python version set to 2.7
11:30:01 PM: Started restoring cached node version
11:30:04 PM: Finished restoring cached node version
11:30:04 PM: v16.11.1 is already installed.
11:30:05 PM: Now using node v16.11.1 (npm v8.0.0)
11:30:05 PM: Started restoring cached build plugins
11:30:05 PM: Finished restoring cached build plugins
11:30:05 PM: Attempting ruby version 2.7.2, read from environment
11:30:06 PM: Using ruby version 2.7.2
11:30:07 PM: Using PHP version 8.0
11:30:07 PM: Started restoring cached node modules
11:30:07 PM: Finished restoring cached node modules
11:30:07 PM: Started restoring cached go cache
11:30:07 PM: Finished restoring cached go cache
11:30:07 PM: go version go1.16.5 linux/amd64
11:30:07 PM: go version go1.16.5 linux/amd64
11:30:07 PM: Installing missing commands
11:30:07 PM: Verify run directory
11:30:09 PM: β
11:30:09 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:30:09 PM: Netlify Build
11:30:09 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:30:09 PM: β
11:30:09 PM: β― Version
11:30:09 PM: @netlify/build 18.17.1
11:30:09 PM: β
11:30:09 PM: β― Flags
11:30:09 PM: baseRelDir: true
11:30:09 PM: buildId: 6168f5a872e7830009f0c73b
11:30:09 PM: deployId: 6168f5a872e7830009f0c73d
11:30:09 PM: β
11:30:09 PM: β― Current directory
11:30:09 PM: /opt/build/repo
11:30:09 PM: β
11:30:09 PM: β― Config file
11:30:09 PM: /opt/build/repo/netlify.toml
11:30:09 PM: β
11:30:09 PM: β― Context
11:30:09 PM: production
11:30:09 PM: β
11:30:09 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:30:09 PM: 1. build.command from netlify.toml
11:30:09 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:30:09 PM: β
11:30:09 PM: $ echo 'toml context & context.production' && echo $CONTEXT && REACT_APP_TEST_VALUE=special npm run build
11:30:09 PM: toml context & context.production
11:30:09 PM: production
11:30:09 PM: > heatwav-admin@0.2.0 build
11:30:09 PM: > react-scripts build
11:30:11 PM: Creating an optimized production build...
11:31:15 PM: Compiled successfully.
11:31:15 PM:
11:31:15 PM: File sizes after gzip:
11:31:16 PM: Creating deploy upload records
11:31:15 PM: 472.97 KB build/static/js/2.e388f16d.chunk.js
11:31:15 PM: 28.38 KB build/static/css/2.629319f3.chunk.css
11:31:15 PM: 16.7 KB build/static/js/main.34cd593e.chunk.js
11:31:15 PM: 2.93 KB build/static/css/main.dd1d9abb.chunk.css
11:31:15 PM: 781 B build/static/js/runtime-main.9d000b1f.js
11:31:15 PM: The project was built assuming it is hosted at /.
11:31:15 PM: You can control this with the homepage field in your package.json.
11:31:15 PM: The build folder is ready to be deployed.
11:31:15 PM: You may serve it with a static server:
11:31:15 PM: npm install -g serve
11:31:15 PM: serve -s build
11:31:15 PM: Find out more about deployment here:
11:31:15 PM: https://cra.link/deployment
11:31:16 PM: β
11:31:16 PM: (build.command completed in 1m 6.8s)
11:31:16 PM: β
11:31:16 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:31:16 PM: 2. Deploy site
11:31:16 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:31:16 PM: β
11:31:16 PM: Starting to deploy site from 'build'
11:31:16 PM: Creating deploy tree
11:31:16 PM: 7 new files to upload
11:31:16 PM: 0 new functions to upload
11:31:16 PM: Site deploy was successfully initiated
11:31:16 PM: β
11:31:16 PM: (Deploy site completed in 553ms)
11:31:16 PM: β
11:31:16 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:31:16 PM: Netlify Build Complete
11:31:16 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:31:16 PM: β
11:31:16 PM: (Netlify Build completed in 1m 7.4s)
11:31:16 PM: Starting post processing
11:31:16 PM: Post processing - HTML
11:31:16 PM: Post processing - header rules
11:31:17 PM: Caching artifacts
11:31:17 PM: Started saving node modules
11:31:17 PM: Finished saving node modules
11:31:17 PM: Started saving build plugins
11:31:17 PM: Finished saving build plugins
11:31:16 PM: Post processing - redirect rules
11:31:17 PM: Started saving pip cache
11:31:17 PM: Post processing done
11:31:17 PM: Finished saving pip cache
11:31:17 PM: Started saving emacs cask dependencies
11:31:17 PM: Finished saving emacs cask dependencies
11:31:17 PM: Started saving maven dependencies
11:31:17 PM: Finished saving maven dependencies
11:31:17 PM: Started saving boot dependencies
11:31:17 PM: Site is live β¨
11:31:17 PM: Finished saving boot dependencies
11:31:17 PM: Started saving rust rustup cache
11:31:17 PM: Finished saving rust rustup cache
11:31:17 PM: Started saving go dependencies
11:31:17 PM: Finished saving go dependencies
11:31:17 PM: Build script success
11:32:14 PM: Finished processing build request in 2m28.083959212s
I noticed what while the scoped environment variables were not being applied, the scoped build command was. What I ended up doing was prepending the env vars in front of the build commands.
[context.production]
command = "URL='prod.com' npm run build'"
[context.branch-deploy]
command = "URL='stage.com' npm run build'"
I also noticed that the βcustom branch nameβ context was not working. I have a staging branch configured for branch deploy; however, during stage builds, the context is showing up as branch-deploy and not staging.
[context.staging] # <-- DOES NOT WORK
command = "URL='stage.com' npm run build'"
[context.branch-deploy] # <-- WORKS
command = "URL='dev.com' npm run build'"
Example branch build log:
11:15:40 PM: Waiting for other deploys from your team to complete. Check the queue: https://app.netlify.com/teams/fireteam99/builds
11:16:34 PM: Build ready to start
11:16:36 PM: build-image version: 3bcb38c35508b42e9121d4badfe6d8c66fd7a3f0
11:16:36 PM: build-image tag: v4.3.2
11:16:36 PM: buildbot version: fdfb4991fe9d90ac67dcc6a49b9c69b58441874a
11:16:36 PM: Fetching cached dependencies
11:16:37 PM: Starting to download cache of 244.3MB
11:16:37 PM: Finished downloading cache in 1.059213553s
11:16:37 PM: Starting to extract cache
11:16:46 PM: Finished extracting cache in 8.795980148s
11:16:46 PM: Finished fetching cache in 9.93734516s
11:16:46 PM: Starting to prepare the repo for build
11:16:47 PM: Preparing Git Reference refs/heads/staging
11:16:47 PM: Parsing package.json dependencies
11:16:48 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'echo 'toml context' && echo $CONTEXT && npm run build' versus 'npm run build' in the Netlify UI
11:16:48 PM: Starting build script
11:16:48 PM: Installing dependencies
11:16:48 PM: Python version set to 2.7
11:16:48 PM: Started restoring cached node version
11:16:51 PM: Finished restoring cached node version
11:16:51 PM: v16.11.1 is already installed.
11:16:52 PM: Now using node v16.11.1 (npm v8.0.0)
11:16:52 PM: Started restoring cached build plugins
11:16:52 PM: Finished restoring cached build plugins
11:16:52 PM: Attempting ruby version 2.7.2, read from environment
11:16:53 PM: Using ruby version 2.7.2
11:16:53 PM: Using PHP version 8.0
11:16:53 PM: Started restoring cached node modules
11:16:53 PM: Finished restoring cached node modules
11:16:54 PM: Started restoring cached go cache
11:16:54 PM: Finished restoring cached go cache
11:16:54 PM: go version go1.16.5 linux/amd64
11:16:54 PM: go version go1.16.5 linux/amd64
11:16:54 PM: Installing missing commands
11:16:54 PM: Verify run directory
11:16:55 PM: β
11:16:55 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:16:55 PM: Netlify Build
11:16:55 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:16:55 PM: β
11:16:55 PM: β― Version
11:16:55 PM: @netlify/build 18.17.1
11:16:55 PM: β
11:16:55 PM: β― Flags
11:16:55 PM: baseRelDir: true
11:16:55 PM: buildId: 6168f25b04f2a50007a67bd6
11:16:55 PM: deployId: 6168f25b04f2a50007a67bd8
11:16:55 PM: β
11:16:55 PM: β― Current directory
11:16:55 PM: /opt/build/repo
11:16:55 PM: β
11:16:55 PM: β― Config file
11:16:55 PM: /opt/build/repo/netlify.toml
11:16:55 PM: β
11:16:55 PM: β― Context
11:16:55 PM: branch-deploy
11:16:56 PM: β
11:16:56 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:16:56 PM: 1. build.command from netlify.toml
11:16:56 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:16:56 PM: β
11:16:56 PM: $ echo 'toml context' && echo $CONTEXT && npm run build
11:16:56 PM: toml context
11:16:56 PM: branch-deploy
11:16:56 PM: > heatwav-admin@0.2.0 build
11:16:56 PM: > react-scripts build
11:16:58 PM: Creating an optimized production build...
11:17:56 PM: Compiled successfully.
11:17:56 PM:
11:17:56 PM: File sizes after gzip:
11:17:56 PM: 472.97 KB build/static/js/2.80ebe5d0.chunk.js
11:17:56 PM: 28.38 KB build/static/css/2.629319f3.chunk.css
11:17:56 PM: 16.7 KB build/static/js/main.82d9fc17.chunk.js
11:17:56 PM: 2.93 KB build/static/css/main.dd1d9abb.chunk.css
11:17:56 PM: 781 B build/static/js/runtime-main.9d000b1f.js
11:17:56 PM: The project was built assuming it is hosted at /.
11:17:56 PM: You can control this with the homepage field in your package.json.
11:17:56 PM: The build folder is ready to be deployed.
11:17:56 PM: You may serve it with a static server:
11:17:56 PM: npm install -g serve
11:17:56 PM: serve -s build
11:17:56 PM: Find out more about deployment here:
11:17:56 PM: https://cra.link/deployment
11:17:56 PM: β
11:17:56 PM: (build.command completed in 1m 0.5s)
11:17:56 PM: β
11:17:56 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:17:56 PM: 2. Deploy site
11:17:56 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:17:56 PM: Creating deploy upload records
11:17:56 PM: Starting post processing
11:17:56 PM: Post processing - HTML
11:17:56 PM: Post processing - header rules
11:17:56 PM: Post processing - redirect rules
11:17:57 PM: Post processing done
11:17:57 PM: Site is live β¨
11:17:56 PM: β
11:17:56 PM: Starting to deploy site from 'build'
11:17:56 PM: Creating deploy tree
11:17:56 PM: 0 new files to upload
11:17:56 PM: 0 new functions to upload
11:17:56 PM: Site deploy was successfully initiated
11:17:56 PM: β
11:17:56 PM: (Deploy site completed in 134ms)
11:17:56 PM: β
11:17:56 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:17:56 PM: Netlify Build Complete
11:17:56 PM: ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
11:17:56 PM: β
11:17:56 PM: (Netlify Build completed in 1m 0.7s)
11:17:57 PM: Caching artifacts
11:17:57 PM: Started saving node modules
11:17:57 PM: Finished saving node modules
11:17:57 PM: Started saving build plugins
11:17:57 PM: Finished saving build plugins
11:17:57 PM: Started saving pip cache
11:17:57 PM: Finished saving pip cache
11:17:57 PM: Started saving emacs cask dependencies
11:17:57 PM: Finished saving emacs cask dependencies
11:17:57 PM: Started saving maven dependencies
11:17:57 PM: Finished saving maven dependencies
11:17:57 PM: Started saving boot dependencies
11:17:57 PM: Finished saving boot dependencies
11:17:57 PM: Started saving rust rustup cache
11:17:57 PM: Finished saving rust rustup cache
11:17:57 PM: Started saving go dependencies
11:17:57 PM: Finished saving go dependencies
11:17:57 PM: Build script success
11:18:42 PM: Finished processing build request in 2m5.732021986s
Hmmm I havenβt tried setting the key as [context.production.environment] nor have I tried setting the value as { REACT_APP_TEST_VALUE = "production" }. Did I need to append environment to context.production?
@coelmay the environmental variable issue has been fixed, thanks! Iβm still having an issue getting the staging context to show up though. Only branch-deploy shows up for some reason.
I donβt have any projects with multiple branches currently such that I can test.
However, as per previously linked sample file, context.staging comes after context.branch-deploy, so if you have these the opposite way, the latter is possibly overriding the former.
This is unrelated but I also noticed that my branch deploys arenβt automatically published. My production branch is being automatically published though. Is there any way to make my staging branch deploy auto publish?
If you click the βBranch Deployβ at the top of the deploy details, or on the Deploys page, it should take you to the branch url e.g. staging--netlify-site-name.netlify.app. This is in addition to the preview URL.
I can only assume this is simply how the build system logs out the data. It doesnβt (appear) to influence the build.
Hmm, even so it seems like anything i have under [context.staging] is not being applied but anything under [context.branch-deploy] is. Even though staging should override branch-deploy.
Regarding the auto deploy issue, I have the setting turned on:
However during branch builds I have to manually click βPublish deployβ after the build finishes. If I click βPreview deployβ before clicking βPublish deployβ I get lead to a link like: https://616a3352015fad00075f9e3b--heatwav-admin.netlify.app/login. Only after clicking βPublish deployβ does the preview lead to staging--heatwav-admin.netlify.app.
Did you try changing the order? E.G having staging after branch-deploy
[content.branch-deploy]
command = "..."
[context.branch-deploy.environment]
SOME_VARIABLE = "This is a standard branch deploy"
[content.staging]
command = "..."
[context.staging.environment]
SOME_VARIABLE = "This is a staging branch deploy"
Doing this publishes the branch to the production site. Automatically publishing branches to production is (generally) not wanted as they are (often) for testing purposes.
In your last image the link βBranch Deployβ (if you hover over it) will show staging branch name (e.g. staging--heatwav-admin.netlify.app) while the βPreview deployβ link will show the preview URL (e.g. 616a3352015fad00075f9e3b--heatwav-admin.netlify.app).
Did you try changing the order? E.G having staging after branch-deploy
Yeah I tried changing the order but didnβt have any luckβ¦
Doing this publishes the branch to the production site. Automatically publishing branches to production is (generally) not wanted as they are (often) for testing purposes.
I updated the netlify.toml in my staging branch, adding context.branch-deploy before. The context.staging settings were used rather than the context.branch-deploy (see deploy log and deploy preview.)
I then removed context.staging and the context.branch-deploy settings were used (see deploy log and deploy preview.)
NOTE: The difference between the two is the URL for Get the template.
If this is not occurring for you, can you share your repository, or a minimal replica, for testing.