Skip to content

Commit ccb079a

Browse files
authored
fix: match the branch-deploy context when using --context branch:foo (#7235)
* fix: match `branch-deploy` context when using `--context branch:foo` The `--context` parameter can be passed to `dev`, `serve`, `functions:serve`, and `deploy`. As documented, its value can be a valid context (`all', 'production', 'deploy-preview', 'branch-deploy', 'dev`) or `branch:foo` where `foo` is the name of a branch. In the former case, we match env vars with a matching `context`. In the latter case, the intent is to match env vars with `context: "branch"` and a `context_parameter` matching the branch name (`foo`). All these behaviours are working as intended. However, in addition to these, it's expected that using `--context branch:foo` implicitly inherits the `branch-deploy` context as well (although a matching `branch` context value should take precedence). This was not implemented. You might think this is unnecessary: if you don't need the override, just use `--context branch-deploy`. However, consider multiple environment variables, some of which have branch-specific overrides, some of which do not. There is currently no mechanism available to make this work as expected. This commit fixes that, by simply adding the missing logic. * fix(docs): improve inline help and docs for `--context` * fix: use smart quotes in build --context docs docs * fix: fix typo in deploy --context docs
1 parent 2172a38 commit ccb079a

File tree

16 files changed

+257
-49
lines changed

16 files changed

+257
-49
lines changed

docs/commands/build.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ netlify build
1717

1818
**Flags**
1919

20-
- `context` (*string*) - Specify a build context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev")
20+
- `context` (*string*) - Specify a deploy context for environment variables read during the build (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT or ”production”)
2121
- `dry` (*boolean*) - Dry run: show instructions without running them
2222
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
2323
- `debug` (*boolean*) - Print debugging information
@@ -28,6 +28,8 @@ netlify build
2828

2929
```bash
3030
netlify build
31+
netlify build --context deploy-preview # Build with env var values from deploy-preview context
32+
netlify build --context branch:feat/make-it-pop # Build with env var values from the feat/make-it-pop branch context or branch-deploy context
3133
```
3234

3335

docs/commands/deploy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ netlify deploy
8787
- `alias` (*string*) - Specifies the alias for deployment, the string at the beginning of the deploy subdomain. Useful for creating predictable deployment URLs. Avoid setting an alias string to the same value as a deployed branch. `alias` doesn’t create a branch deploy and can’t be used in conjunction with the branch subdomain feature. Maximum 37 characters.
8888
- `branch` (*string*) - Serves the same functionality as --alias. Deprecated and will be removed in future versions
8989
- `build` (*boolean*) - Run build command before deploying
90-
- `context` (*string*) - Context to use when resolving build configuration
90+
- `context` (*string*) - Specify a deploy context for environment variables read during the build (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)
9191
- `dir` (*string*) - Specify a folder to deploy
9292
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
9393
- `functions` (*string*) - Specify a functions folder to deploy

docs/commands/dev.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ netlify dev
2020
**Flags**
2121

2222
- `command` (*string*) - command to run
23-
- `context` (*string*) - Specify a deploy context or branch for environment variables (contexts: "production", "deploy-preview", "branch-deploy", "dev")
23+
- `context` (*string*) - Specify a deploy context for environment variables (production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)
2424
- `country` (*string*) - Two-letter country code (https://ntl.fyi/country-codes) to use as mock geolocation (enables --geo=mock automatically)
2525
- `dir` (*string*) - dir with static files
2626
- `edge-inspect` (*string*) - enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format
@@ -49,7 +49,9 @@ netlify dev
4949
netlify dev
5050
netlify dev -d public
5151
netlify dev -c "hugo server -w" --target-port 1313
52-
netlify dev --context production
52+
netlify dev --context production # Use env var values from production context
53+
netlify dev --context deploy-preview # Use env var values from deploy-preview context
54+
netlify dev --context branch:feat/make-it-pop # Use env var values from the feat/make-it-pop branch context or branch-deploy context
5355
netlify dev --edge-inspect
5456
netlify dev --edge-inspect=127.0.0.1:9229
5557
netlify dev --edge-inspect-brk
@@ -74,7 +76,7 @@ netlify dev:exec
7476

7577
**Flags**
7678

77-
- `context` (*string*) - Specify a deploy context or branch for environment variables (contexts: "production", "deploy-preview", "branch-deploy", "dev")
79+
- `context` (*string*) - Specify a deploy context for environment variables (production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)
7880
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
7981
- `debug` (*boolean*) - Print debugging information
8082
- `auth` (*string*) - Netlify auth token - can be used to run this command without logging in
@@ -83,6 +85,8 @@ netlify dev:exec
8385

8486
```bash
8587
netlify dev:exec npm run bootstrap
88+
netlify dev:exec --context deploy-preview npm run bootstrap # Run with env var values from deploy-preview context
89+
netlify dev:exec --context branch:feat/make-it-pop npm run bootstrap # Run with env var values from the feat/make-it-pop branch context or branch-deploy context
8690
```
8791

8892
---

docs/commands/env.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ netlify env:get
8787

8888
**Flags**
8989

90-
- `context` (*string*) - Specify a deploy context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev")
90+
- `context` (*string*) - Specify a deploy context for environment variables (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch
9191
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
9292
- `json` (*boolean*) - Output environment variables as JSON
9393
- `debug` (*boolean*) - Print debugging information
@@ -99,7 +99,7 @@ netlify env:get
9999
```bash
100100
netlify env:get MY_VAR # get value for MY_VAR in dev context
101101
netlify env:get MY_VAR --context production
102-
netlify env:get MY_VAR --context branch:staging
102+
netlify env:get MY_VAR --context branch:feat/make-it-pop # get value in the feat/make-it-pop branch context or branch-deploy context
103103
netlify env:get MY_VAR --scope functions
104104
```
105105

@@ -139,7 +139,7 @@ netlify env:list
139139

140140
**Flags**
141141

142-
- `context` (*string*) - Specify a deploy context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev")
142+
- `context` (*string*) - Specify a deploy context for environment variables (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: all contexts)
143143
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
144144
- `json` (*boolean*) - Output environment variables as JSON
145145
- `scope` (*builds | functions | post-processing | runtime | any*) - Specify a scope
@@ -152,7 +152,7 @@ netlify env:list
152152
```bash
153153
netlify env:list # list variables with values in the dev context and with any scope
154154
netlify env:list --context production
155-
netlify env:list --context branch:staging
155+
netlify env:list --context branch:feat/make-it-pop # list variables with values in the feat/make-it-pop branch context or branch-deploy context
156156
netlify env:list --scope functions
157157
netlify env:list --plain
158158
```
@@ -175,7 +175,7 @@ netlify env:set
175175

176176
**Flags**
177177

178-
- `context` (*string*) - Specify a deploy context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev") (default: all contexts)
178+
- `context` (*string*) - Specify a deploy context for environment variables (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: all contexts)
179179
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
180180
- `force` (*boolean*) - Bypasses prompts & Force the command to run.
181181
- `json` (*boolean*) - Output environment variables as JSON
@@ -189,7 +189,8 @@ netlify env:set
189189
```bash
190190
netlify env:set VAR_NAME value # set in all contexts and scopes
191191
netlify env:set VAR_NAME value --context production
192-
netlify env:set VAR_NAME value --context production deploy-preview
192+
netlify env:set VAR_NAME value --context production deploy-preview # set in the production and deploy-preview contexts
193+
netlify env:set VAR_NAME value --context branch:feat/make-it-pop # set in the feat/make-it-pop branch context
193194
netlify env:set VAR_NAME value --context production --secret
194195
netlify env:set VAR_NAME value --scope builds
195196
netlify env:set VAR_NAME value --scope builds functions
@@ -213,7 +214,7 @@ netlify env:unset
213214

214215
**Flags**
215216

216-
- `context` (*string*) - Specify a deploy context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev") (default: all contexts)
217+
- `context` (*string*) - Specify a deploy context for environment variables (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: all contexts)
217218
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
218219
- `force` (*boolean*) - Bypasses prompts & Force the command to run.
219220
- `json` (*boolean*) - Output environment variables as JSON
@@ -226,6 +227,7 @@ netlify env:unset
226227
netlify env:unset VAR_NAME # unset in all contexts
227228
netlify env:unset VAR_NAME --context production
228229
netlify env:unset VAR_NAME --context production deploy-preview
230+
netlify env:unset VAR_NAME --context branch:feat/make-it-pop # unset in the feat/make-it-pop branch context
229231
```
230232

231233
---

docs/commands/serve.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ netlify serve
2020

2121
**Flags**
2222

23-
- `context` (*string*) - Specify a deploy context or branch for environment variables (contexts: "production", "deploy-preview", "branch-deploy", "dev")
23+
- `context` (*string*) - Specify a deploy context for environment variables (production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)
2424
- `country` (*string*) - Two-letter country code (https://ntl.fyi/country-codes) to use as mock geolocation (enables --geo=mock automatically)
2525
- `dir` (*string*) - dir with static files
2626
- `filter` (*string*) - For monorepos, specify the name of the application to run the command in
@@ -37,6 +37,9 @@ netlify serve
3737
```bash
3838
netlify serve
3939
BROWSER=none netlify serve # disable browser auto opening
40+
netlify serve --context production # Use env var values from production context
41+
netlify serve --context deploy-preview # Use env var values from deploy-preview context
42+
netlify serve --context branch:feat/make-it-pop # Use env var values from the feat/make-it-pop branch context or branch-deploy context
4043
```
4144

4245

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
"@types/inquirer-autocomplete-prompt": "3.0.3",
174174
"@types/jsonwebtoken": "9.0.9",
175175
"@types/lodash": "4.17.16",
176+
"@types/lodash.shuffle": "^4.2.9",
176177
"@types/multiparty": "4.2.1",
177178
"@types/node": "18.19.86",
178179
"@types/node-fetch": "2.6.12",
@@ -195,6 +196,7 @@
195196
"eslint-config-prettier": "10.1.2",
196197
"eslint-plugin-n": "17.17.0",
197198
"form-data": "4.0.2",
199+
"lodash.shuffle": "^4.2.0",
198200
"memfs": "^4.17.0",
199201
"nock": "14.0.4",
200202
"npm-run-all2": "^7.0.2",

src/commands/build/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ export const createBuildCommand = (program: BaseCommand) =>
99
.description('Build on your local machine')
1010
.option(
1111
'--context <context>',
12-
'Specify a build context or branch (contexts: "production", "deploy-preview", "branch-deploy", "dev")',
12+
'Specify a deploy context for environment variables read during the build (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT or ”production”)',
1313
normalizeContext,
1414
process.env.CONTEXT || 'production',
1515
)
1616
.option('--dry', 'Dry run: show instructions without running them', false)
1717
.option('-o, --offline', 'Disables any features that require network access')
18-
.addExamples(['netlify build'])
18+
.addExamples([
19+
'netlify build',
20+
'netlify build --context deploy-preview # Build with env var values from deploy-preview context',
21+
'netlify build --context branch:feat/make-it-pop # Build with env var values from the feat/make-it-pop branch context or branch-deploy context',
22+
])
1923
.action(async (options, command) => {
2024
const { build } = await import('./build.js')
2125
await build(options, command)

src/commands/deploy/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ Support for package.json's main field, and intrinsic index.js entrypoints are co
110110
),
111111
)
112112
.option('--build', 'Run build command before deploying', false)
113-
.option('--context <context>', 'Context to use when resolving build configuration')
113+
.option(
114+
'--context <context>',
115+
'Specify a deploy context for environment variables read during the build (”production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)',
116+
)
114117
.option(
115118
'--skip-functions-cache',
116119
'Ignore any functions created as part of a previous `build` or `deploy` commands, forcing them to be bundled again as part of the deployment',

src/commands/dev-exec/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ export const createDevExecCommand = (program: BaseCommand) =>
99
.argument('<...cmd>', `the command that should be executed`)
1010
.option(
1111
'--context <context>',
12-
'Specify a deploy context or branch for environment variables (contexts: "production", "deploy-preview", "branch-deploy", "dev")',
12+
'Specify a deploy context for environment variables (production”, ”deploy-preview”, ”branch-deploy”, ”dev”) or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)',
1313
normalizeContext,
1414
'dev',
1515
)
1616
.description(
1717
'Runs a command within the netlify dev environment. For example, with environment variables from any installed add-ons',
1818
)
1919
.allowExcessArguments(true)
20-
.addExamples(['netlify dev:exec npm run bootstrap'])
20+
.addExamples([
21+
'netlify dev:exec npm run bootstrap',
22+
'netlify dev:exec --context deploy-preview npm run bootstrap # Run with env var values from deploy-preview context',
23+
'netlify dev:exec --context branch:feat/make-it-pop npm run bootstrap # Run with env var values from the feat/make-it-pop branch context or branch-deploy context',
24+
])
2125
.action(async (cmd: string, options: OptionValues, command: BaseCommand) => {
2226
const { devExec } = await import('./dev-exec.js')
2327
await devExec(cmd, options, command)

0 commit comments

Comments
 (0)