'Continue running scripts even after exit code 1

I'm trying to run Cypress test in Gitlab. Below is the sample script. After executing 'npm run Cypress', if there is any test case fail, it exits with 'exit code 1' and next two commands won't run.

Is there a way I can execute next two commands. Next two commands, generates consolidated Jnuit and HTML report.

script: - cd ./cypress - npm ci - npm run Cypress - npm run mochawesome - npm run junit:merge

I have tried below mentioned solution but no luck.

script:
     - cd ./cypress
     - npm ci
     - npm run Cypress || exit 0
     - npm run mochawesome
     - npm run junit:merge

script:
  - cd ./cypress
  - npm ci
  - npm run Cypress
after_script:
  - npm run mochawesome
  - npm run junit:merge

output Image: enter image description here



Solution 1:[1]

One way would be instead of mentioning the exit code, which seems to be dynamic you can directly echo something after the || operator.

npm run Cypress || echo \"The previous command has some errors..Continuing\"

Solution 2:[2]

Using the after_script approach actually should work fine as you can see from this minimal example:

# .gitlab-ci.yml
test:
  image: alpine
  script:
    - echo "Hello after_script!" > test.txt
    - exit 1
  after_script:
    - cat test.txt

Output:

$ echo "Hello after_script!" > test.txt
$ exit 1
Running after_script
Running after script...
$ cat test.txt
Hello after_script!
Cleaning up file based variables
ERROR: Job failed: exit code 1

Solution 3:[3]

Also, you can consider using set +e and set -e to disable/enable exit on error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alapan Das
Solution 2 slauth
Solution 3 user3007799