GitLab CI在构建阶段之间保留环境
我正在从事一个python项目,并使用它来管理我的环境。我将GitLab for CI与以下运行程序配置一起使用GitLab CI在构建阶段之间保留环境,gitlab,gitlab-ci,gitlab-ci-runner,Gitlab,Gitlab Ci,Gitlab Ci Runner,我正在从事一个python项目,并使用它来管理我的环境。我将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
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;}'
我(错误地)假设我的构建
阶段将设置正确的环境,在其中我可以运行测试
阶段。看着,我看到了
.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;}'
我能想到的唯一其他选择是将环境创建步骤放在一个阶段中,但在每个阶段之前不断地重新创建相同的环境似乎是多余的 工作的独立性是设计的一个特点。您可能已经注意到,GitLab的接口允许您重新运行单个作业,如果作业相互依赖,这是不可能的 我不知道Miniconda到底执行什么,但如果它在特定文件夹中构建虚拟环境,您可以使用在作业之间保留这些文件夹的内容。但是,您不能完全依赖它,因为文档说明 缓存是在尽力而为的基础上提供的,所以不要期望 缓存将始终存在。有关实施详情,请查看 GitLab Runner 考虑到您的作业完全取决于正在构建的环境,您需要一种机制来检测(缓存的)环境是否存在,并仅在需要时重新创建它 我认为您尝试将环境设置和作业分开是一条很好的途径,因为如果有一天您决定同时运行不同的测试(同一阶段的作业并行运行)。您可以使用它在构建阶段之间传递文件
但是,如果共享部分是环境内容(即,它不是您编写的代码),那么您可能应该使用缓存。您可以将常用命令放在下面。它将应用于所有子阶段。这样,您就不需要重复代码 您可以使用以下内容:
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 >>
阶段:
-建造
-试验
-部署
在脚本之前:
-如果散列$HOME/miniconda/bin/conda 2>/dev/null;
然后
导出路径=“$HOME/miniconda/bin:$PATH”;
其他的
wgethttp://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh;
bash miniconda.sh-b-p$HOME/miniconda;
导出路径=“$HOME/miniconda/bin:$PATH”;
fi
-康达更新——是康达
建造:
阶段:建造
脚本:
- >
测试:
阶段:测试
脚本:
-conda env create--quiet--force--file environment.yml
-源代码激活myenv
-nosetests——覆盖率——覆盖擦除——覆盖包=mypackage——覆盖html
-pylint——报告=n个测试/测试_final.py
-pep8测试/测试_final.py
-grep pc_cov cover/index.html | egrep-o“[0-9]+\%”| awk'{print“cover”$1;}”
部署:
阶段:部署
在脚本之前:
- >
- >
请注意,Gitlab的更新文档中缺少上述警告