Answers: <p>What we do is download and install miniconda in the first step in our (declarative) pipeline and create the environment(s) from a yaml file.</p>
  
  <pre><code>sh '''#!/usr/bin/env bash
  wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -nv -O miniconda.sh
  bash miniconda.sh -b -p $WORKSPACE/miniconda
  conda config --set always_yes yes --set changeps1 no
  conda update -q conda
  
  conda env create -f envs/ansible-env.yaml
  '''
  </code></pre>
  
  <p>In each step we start the conda environment like so:</p>
  
  <pre><code>sh '''#!/usr/bin/env bash
  source $WORKSPACE/miniconda/etc/profile.d/conda.sh
  conda activate miniconda/envs/ansible-env/
  
  # do stuff
  '''
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-02-14T15:11:22.300Z
Answers: Thank you for this suggestion, @Siebren! I decided to take an entirely different approach, but I wanted to mention that I completely overlooked using `sh()` with multiple lines. That would have likely solved my original problem, so I'm going to mark this as the accepted answer for that reason.
Answers: Argyle
Answers: CC BY-SA 4.0
Answers: 2021-03-27T14:09:37.707Z
Answers: continuum.io moved to anaconda.com see https://github.com/mlflow/mlflow/issues/2683 and https://github.com/mlflow/mlflow/pull/2685 so its now either `curl -s -o miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh` or `wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -nv -O miniconda.sh`
Answers: user2358582
Answers: CC BY-SA 4.0
Answers: 2020-02-13T17:22:47.033Z
Answers: 2020-02-13T17:22:47.033Z
Answers: Siebren
Answers: <p>I've encountered the same issue when trying to use conda environments in CI builds and Docker images. The <a href="https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/use-conda-with-travis-ci.html" rel="nofollow noreferrer">conda documentation</a> does provide an example when using Travis CI, but it does not work for me.</p>
  <p>However, via <a href="https://pythonspeed.com/articles/activate-conda-dockerfile/" rel="nofollow noreferrer">this blog</a> I found a working solution! Instead of activating your environment, you can use it by calling <code>conda run -n my_env python my_file.py</code>. So, keep everything like it was until you activate your environment.</p>
  <p>These build specs worked for me when using AWS CodeBuild:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;version&quot;: &quot;0.2&quot;,
    &quot;phases&quot;: {
      &quot;pre_build&quot;: {
        &quot;commands&quot;: [
          &quot;wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh -q&quot;,
          &quot;bash miniconda.sh -b -p $HOME/miniconda&quot;,
          &quot;. \&quot;$HOME/miniconda/etc/profile.d/conda.sh\&quot;&quot;,
          &quot;hash -r&quot;,
          &quot;conda config --set always_yes yes --set changeps1 no&quot;,
          &quot;conda update -q conda&quot;,
          &quot;conda info -a&quot;,
          &quot;conda env create -q -p ./py_env -f environment.yml&quot;
        ]
      },
      &quot;build&quot;: {
        &quot;commands&quot;: [
          &quot;conda run -p ./py_env python app.py&quot;
        ]
      }
    }
  }
  
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-12-17T14:05:49.657Z
Answers: 2020-12-18T18:51:21.780Z
Answers: 2020-12-18T18:51:21.780Z
Answers: Jels Boulangier
Answers: <p>As a suggestion... Can you try calling the absolute path of the python executable in the environment? I mean: when you change conda environments, what happens is that the python executable you call changes path; you can find which python executable would be executed by</p>
  
  <pre><code>which python
  </code></pre>
  
  <p>Inside the environment. Then just forget about calling conda in the Jenkins pipeline. That should work if that’s what you’re changing environments for. Or are you changing the conda environment for some other reason?</p>
  
  <p>EDIT: the same would happen when calling ansible as a library installed in the environment. It will change path to something like:</p>
  
  <pre><code>~/conda/envs/your_env/lib/ansible
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-08T08:00:14.377Z
Answers: 2020-01-08T08:00:14.377Z
Answers: jasper
Body: <p>I'm running Conda 4.6.14 on Ubuntu 16.04. </p>
  
  <p>I thought about using virtual environments with Conda in my Jenkins pipelines that needed Ansible, by having all nodes use the same environment, <code>ansible-env</code> to run playbooks. While the process seems pretty straight forward, I'm unable to call <code>conda activate ansible-env</code>, because it results in the following error:</p>
  
  <pre><code>+ conda activate ansible-env
  
  CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run
  
      $ conda init &lt;SHELL_NAME&gt;
  
  Currently supported shells are:
    - bash
    - fish
    - tcsh
    - xonsh
    - zsh
    - powershell
  
  See 'conda init --help' for more information and options.
  
  IMPORTANT: You may need to close and restart your shell after running 'conda init'.
  </code></pre>
  
  <p>Here is the code I'm using to activate...pretty basic</p>
  
  <pre><code>sh("conda activate ansible-env")
  sh("ansible --version")
  sh("conda deactivate")
  </code></pre>
  
  <p>I ran <code>conda init --system</code> on the build node as the Jenkins user and then rebooted. No change. </p>
  
  <p>I've spent a great deal of time trying to google a solution, but everything I've tried hasn't worked.</p>
  
  <p><strong>What I've attempted:</strong></p>
  
  <ul>
  <li>Tried running <code>sh("conda init bash")</code> in the pipeline.</li>
  <li>Executing <code>. /home/jenkins/miniconda3/profile.d/conda.sh</code> from within the pipeline.</li>
  <li>Made sure the <code>bin</code> directory for miniconda3 is set in $PATH.</li>
  <li>Ran <code>dpkg-configure dash</code> to make sure <code>/bin/bash</code> is the default shell on the node.</li>
  <li>Tried sourcing the <code>/home/jenkins/.bashrc</code> file</li>
  <li>Calling the <code>conda</code> executable via the absolute path. </li>
  <li>Tried using <code>source ansible-env</code> or <code>activate ansible-env</code> without <code>conda</code>.</li>
  </ul>
  
  <p>I'm pretty sure the issue has to do with the following lines added to <code>~/.bashrc</code> of the jenkins user after running <code>conda init --system</code>, but still not being accessible from that same user when a job is running. Unfortunately, I've hit a wall and not sure where to go from here. </p>
  
  <p><strong>NOTE:</strong> Please don't suggest switching to VirtuanEnv unless there's a specific reason Conda won't work with Jenkins. I'm aware of that route, but went with conda for...reasons.</p>
  
Tags: <jenkins>
Title: Unable to run `conda activate` from Jenkins pipeline
UserDisplayName: Argyle
ThreadText: # Unable to run `conda activate` from Jenkins pipeline
  
  (Asked by: username_0 on 2020-01-07)
  
  I'm running Conda 4.6.14 on Ubuntu 16.04.
  
  I thought about using virtual environments with Conda in my Jenkins pipelines that needed Ansible, by having all nodes use the same environment, `ansible-env` to run playbooks. While the process seems pretty straight forward, I'm unable to call `conda activate ansible-env`, because it results in the following error:
  
  ```
  + conda activate ansible-env
  
  CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run
  
      $ conda init <SHELL_NAME>
  
  Currently supported shells are:
    - bash
    - fish
    - tcsh
    - xonsh
    - zsh
    - powershell
  
  See 'conda init --help' for more information and options.
  
  IMPORTANT: You may need to close and restart your shell after running 'conda init'.
  
  ```
  
  Here is the code I'm using to activate...pretty basic
  
  ```
  sh("conda activate ansible-env")
  sh("ansible --version")
  sh("conda deactivate")
  
  ```
  
  I ran `conda init --system` on the build node as the Jenkins user and then rebooted. No change.
  
  I've spent a great deal of time trying to google a solution, but everything I've tried hasn't worked.
  
  **What I've attempted:**
  
  * Tried running `sh("conda init bash")` in the pipeline.
  * Executing `. /home/jenkins/miniconda3/profile.d/conda.sh` from within the pipeline.
  * Made sure the `bin` directory for miniconda3 is set in $PATH.
  * Ran `dpkg-configure dash` to make sure `/bin/bash` is the default shell on the node.
  * Tried sourcing the `/home/jenkins/.bashrc` file
  * Calling the `conda` executable via the absolute path.
  * Tried using `source ansible-env` or `activate ansible-env` without `conda`.
  
  I'm pretty sure the issue has to do with the following lines added to `~/.bashrc` of the jenkins user after running `conda init --system`, but still not being accessible from that same user when a job is running. Unfortunately, I've hit a wall and not sure where to go from here.
  
  **NOTE:** Please don't suggest switching to VirtuanEnv unless there's a specific reason Conda won't work with Jenkins. I'm aware of that route, but went with conda for...reasons.
  
  **Tags:** jenkins
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2020-02-13. Score: 2 | Accepted answer
  
  What we do is download and install miniconda in the first step in our (declarative) pipeline and create the environment(s) from a yaml file.
  
  ```
  sh '''#!/usr/bin/env bash
  wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -nv -O miniconda.sh
  bash miniconda.sh -b -p $WORKSPACE/miniconda
  conda config --set always_yes yes --set changeps1 no
  conda update -q conda
  
  conda env create -f envs/ansible-env.yaml
  '''
  
  ```
  
  In each step we start the conda environment like so:
  
  ```
  sh '''#!/usr/bin/env bash
  source $WORKSPACE/miniconda/etc/profile.d/conda.sh
  conda activate miniconda/envs/ansible-env/
  
  # do stuff
  '''
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for this suggestion, @username_1! I decided to take an entirely different approach, but I wanted to mention that I completely overlooked using `sh()` with multiple lines. That would have likely solved my original problem, so I'm going to mark this as the accepted answer for that reason.  
  > **username_2**: continuum.io moved to anaconda.com see https://github.com/mlflow/mlflow/issues/2683 and https://github.com/mlflow/mlflow/pull/2685 so its now either `curl -s -o miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh` or `wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -nv -O miniconda.sh`  
  
  ---
  
  ### Answer by username_3 on 2020-12-17. Score: 1
  
  I've encountered the same issue when trying to use conda environments in CI builds and Docker images. The [conda documentation](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/use-conda-with-travis-ci.html) does provide an example when using Travis CI, but it does not work for me.
  
  However, via [this blog](https://pythonspeed.com/articles/activate-conda-dockerfile/) I found a working solution! Instead of activating your environment, you can use it by calling `conda run -n my_env python my_file.py`. So, keep everything like it was until you activate your environment.
  
  These build specs worked for me when using AWS CodeBuild:
  
  ```json
  {
    "version": "0.2",
    "phases": {
      "pre_build": {
        "commands": [
          "wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh -q",
          "bash miniconda.sh -b -p $HOME/miniconda",
          ". \"$HOME/miniconda/etc/profile.d/conda.sh\"",
          "hash -r",
          "conda config --set always_yes yes --set changeps1 no",
          "conda update -q conda",
          "conda info -a",
          "conda env create -q -p ./py_env -f environment.yml"
        ]
      },
      "build": {
        "commands": [
          "conda run -p ./py_env python app.py"
        ]
      }
    }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_4 on 2020-01-08. Score: 0
  
  As a suggestion... Can you try calling the absolute path of the python executable in the environment? I mean: when you change conda environments, what happens is that the python executable you call changes path; you can find which python executable would be executed by
  
  ```
  which python
  
  ```
  
  Inside the environment. Then just forget about calling conda in the Jenkins pipeline. That should work if that’s what you’re changing environments for. Or are you changing the conda environment for some other reason?
  
  EDIT: the same would happen when calling ansible as a library installed in the environment. It will change path to something like:
  
  ```
  ~/conda/envs/your_env/lib/ansible
  
  ```
  
  
  ---
  
  