GitLab CI在构建阶段之间保留环境

时间:2016-08-10 09:52:08

标签: gitlab gitlab-ci gitlab-ci-runner

我正在开发一个python项目并使用miniconda来管理我的环境。我正在使用GitLab for CI以及以下运行器配置

stages:
  - build
  - test 

build:
  stage: build
  script:
    - if hash $HOME/miniconda/bin/conda 2>/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;}'

我假设(错误地)我的build阶段会设置正确的环境,我可以在其中运行test阶段。查看this questionthis GitLab issue我看到了

  

.gitlab-ci.yml中定义的每个作业都作为单独的构建运行(我们在哪里   假设没有历史记录)

但是,在一个阶段将所有东西混为一谈的替代方案并不吸引人

stages:
  - test 

test:
  stage: test
  script:
    - if hash $HOME/miniconda/bin/conda 2>/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;}'

我能想到的唯一另一个选择是将环境创建步骤放在before_script阶段,但在每个阶段之前不断重新创建相同的环境似乎是多余的。

3 个答案:

答案 0 :(得分:8)

作业的独立性是一项设计功能。您可能已经注意到GitLab的界面允许您重新运行单个作业,如果作业相互依赖,则该作业是不可能的。

我不知道Miniconda的确切表现,但如果它在特定文件夹中构建虚拟环境,您可以使用 cache 在作业之间保留这些文件夹的内容。但是,您无法完全依赖它,因为文档说明了......

  

缓存是在尽力而为的基础上提供的,所以不要期望   缓存将始终存在。有关实施细节,请检查   GitLab Runner。

考虑到您的工作绝对取决于正在构建的环境,您需要一种机制来检测(缓存)环境是否存在,并仅在需要时重新创建它。

我认为您正在尝试将环境设置和作业分开,因为它可以节省大量时间,以防您有一天运行不同的测试同时(同一阶段的工作并行运行)。

答案 1 :(得分:5)

您可以使用artifacts在构建阶段之间传递文件。

但是,如果共享部分是环境内容(即它不是您编写的代码),那么您可能应该使用缓存。

答案 2 :(得分:2)

您可以将常用命令放在before_script下。它将应用于所有子阶段。这样,您无需重复代码。

您可以使用类似以下的内容:

stages:
  - build
  - test
  - deploy 

before_script:
  - if hash $HOME/miniconda/bin/conda 2>/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

build:
  stage: build
  script:
    - << Your build script >>

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;}'

deploy:
  stage: deploy
  before_script:
    - << Override to global before_script >>
    - << DO something else >>
相关问题