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

Add environment variables to environment.yml files #10169

Merged
merged 4 commits into from
Oct 5, 2020

Conversation

AlbertDeFusco
Copy link
Contributor

This PR enables creation and export of conda environments that have environment variable created with conda env config vars.

Tests have been included for this behavior.

Here' an example

Create and export

First create a new env and set two variables and confirm

> conda create -y -n env_with_vars python=3.7
> conda env config vars set DUDE=woah SWEET=yaaa -n env_with_vars
> conda run -n env_with_vars env | grep -E "DUDE|SWEET"
DUDE=woah
SWEET=yaaa

When the env is exported the variables are retained in a key called variables:

> conda env export -n env_with_vars --from-history
name: env_with_vars
channels:
  - defaults
dependencies:
  - python=3.7
variables:
  DUDE: woah
  SWEET: yaaa
prefix: /Users/adefusco/Development/Conda/conda/devenv/envs/env_with_vars

And also with the full dependency spec

> name: env_with_vars
channels:
  - defaults
dependencies:
  - ca-certificates=2020.6.24=0
  - certifi=2020.6.20=py37_0
  - libcxx=10.0.0=1
  - libedit=3.1.20191231=h1de35cc_1
  - libffi=3.3=hb1e8313_2
  - ncurses=6.2=h0a44026_1
  - openssl=1.1.1g=h1de35cc_0
  - pip=20.2.2=py37_0
  - python=3.7.7=hf48f09d_4
  - readline=8.0=h1de35cc_0
  - setuptools=49.6.0=py37_0
  - sqlite=3.32.3=hffcf06c_0
  - tk=8.6.10=hb0a8c7a_0
  - wheel=0.34.2=py37_0
  - xz=5.2.5=h1de35cc_0
  - zlib=1.2.11=h1de35cc_3
variables:
  DUDE: woah
  SWEET: yaaa
prefix: /Users/adefusco/Development/Conda/conda/devenv/envs/env_with_vars

Create from yaml

The environment.yml spec will allow variables to be set when the env is created.

# environment.yml
name: new_env
dependencies:
  - python=3.8
variables:
  DUDE: woah
  SWEET: yaaa

Confirm that the variables have been set.

> conda env create -f environment.yml
> conda run -n new_env env | grep -E "DUDE|SWEET"
DUDE=woah
SWEET=yaaa

This adds "variables:" key to set environment variables
in the environment.yml file. Any number of variables are
supported. The format is.

variables:
    <VARIABLE>: <value>
    <VARIABLE>: <value>

Variables set using `conda env config set vars` are retained
with `conda env export` and set with `conda env create` if the
variables key is included.
testing for creation and export with variables

fixed issue where variables not exported without --from-history
@AlbertDeFusco AlbertDeFusco requested a review from a team as a code owner August 17, 2020 17:57
@cla-bot cla-bot bot added the cla-signed [bot] added once the contributor has signed the CLA label Aug 17, 2020
@chenghlee
Copy link
Contributor

Closing & re-opening this PR to trigger new CI checks.

@chenghlee chenghlee closed this Oct 5, 2020
@chenghlee chenghlee reopened this Oct 5, 2020
Copy link
Contributor

@chenghlee chenghlee left a comment

Choose a reason for hiding this comment

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

LGTM

@chenghlee chenghlee merged commit feb115a into conda:master Oct 5, 2020
@Florents-Tselai
Copy link

What happens when you don't want to store certain ENV_VARS in Git or want to separate between dev and production. Is there any recipe for an "if not already defined use this" implementation?

In prod for example these can be injected via systemd, but in dev, they should be there

@AlbertDeFusco
Copy link
Contributor Author

This is a very good point and perhaps anaconda-project can help. I'd like to continue to this discussion.

https://anaconda-project.readthedocs.io/en/latest/user-guide/tasks/work-with-variables.html

This recent PR defines env var priority ordering in anaconda-project and will be part of the next release

anaconda/anaconda-project#286

@github-actions
Copy link

github-actions bot commented Jan 7, 2022

Hi there, thank you for your contribution to Conda!

This pull request has been automatically locked since it has not had recent activity after it was closed.

Please open a new issue or pull request if needed.

@github-actions github-actions bot added the locked [bot] locked due to inactivity label Jan 7, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed [bot] added once the contributor has signed the CLA locked [bot] locked due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants