Skip to content

Commit

Permalink
feat: expose NOVOPS_ENVIRONMENT variable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBeucher committed Dec 9, 2023
1 parent 87c729c commit 2dafcc3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- [Installation](install.md)
- [Getting started](getting-started.md)
- [Security Model](security.md)
- [CLI reference](cli-reference.md)
- [CLI reference and examples](cli-reference.md)
- [Configuration and Modules](config/README.md)
- [Configuration](config/config.md)
- [`.novops.yml` schema](config/schema.md)
Expand Down
40 changes: 40 additions & 0 deletions docs/src/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
- [`novops completion`](#novops-completion)
- [`novops schema`](#novops-schema)
- [Built-in environment variables](#built-in-environment-variables)
- [Variables loaded by default](#variables-loaded-by-default)
- [Examples](#examples)
- [Override default config path](#override-default-config-path)
- [Run a sub-process](#run-a-sub-process)
- [Specify environment without prompt](#specify-environment-without-prompt)
- [Use built-in environment variables](#use-built-in-environment-variables)
- [Check environment currently loaded by Novops](#check-environment-currently-loaded-by-novops)
- [Writing .env to secure directory](#writing-env-to-secure-directory)
- [Change working directory](#change-working-directory)
- [Dry-run](#dry-run)
Expand Down Expand Up @@ -118,6 +120,12 @@ CLI flags can be specified via environment variables `NOVOPS_*`:
- `NOVOPS_LOAD_FORMAT` - load subcommand flag `-f, --format `
- `NOVOPS_LOAD_SKIP_TTY_CHECK` - load subcommand `--skip-tty-check`

## Variables loaded by default

Novops will load some variables by default when running `novops [load|run]`:

- `NOVOPS_ENVIRONMENT` - Name of the loaded environment

## Examples

### Override default config path
Expand Down Expand Up @@ -173,6 +181,38 @@ Equivalent to
novops load -e dev -s /tmp/.env
```

### Check environment currently loaded by Novops

Novops exposes some variables by default (eg. `NOVOPS_ENVIRONMENT`). You can use them to perform some specific actions.

Simple example: show loaded environment

```sh
novops run -e dev -- sh -c 'echo "Current Novops environment: $NOVOPS_ENVIRONMENT"'
```

You can leverage `NOVOPS_ENVIRONMENT` to change behavior on certain environments, such as avoiding destructive action in Prod:

```sh
# Failsafe: if current environment is prod or contains 'prod', exit with error
if [[ $NOVOPS_ENVIRONMENT == *"prod"* ]]; then
echo "You can't run this script in production or prod-like environments!"
exit 1
fi

# ... some destructive actions
make destroy-all
```

`NOVOPS_ENVIRONMENT` is automayically loaded:

```sh
novops run -e prod -- ./destroy-all.sh # Won't work
novops run -e dev -- ./destroy-all.sh # OK
```

You may instead add a custom `MY_APP_ENVIRONMENT` on each environment but it's less convenient.

### Writing .env to secure directory

You can write `.env` variable file to to disk in a secure directory and source it later. **This usage is not recommended** as writing data to disk may represent a risk.
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ pub async fn resolve_environment_inputs(ctx: &NovopsContext, inputs: NovopsEnvir

let mut variable_outputs: HashMap<String, VariableOutput> = HashMap::new();
let mut file_outputs: HashMap<String, FileOutput> = HashMap::new();

// Expose Novops internal variables
// Load first so user can override via config if needed
variable_outputs.insert(String::from("NOVOPS_ENVIRONMENT"), VariableOutput {
name: String::from("NOVOPS_ENVIRONMENT"),
value: ctx.env_name.clone()
});

for v in &inputs.variables.unwrap_or(vec![]) {
let val = v.resolve(&ctx).await
Expand Down
11 changes: 10 additions & 1 deletion tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ mod tests {
Ok(())
}


#[tokio::test]
async fn test_should_error_tty() -> Result<(), anyhow::Error> {

Expand All @@ -288,4 +287,14 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_default_loaded_vars() -> Result<(), anyhow::Error> {

let result = load_env_dryrun_for("empty", "dev").await?;

assert_eq!(result.variables.get("NOVOPS_ENVIRONMENT").unwrap().value, "dev");

Ok(())
}

}

0 comments on commit 2dafcc3

Please sign in to comment.