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

问题描述 投票:5回答:3

我正在开发一个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阶段,但在每个阶段之前不断重新创建相同的环境似乎是多余的。

gitlab gitlab-ci gitlab-ci-runner
3个回答
8
投票

工作的独立性是一种设计特征。您可能已经注意到GitLab的界面允许您重新运行单个作业,如果作业相互依赖则无法执行。

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

缓存是在尽力而为的基础上提供的,因此不要指望缓存总是存在。有关实施细节,请查看GitLab Runner。

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

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


5
投票

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

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


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 >>
© www.soinside.com 2019 - 2024. All rights reserved.