Patryk Furman
Patryk Furman

Reputation: 53

For loop in gilab-ci exits when inner command produces non-zero exit code

I'm trying to launch mypy code analyze inside my gitlab-ci. I do it with following code:

mypy:
  only:
  - master
  script:
  - for config_path in $(find * -regex '.*__init__.py' -mindepth 2); do mypy $config_path; exit_code=$?; done
  allow_failure: true

Unfortunately CI stops after executing mypy on first element from list found by find command. Command mypy returns RC=2.

I put extraction of exit code into variable because of warning: If any of the script commands return an exit code different from zero, the job will fail and further commands will not be executed. This behavior can be avoided by storing the exit code in a variable mentioned here: https://docs.gitlab.com/ee/ci/yaml/README.html#script

How can I achieve launching whole for loop despite of non-zero exit codes inside its body?

Upvotes: 1

Views: 832

Answers (4)

SmilingSisyphus
SmilingSisyphus

Reputation: 11

I avoided the exit on the non-zero exit code by executing the following command in a sub-shell:

bash -c "set +e; for i in {1..30}; do (INSERT YOUR COMMAND HERE) && exit 0; sleep 1; done; exit 1"

Upvotes: 0

Grumbunks
Grumbunks

Reputation: 1277

try

do mypy $config_path || true; done

|| true will force a return code of 0 no matter what return code mypy $config_path has.

Upvotes: 3

MrBerta
MrBerta

Reputation: 2758

I would just put the script inside of text file, add it to git, and just call it from .gitlab-ci.yml. Something like:

//run_mypy.sh
for config_path in $(find * -regex '.*__init__.py' -mindepth 2); do mypy $config_path; exit_code=$?; done

//.gitlab-ci.yml
mypy:
  only:
   - master
  script:
   - run_mypy.sh
  allow_failure: true

I have tried and struggled too many times to get complex scripts to run directly from the .gitlab-ci.yml file. In my case it was windows batch files and powershell scripts, but they all suffer from the same problem: You have a yaml file that supports a certain format and that uses certain special characters, and inside of this you try to express a completely different format.

It is also much easier to control an exit code from a separate script then to try to figure out exactly how GitLab handles exit codes. Again, this might mostly be a problem for powershell scripts, but I would still suggest everybody to store your complex scripts outside of .gitlab-ci.yml.

Upvotes: 0

644
644

Reputation: 639

I'm not sure if gitlab-ci supports <() but in bash this would be

while read -r -d '' config_path; do mypy "$config_path"; exit_code=$?; done < <(find . -mindepth 2 -name '*__init__.py' -print0)

The error was likely due to -mindepth 2 coming after the -regex flag, however -name or -iname are probably better for this

Upvotes: 0

Related Questions