11

I would like to prevent TODO comments (or other problematic strings) from being checked in with a gitlab CI test rule. I added the last line here:

.job_template: &template_test
  image: python:3.6-stretch
  tags:
    - python
  # ...

stages:
  - test

test:
  <<: *template_test
  stage: test
  script:
    - flake8 *.py
    - ! grep TODO *.py

But when I look at the output of the runner, it fails:

$ flake8 *.py
$ grep TODO *.py
ERROR: Job failed: exit code 1

It seems like Gitlab swallowed the exclamation mark !, used in the shell to negate the return value of grep.

phihag
  • 263,143
  • 67
  • 432
  • 458

2 Answers2

13

A line with the exclamation mark (! grep ...) at the beginning must be quoted. However, even this ('! grep ...') will not work here, return code will always be zero. I got the solution from https://stackoverflow.com/a/31549913/491884, a subshell must be started because GitLab CI starts the shell with set -e. This should work and is reasonably short:

script:
...
- (! grep TODO *.py)
jmuc
  • 1,501
  • 14
  • 16
2

The ! is a reserved character in YAML, therefore this does not work.

However, in this case you could use a if..then expression:

- if [ "$(grep TODO *.py)" != "" ]; then exit 1; fi
Philipp Ludwig
  • 2,947
  • 2
  • 24
  • 43