GitLab CI preserve environment between build stages
<p>I am working on a python project and using <a href="http://conda.pydata.org/miniconda.html" rel="noreferrer">miniconda</a> to manage my environment. I am using GitLab for CI with the following runner configuration</p>

<pre><code>stages:
  - build
  - test 

build:
  stage: build
  script:
    - if hash $HOME/miniconda/bin/conda 2&gt;/dev/null; 
      then
         export PATH="$HOME/miniconda/bin:$PATH";
      else
        wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh;
        bash miniconda.sh -b -p $HOME/miniconda;
        export PATH="$HOME/miniconda/bin:$PATH";
      fi
    - conda update --yes conda

test:
  stage: test
  script:
    - conda env create --quiet --force --file environment.yml
    - source activate myenv
    - nosetests --with-coverage --cover-erase --cover-package=mypackage --cover-html
    - pylint --reports=n tests/test_final.py
    - pep8 tests/test_final.py
    - grep pc_cov cover/index.html | egrep -o "[0-9]+\%" | awk '{ print "covered " $1;}'
</code></pre>

<p>I assumed (incorrectly) that my <code>build</code> stage would setup the correct environment in which I could run my <code>test</code> stage. Looking at <a href="https://stackoverflow.com/questions/31623924/test-after-build-would-run-in-new-environment-on-gitlab-ci">this question</a> and <a href="https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/124" rel="noreferrer">this GitLab issue</a> I see that </p>

<blockquote>
  <p>each job defined in .gitlab-ci.yml is run as separate build (where we
  assume that there's no history)</p>
</blockquote>

<p>But the alternative of lumping everything together in one stage isn't appealing</p>

<pre><code>stages:
  - test 

test:
  stage: test
  script:
    - if hash $HOME/miniconda/bin/conda 2&gt;/dev/null; 
      then
         export PATH="$HOME/miniconda/bin:$PATH";
      else
        wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh;
        bash miniconda.sh -b -p $HOME/miniconda;
        export PATH="$HOME/miniconda/bin:$PATH";
      fi
    - conda update --yes conda
    - conda env create --quiet --force --file environment.yml
    - source activate myenv
    - nosetests --with-coverage --cover-erase --cover-package=mypackage --cover-html
    - pylint --reports=n tests/test_final.py
    - pep8 tests/test_final.py
    - grep pc_cov cover/index.html | egrep -o "[0-9]+\%" | awk '{ print "covered " $1;}'
</code></pre>

<p>The only other option I can think of is to put the environment creation steps in a <a href="http://docs.gitlab.com/ce/ci/yaml/README.html#before_script" rel="noreferrer">before_script</a> stage, but it seems redundant to continuously recreate the same environment before each stage. </p>