Skip to content

Commit a152b60

Browse files
Add env section to bundle scripts for DABs interpolation (#5299)
Fixes #4179. Adds an `env:` map to `scripts.<name>`. Values may reference `${bundle.*}`, `${workspace.*}`, and `${var.*}`; they're resolved before the script runs and exported into its shell, where `content` uses plain `$NAME`: ```yaml scripts: show_env: env: REGION: ${var.region} content: echo "region=$REGION" ``` `content` is still passed to the shell as-is, so `${...}` in `content` stays unsupported (the error now points to the `env:` section). Env values with any other prefix (e.g. `${resources.*}`) are rejected. On a name collision with the CLI-injected auth vars, the script's `env:` wins. This pull request was AI-assisted by Isaac.
1 parent 7c368ae commit a152b60

22 files changed

Lines changed: 229 additions & 37 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Bundles
1212

13+
* Added an `env:` section to `scripts.<name>` for declaring environment variables that may reference `${bundle.*}`, `${workspace.*}`, and `${var.*}` ([#4179](https://github.com/databricks/cli/issues/4179)).
14+
1315
### Dependency updates
1416

1517
### API Changes
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
bundle:
2+
name: script-env-bad-prefix
3+
4+
resources:
5+
jobs:
6+
my_job:
7+
name: my-job
8+
9+
scripts:
10+
bad:
11+
env:
12+
JOB_ID: ${resources.jobs.my_job.id}
13+
UNKNOWN: ${something.else}
14+
content: echo "$JOB_ID $UNKNOWN"

acceptance/bundle/run/scripts/env-bad-prefix/out.test.toml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
>>> [CLI] bundle validate
3+
Error: ${resources.jobs.my_job.id} cannot be used in scripts.bad.env.JOB_ID; only ${bundle.*}, ${workspace.*}, and ${var.*} are resolved before scripts execute
4+
at scripts.bad.env.JOB_ID
5+
in databricks.yml:12:15
6+
7+
Error: ${something.else} cannot be used in scripts.bad.env.UNKNOWN; only ${bundle.*}, ${workspace.*}, and ${var.*} are resolved before scripts execute
8+
at scripts.bad.env.UNKNOWN
9+
in databricks.yml:13:16
10+
11+
Name: script-env-bad-prefix
12+
Target: default
13+
14+
Found 2 errors
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
musterr trace $CLI bundle validate
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Local = true
2+
Cloud = false
3+
4+
[EnvMatrix]
5+
DATABRICKS_BUNDLE_ENGINE = ["terraform", "direct"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
bundle:
2+
name: script-env-precedence
3+
4+
targets:
5+
default:
6+
default: true
7+
8+
# A script's env: entry must win over the DATABRICKS_BUNDLE_TARGET variable the
9+
# CLI injects into the script process (executeScript appends env: last).
10+
scripts:
11+
show_target:
12+
env:
13+
DATABRICKS_BUNDLE_TARGET: from-script-env
14+
content: echo "target=$DATABRICKS_BUNDLE_TARGET"

acceptance/bundle/run/scripts/env-precedence/out.test.toml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
>>> [CLI] bundle run show_target
3+
target=from-script-env
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trace $CLI bundle run show_target

0 commit comments

Comments
 (0)