简体   繁体   中英

Activating conda environment during gitlab CI

My .gitlab-ci.yml file looks like this:

anomalydetector:
  image: continuumio/miniconda:4.7.10
  stage: build
  tags:
    - docker
  script:
    - conda env create -f environment.yml
    - conda activate my-env
    - pytest tests/.

On Gitlab, this job starts fine, and the logs read

$ conda env create -f environment.yml
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done


==> WARNING: A newer version of conda exists. <==
  current version: 4.7.10
  latest version: 4.7.11

Ok, so I'm using a conda version later than 4.4, so conda activate should work. However, the job fails with the following:

# To activate this environment, use
#
#     $ conda activate my-env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

$ conda activate my-env

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

I have then tried editing my .gitlab-ci.yml file so that there is a command

conda init bash

but then get the message

==> For changes to take effect, close and re-open your current shell. <==

How can I activate my conda environment in the gitlab CI process?

conda init touches the .bashrc file. To reinitialize the shell you can source it:

  - conda create --name myenv
  - conda init bash
  - source ~/.bashrc    # <- !!!
  - conda activate myenv 

Whether this is better or worse than source activate myenv is a separate discussion, I guess.

Similarly to Tommy's answer, this needs to be done for the Windows Powershell as well. Contrary to bash conda activate myenv does not fail in the powershell. It just has no effect (ie the environment is not switched) without calling conda init powershell which makes it even more awkward. Reloading the profile in the powershell is more complicated since there are six of them [1]. I used:

 - conda create --name myenv
 - conda init powershell
 - "if (test-path $PROFILE.CurrentUserAllHosts) { & $PROFILE.CurrentUserAllHosts}"
 - conda activate myenv 

Why Conda uses the $PROFILE.CurrentUserAllHosts profile has been asked in an issue [2].

references:

[1] https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/

[2] https://github.com/conda/conda/issues/8608

Another possibility that you might find more succinct and elegant: source directly the code needed for conda to run run with bash. This also has the effect of adding conda to the PATH, if it's not the case.

This is done with

- source <anaconda_root>/etc/profile.d/conda.sh
- conda activate myenv

(Stolen from https://stackoverflow.com/a/60523131/11343 )

Somehow all of these answers failed me. I ended up using conda run instead of activating an environment. That allowed me to run pytest without activating the environment

- conda run -n <environment-name> python -m pytest <addl-pytest-args>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM