我正在开发一个python项目并使用miniconda来管理我的环境 . 我使用GitLab进行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 question和this 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 回答
作业的 independence 是一项设计功能 . 您可能已经注意到,如果作业相互依赖,GitLab可以实现 .
我不知道Miniconda的确切执行情况,但如果它在特定文件夹中构建虚拟环境,您可以使用 cache 来保留作业之间这些文件夹的内容 . 但是,您不能完全依赖它,因为文档说明......
考虑到您的工作绝对取决于正在构建的环境,您需要一种机制来检测(缓存)环境是否存在,并仅在需要时重新创建它 .
我认为你正在尝试将环境设置和工作分开,因为它可以节省大量时间,以防你有一天决定 simultaneously (同一阶段的工作并行运行) .
您可以使用artifacts在构建阶段之间传递文件 .
但是,如果共享部分是环境内容(即它不是您编写的代码),那么您可能应该使用缓存 .
您可以将常用命令放在before_script下 . 它将适用于所有儿童阶段 . 这样,您就不需要重复代码了 .
您可以使用以下内容: