Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invert a test in .gitlab-ci.yml

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.

like image 813
phihag Avatar asked Sep 03 '18 07:09

phihag


People also ask

What is lint in CI?

Lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs. The term originates from a Unix utility that examined C language source code.

What is Before_script in GitLab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

How do you test for CI lint?

The CI lint tool checks the syntax of GitLab CI/CD configuration, including configuration added with the includes keyword. To check CI/CD configuration with the CI lint tool: On the top bar, select Main menu > Projects and find your project. On the left sidebar, select CI/CD > Pipelines.


1 Answers

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)
like image 177
jmuc Avatar answered Sep 28 '22 11:09

jmuc