Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for Strict Environments #4490

Merged
merged 5 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/pages/repo/docs/reference/command-line-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,62 @@ Task details include:
- `dependencies`: Tasks that must run before this task
- `dependents`: Tasks that must be run after this task

#### `--experimental-env-mode`

`type: string`

Controls the available environment variables to your tasks.

<Callout type="info">
**Warning**: This in experimental flag, so its name and behavior can change.
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
</Callout>

| option | description |
| ------- | ---------------------------------------------------------- |
| [blank] | Behaves same as infer |
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
| infer | infers strict mode or loose mode based on allowlist config |
| loose | allows all environment variables |
| strict | only allow declared variables |

`PATH`, `SHELL`, and `SYSTEMROOT` are always available to all tasks.

**`infer`**

In `infer` mode, Turborepo will look for
[`experimentalPassThroughEnv`][r-config-experimentalPassThroughEnv] for each
task config, and
[`experimentalGlobalPassThroughEnv`][r-config-experimentalGlobalPassThroughEnv]
in the root of the `turbo.json` config. If either is defined, "strict" mode will
be inferred. If neither is defined, then `loose` mode is inferred.

`infer` mode is not part of the global cache key, so using the
`--experimental-env-mode=infer` (or `--experimental-env-mode` without a value)
flag will not invalidate the global cache.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little torn on how to message this. There is not exactly a thing that is "the global cache" that can be invalidated. No cache entries are being lost or made inaccessible, which I would expect with the word "invalidate" applied to a cache.

Furthermore, I think this is incorrect. If the mode is infer, then we will alter the global hash if experimentalGLobalPassThroughEnv is defined. At least, that is what it looks like to me from the implementation.


**`loose`**

In `loose` mode, all environment variables are made available to the task.

When using this flag, if `experimentalGlobalPassThroughEnv` is set in
`turbo.json`, Turborepo will invalidate the global cache, as it could change the
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
behavior of all task executions.

**`strict`**

In `strict` mode, only environment variables specified in the following keys are
available to the task.

- `env` and experimentalPassThroughEnv` in each task
- `globalEnv` and experimentalGlobalPassThroughEnv` in each task

When using this flag, if `experimentalGlobalPassThroughEnv` is set in
`turbo.json`, Turborepo will invalidate the global cache, as it could change the
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
behavior of all task executions.

Note that in `strict` mode, (even if it is inferred) _all_ tasks are opted into
`strict` mode, regardless of whether `experimentalGlobalPassThroughEnv` or
`globalEnv` are set.

#### `--filter`

`type: string[]`
Expand Down Expand Up @@ -648,3 +704,6 @@ Unlink the current directory from the Remote Cache.
## `turbo bin`

Get the path to the `turbo` binary.

[r-config-experimentalPassThroughEnv]: /repo/docs/refernce/configuration#experimentalPassThroughEnv
[r-config-experimentalGlobalPassThroughEnv]: /repo/docs/refernce/configuration#experimentalGlobalPassThroughEnv
62 changes: 62 additions & 0 deletions docs/pages/repo/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,65 @@ option, `turbo` can warn you about an invalid configuration.
```

[1]: /repo/docs/core-concepts/monorepos/configuring-workspaces

## Experimental

### `experimentalGlobalPassThroughEnv`

This goes at the root of your configuration.

`type: string[]`

An allowlist of environment variables that should be made available to all tasks
but should not contribute to the task's cache key. Using this key opts all tasks
into strict environment variable mode.

Changing this list will contribute to the global cache key, but the value of each
variable will not.

**Example**

`AWS_SECRET_KEY` and `GITHUB_TOKEN` are available to all tasks in `strict` [env mode][r-cli-env-mode].

```jsonc
{
"$schema": "https://turbo.build/schema.json",
"experimentalGlobalPassThroughEnv": ["AWS_SECRET_KEY", "GITHUB_TOKEN"],
"pipeline": {
// ...task definitions...
}
}
```

### `experimentalPassThroughEnv`

`type: string[]`

This config goes inside each task definition in the [`pipeline`][r-config-pipeline].

An allowlist of environment variables that should be made available to this task
but should not contribute to the task's cache key. Using this key opts this task
into strict environment variable mode.

Changing this list will contribute to the task's cache key, but the value of each
variable will not.

**Example**

`AWS_SECRET_KEY` and `GITHUB_TOKEN` are available to the `build` task, but not to the `lint` task
in `strict` [env mode][r-cli-env-mode].

```jsonc
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"experimentalPassThroughEnv": ["AWS_SECRET_KEY", "GITHUB_TOKEN"]
},
"lint": {},
}
}
```

[r-config-pipeline]: #pipeline
[r-cli-env-mode]: /repo/docs/reference/command-line-reference#--experimental-env-mode
25 changes: 25 additions & 0 deletions packages/turbo-types/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ export interface RootSchema extends BaseSchema {
*/
globalEnv?: string[];

/**
* An allowlist of environment variables that should be made to all tasks, but
* should not contribute to the task's cache key, e.g. `AWS_SECRET_KEY`.
*
* Only applies in `--env=strict` mode.
*
* Documentation: https://turbo.build/repo/docs/refernce/configuration#experimentalGlobalPassThroughEnv
*
* @default []
*/
experimentalGlobalPassThroughEnv?: string[];

/**
* Configuration options that control how turbo interfaces with the remote cache.
*
Expand Down Expand Up @@ -114,6 +126,19 @@ export interface Pipeline {
*/
env?: string[];

/**
* An allowlist of environment variables that should be made available in this
* task's environment, but should not contribute to the task's cache key,
* e.g. `AWS_SECRET_KEY`.
*
* Only applies in `--env=strict` mode.
*
* Documentation: https://turbo.build/repo/docs/refernce/configuration#experimentalPassThroughEnv
*
* @default []
*/
experimentalPassThroughEnv?: string[];

/**
* The set of glob patterns indicating a task's cacheable filesystem outputs.
*
Expand Down