我正在尝试创建一个gitlab作业,以显示测试代码覆盖率的指标。为此,我正在创建一个.coverage
文件并将其放置在上载工件的目录中。在随后的阶段中,工件将被覆盖工具下载并使用,以生成覆盖报告。我注意到,当gitlab运行程序完成工作并膨胀我的文件系统时,不会删除工件。上传工件后如何删除工件目录?
这是我们目前拥有的
stages:
- test
- build
before_script:
- export GITLAB_ARTIFACT_DIR="$(pwd)"/artifacts
[...]
some-test:
stage: test
script:
- [some script that puts something in ${GITLAB_ARTIFACTS_DIR}
artifacts:
expire_in: 4 days
paths:
- artifacts/
some-other-test:
stage: test
script:
- [some script that puts something in ${GITLAB_ARTIFACTS_DIR}
artifacts:
expire_in: 4 days
paths:
- artifacts/
[...]
coverage:
stage: build
before_script:
script:
- [our coverage script]
coverage: '/TOTAL.*\s+(\d+%)$/'
artifacts:
expire_in: 4 days
paths:
- artifacts/
when: always
[...]
after_script:
- sudo rm -rf "${GITLAB_ARTIFACT_DIR}"
根据https://gitlab.com/gitlab-org/gitlab-runner/issues/4146的 after_script
规定,不能访问before_script
或scripts
环境变量。
一个解决办法是使用cache
和artifact
同时进行。
此配置将根据$CI_JOB_ID
每个作业执行的作业ID()创建一个新目录:
stages:
- test
remote:
stage: test
script :
- mkdir cache-$CI_JOB_ID
- echo hello> cache-$CI_JOB_ID/foo.txt
cache:
key: build-cache
paths:
- cache-$CI_JOB_ID/
artifacts:
paths:
- cache-$CI_JOB_ID/foo.txt
expire_in: 1 week
在下一次运行时,先前的cache-$CI_JOB_ID
将被删除并替换为新目录(因为目录$CI_JOB_ID
将有所不同)。这将只保留一个缓存文件实例,直到执行下一个作业。
注意:您必须在目录名称前加上,cache-
否则.gitlab-ci.yml
无效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句