Job scripts don't fail when command chained with AND operator fails

Summary

When two commands are chained using && (AND operator) in the script or before_script of a job and the first command fails, the script continues.

job:
  before_script:
    - wrongcommand xyz && wrongcommand abc
    - echo "This is executed."
  script:
    - echo "This is executed."

See gitlab-org/security-products/tests/js-npm!13597 (closed)

Further details

The current behaves is error prone. See gitlab-org/security-products/analyzers/gemnasium!358 (comment 1036853257) and https://gitlab.com/gitlab-org/security-products/tests/js-npm/-/jobs/2760742705 for instance.

However, this is consistent with how Bash behaves when option -e is set:

% bash -e
bash-3.2$ wrong
bash: wrong: command not found
(exited)

% bash -e
bash-3.2$ wrong && wrong
bash: wrong: command not found
bash-3.2$ echo OK
OK

Steps to reproduce

  1. Create a project.
  2. Set up a CI job with a script and before_script.
  3. In the before_script, chain 2 commands using &&, and make the first command fail.
  4. Trigger a pipeline.

The job and the pipeline are successful.

Example Project

https://gitlab.com/gitlab-org/security-products/tests/js-npm/-/blob/148c6fa695127f561e3f3e55bf261dac327fe8d7/.gitlab-ci.yml

job:
  image: alpine
  before_script:
    - wrongcommand xyz && wrongcommand abc
    - echo should not be executed
  script:
    - echo script should not be executed

What is the current bug behavior?

The before_script and the script are fully executed, and the job is successful.

What is the expected correct behavior?

The script or before_script stops as soon as a command fails, and the job fails.

Relevant logs and/or screenshots

https://gitlab.com/gitlab-org/security-products/tests/js-npm/-/jobs/2788421661

/bin/sh: eval: line 135: wrongcommand: not found
$ wrongcommand xyz && wrongcommand abc
$ echo should not be executed
should not be executed
$ echo script should not be executed
script should not be executed
Cleaning up project directory and file based variables
00:01
Job succeeded

Screenshot_2022-08-01_at_10.53.50

Output of checks

This bug happens on GitLab.com.

Possible fixes