dockergitlab-ci

Simple commands are not working during pipeline execution of a docker image with gitlab-ci.yml


I have a repository in Gitlab, for which, I am trying to detect typos automatically with the gitlab-ci pipeline, using https://github.com/crate-ci/typos. I am using a custom docker image built from rust:slim-bullseye, where I install the tool and save it afterwards.

FROM rust:slim-bullseye
RUN cargo install typos-cli
WORKDIR /app
ENTRYPOINT ["tail", "-f", "/dev/null"]

I want to print the number of errors found in the repo. To do this, I tried using the following command: typos | grep "error" | wc -l. This works just fine in my local machine inside the docker container. However, the pipeline exits while trying to use the grep command. I don't know why.

...
 
image:
    name: my-image:latest
    entrypoint: [""]
  script:
    - echo "Checking typos..."
    - touch typos.txt
    - typos > typos.txt
    - echo "Typos check complete."
    - grep "error" typos.txt > grep.txt  ## Pipeline stops here
    - echo "Typos found in repo -> "
    - wc -l < grep.txt

pipeline output

I have tried:


Solution

  • The issue is likely that what you are grepping for doesnt exist. I.E the word error does not exist in your typos.txt. If you check the exit code of running grep locally. Although we dont get any error on screen. The exist code is non-zero. Indicating it didnt match anything

    root@vps-b21d2eed:/var/log# grep foo syslog > /tmp/foo.txt
    root@vps-b21d2eed:/var/log# echo $?
    1
    

    Gitlab, and infact most CI's are designed to stop the job and mark it as failed if any command finishes with a non-zero exit code. This makes sense in the majority of cases as why would you want to keep processing commands if one of the commands in the flow failed.

    However in your case it makes sense to continue. Gitlab covers this in this page https://docs.gitlab.com/ee/ci/yaml/script.html#ignore-non-zero-exit-codes.

    In your case you could just use true so this command always finishes with a zero exit code

    root@vps-b21d2eed:/var/log# grep foo syslog > /tmp/foo.txt || true
    root@vps-b21d2eed:/var/log# echo $?
    0
    

    That way you pipeline job should continue to the next command.

    For reference from the GREP man page

    EXIT STATUS Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. However, if the -q or --quiet or --silent is used and a line is selected, the exit status is 0 even if an error occurred.