How to prevent a step failing in Bitbucket Pipelines?
Asked Answered
M

3

19

I am running all my test cases and some of them get fail sometimes, pipeline detects it and fail the step and build. this blocks the next step to be executed (zip the report folder). I want to send that zip file as an email attachment.

Here is my bitbucket-pipelines.yml file

custom: # Pipelines that can only be triggered manually
  QA2: # The name that is displayed in the list in the Bitbucket Cloud GUI
  - step:
      image: openjdk:8
      caches:
      - gradle
      size: 2x    # double resources available for this step to 8G
      script:
      - apt-get update
      - apt-get install zip
      - cd config/geb
      - ./gradlew -DBASE_URL=qa2 clean BSchrome_win **# This step fails** 
      - cd build/reports
      - zip -r testresult.zip BSchrome_winTest 

      after-script: # On test execution completion or build failure, send test report to e-mail lists
      - pipe: atlassian/email-notify:0.3.11
        variables:
          <<: *email-notify-config
          TO: '[email protected]'
          SUBJECT: "Test result for QA2 environment"
          BODY_PLAIN: |
            Please find the attached test result report to the email.
          ATTACHMENTS: config/geb/build/reports/testresult.zip

Screenshot of build failure

The steps:

- cd build/reports 
and
- zip -r testresult.zip BSchrome_winTest

do not get executed because - ./gradlew -DBASE_URL=qa2 clean BSchrome_win failed

I don't want bitbucket to fail the step and stop the Queue's step from executing.

Monologue answered 1/8, 2020 at 10:31 Comment(1)
Please add some details about what the gradle config is running and the exact error message that is printedSuzettesuzi
T
22

The bitbucket-pipelines.yml file is just running bash/shell commands on Unix. The script runner looks for the return status codes of each command, to see if it succeeded (status = 0) or failed (status = non-zero). So you can use various techniques to control this status code:

Add " || true" to the end of your command

./gradlew -DBASE_URL=qa2 clean BSchrome_win || true

When you add "|| true" to the end of a shell command, it means "ignore any errors, and always return a success code 0". More info:

Use "gradlew --continue" flag

./gradlew -DBASE_URL=qa2 clean BSchrome_win --continue

The "--continue" flag can be used to prevent a single test failure from stopping the whole task. So if one test or sub-step fails, gradle will try to continue running the other tests until all are run. However, it may still return an error, if an important step failed. More info: Ignore Gradle Build Failure and continue build script?

Move the 2 steps to the after-script section

after-script:
  - cd config/geb # You may need this, if the current working directory is reset. Check with 'pwd'
  - cd build/reports
  - zip -r testresult.zip BSchrome_winTest 

If you move the 2 steps for zip creation to the after-script section, then they will always run, regardless of the success/fail status of the previous step.

Tver answered 1/8, 2020 at 11:57 Comment(2)
The first solution will always show the step as "Success" even if you have failing tests. The --continue would be only applicable I guess for gradle commands? Lastly, the after-script is hard to use for parallel steps. Is there an option for parallel steps / tests? So basically, I am running parallel tests using Cypress then last step is consolidate all tests then generate a report. I need to report failing tests and should show failed tests on the pipeline.Nibbs
Only a very small(or at least not useless) suggestion, now I use || echo $? instaed of using || true, giving one additional tracking information.Fernandafernande
W
9

A better solution

If you want all the commands in your script to execute regardless of errors then put set +e at the top of your script.

If you just want to ignore the error for one particular command then put set +e before that command and set -e after it.

Example:

- set +e 
- ./gradlew -DBASE_URL=qa2 clean BSchrome_win **# This step fails** 
- set -e 

Also valid for group of commands:

- set +e    
- cd config/geb 
- ./gradlew -DBASE_URL=qa2 clean BSchrome_win **# This step fails** 
- cd config/geb
- set -e 
Wheelman answered 14/12, 2020 at 17:43 Comment(1)
This solutions looks clean. However, it will always show as "Success" on the steps. Ideally, the step that failed should still show as "Failed" then continue to the next step (zip and report).Nibbs
S
0

I had a similar problem I had a command that normally takes 1 minute, but sometimes stalls and hits the 2 hour max build timeout (and corrupts my cypress installation)... I wrapped my command with the timeout command and then ORd the result with true

eg. I changed this:

- yarn

to this:

- timeout 5m yarn || yarn cypress install --force || true # Sometimes this stalls, so kill it if it takes more than 5m and reinstall cypress
- timeout 5m yarn # Try again (in case it failed on previous line). Should be quick
Sumrall answered 22/11, 2022 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.