devspace-sh / devspace

DevSpace - The Fastest Developer Tool for Kubernetes ⚡ Automate your deployment workflow with DevSpace and develop software directly inside Kubernetes.
https://devspace.sh
Apache License 2.0
4.19k stars 351 forks source link

Defined `vars` will not be overwritten by `DEVSPACE_ENV_FILE: ".env"` #2248

Open ethankhall opened 1 year ago

ethankhall commented 1 year ago

What happened?

Defining a default value in the devspace.yaml file will not be overwritten by the .env file. I tried putting DEVSPACE_ENV_FILE: ".env" at the beginning and end of the vars.

When running devspace run-pipeline test I get

info Using namespace 'ethan-hall'
info Using kube context 'loft_ethan-hall_portland'
false

What did you expect to happen instead?

When running devspace run-pipeline test I get

info Using namespace 'ethan-hall'
info Using kube context 'loft_ethan-hall_portland'
true

How can we reproduce the bug? (as minimally and precisely as possible)

My devspace.yaml:

version: v2beta1
name: foo
vars:
  CLOUDDEV_AUTO_MIGRATE: false
  DEVSPACE_ENV_FILE: ".env"
functions:
  test_fun: |-
    echo ${CLOUDDEV_AUTO_MIGRATE}
pipelines:
  test: |-
    test_fun

My .env:

CLOUDDEV_AUTO_MIGRATE=true

Local Environment:

/kind bug

noizwaves commented 1 year ago

Playing around with this example verbatim, I can't even override CLOUDDEV_AUTO_MIGRATE with an explicit environment variable either:

`CLOUDDEV_AUTO_MIGRATE=true devspace print` ``` ❯ CLOUDDEV_AUTO_MIGRATE=true devspace print ------------------- Vars: NAME | VALUE ------------------------------+---------------------------------------------------- CLOUDDEV_AUTO_MIGRATE | false ... ------------------- Loaded path: /Users/adam.neumann/workspace/v6-vars/devspace.yaml ------------------- version: v2beta1 name: foo functions: test_fun: echo false pipelines: test: run: test_fun ```

Reading the variable docs closely, it looks like CLOUDDEV_AUTO_MIGRATE is a static value and must be explicitly overridden with --var CLOUDDEV_AUTO_MIGRATE=foobar. This does change the value, but it's still not from the .env file:

`devspace-beta print --var CLOUDDEV_AUTO_MIGRATE=foobar` ``` ❯ devspace-beta print --var CLOUDDEV_AUTO_MIGRATE=foobar ------------------- Vars: NAME | VALUE ------------------------------+---------------------------------------------------- CLOUDDEV_AUTO_MIGRATE | foobar ... ------------------- Loaded path: /Users/adam.neumann/workspace/v6-vars/devspace.yaml ------------------- version: v2beta1 name: foo functions: test_fun: echo foobar pipelines: test: run: test_fun ```

The section on env vars states that:

... without the need to explictly define them.

Removing the CLOUDDEV_AUTO_MIGRATE: false line results in the expected output to devspace run-pipeline test:

`devspace-beta run-pipeline test` fixed ``` ❯ cat .env CLOUDDEV_AUTO_MIGRATE=true ❯ cat devspace.yaml version: v2beta1 name: foo vars: DEVSPACE_ENV_FILE: ".env" functions: test_fun: |- echo ${CLOUDDEV_AUTO_MIGRATE} pipelines: test: |- test_fun ❯ devspace-beta run-pipeline test 17:40:43 info Using namespace 'adam-neumann' 17:40:43 info Using kube context 'loft_adam-neumann_portland' 17:40:43 true ```

but with the loss of the default false behavior:

Loss of default `false` behavior ``` ❯ cat .env #CLOUDDEV_AUTO_MIGRATE=true ❯ cat devspace.yaml version: v2beta1 name: foo vars: DEVSPACE_ENV_FILE: ".env" functions: test_fun: |- echo ${CLOUDDEV_AUTO_MIGRATE} pipelines: test: |- test_fun ❯ devspace-beta run-pipeline test 17:45:37 info Using namespace 'adam-neumann' 17:45:37 info Using kube context 'loft_adam-neumann_portland' 17:45:38 ```
noizwaves commented 1 year ago

It looks like the v1beta style default config is still available, and this appears to mitigate the issue:

With default value ✅ ``` ❯ cat .env #CLOUDDEV_AUTO_MIGRATE=true ❯ cat devspace.yaml version: v2beta1 name: foo vars: CLOUDDEV_AUTO_MIGRATE: default: false source: env DEVSPACE_ENV_FILE: ".env" functions: test_fun: |- echo ${CLOUDDEV_AUTO_MIGRATE} pipelines: test: |- test_fun ❯ devspace-beta run-pipeline test 17:53:53 info Using namespace 'adam-neumann' 17:53:53 info Using kube context 'loft_adam-neumann_portland' 17:53:53 false ```
With override in .env ✅ ``` ❯ cat .env CLOUDDEV_AUTO_MIGRATE=true ❯ cat devspace.yaml version: v2beta1 name: foo vars: CLOUDDEV_AUTO_MIGRATE: default: false source: env DEVSPACE_ENV_FILE: ".env" functions: test_fun: |- echo ${CLOUDDEV_AUTO_MIGRATE} pipelines: test: |- test_fun ❯ devspace-beta run-pipeline test 17:54:46 info Using namespace 'adam-neumann' 17:54:46 info Using kube context 'loft_adam-neumann_portland' 17:54:46 true ```
FabianKramm commented 1 year ago

@ethankhall @noizwaves thanks for creating this issue! The correct syntax is to use:

vars:
  MY_ENV_VAR:
    source: env
    default: my-default

which would be then overridable from the .env file as @noizwaves has suggested

FabianKramm commented 1 year ago

We'll also update the documentation for this

ethankhall commented 1 year ago

@FabianKramm can you also update the parsing of v1beta11 into v2beta1 to show the values of vars?

When I run devspace print against the v1beta11, the only var in the yaml output is

vars:
    DEVSPACE_ENV_FILE:
        value: .env
        alwaysResolve: false
FabianKramm commented 1 year ago

@ethankhall variables should be shown separately within the devspace print section as these could change or not even be defined (in the case of environment variables), so the vars section would be incomplete anyways