在 .gitlab-ci.yml 中反转测试
Invert a test in .gitlab-ci.yml
我想防止使用 gitlab CI 测试规则签入 TODO 注释(或其他有问题的字符串)。我在这里添加了最后一行:
.job_template: &template_test
image: python:3.6-stretch
tags:
- python
# ...
stages:
- test
test:
<<: *template_test
stage: test
script:
- flake8 *.py
- ! grep TODO *.py
但是当我查看运行程序的输出时,它失败了:
$ flake8 *.py
$ grep TODO *.py
ERROR: Job failed: exit code 1
好像Gitlab把感叹号!
给吞了,用在shell到negate the return value of grep.
!
是 reserved character in YAML,因此这不起作用。
但是,在这种情况下,您可以使用 if..then
表达式:
- if [ "$(grep TODO *.py)" != "" ]; then exit 1; fi
必须引用开头带有感叹号 (! grep ...
) 的行。但是,即使这样 ('! grep ...'
) 在这里也不起作用,return 代码将始终为零。我从 得到了解决方案,必须启动子 shell,因为 GitLab CI 使用 set -e
启动 shell。这应该有效并且相当短:
script:
...
- (! grep TODO *.py)
我想防止使用 gitlab CI 测试规则签入 TODO 注释(或其他有问题的字符串)。我在这里添加了最后一行:
.job_template: &template_test
image: python:3.6-stretch
tags:
- python
# ...
stages:
- test
test:
<<: *template_test
stage: test
script:
- flake8 *.py
- ! grep TODO *.py
但是当我查看运行程序的输出时,它失败了:
$ flake8 *.py
$ grep TODO *.py
ERROR: Job failed: exit code 1
好像Gitlab把感叹号!
给吞了,用在shell到negate the return value of grep.
!
是 reserved character in YAML,因此这不起作用。
但是,在这种情况下,您可以使用 if..then
表达式:
- if [ "$(grep TODO *.py)" != "" ]; then exit 1; fi
必须引用开头带有感叹号 (! grep ...
) 的行。但是,即使这样 ('! grep ...'
) 在这里也不起作用,return 代码将始终为零。我从 得到了解决方案,必须启动子 shell,因为 GitLab CI 使用 set -e
启动 shell。这应该有效并且相当短:
script:
...
- (! grep TODO *.py)