Send notification if build failure log contains specific error message or code
Asked by Devendra Sharma:

Hi,

I have a Bitbucket pipeline which internally calls different APIs for authentication and other functionalities. I want to get notification for all the pipeline failures. But if the failure log contains specific error message or error code, I would like send a notification to other teams as well. These team may change base based on the error code/message.

Is there a way to achieve this scenario in Bitbucket pipeline.


Answers:
author: o_kyrdan
body: Hi [@Devendra Sharma](/t5/user/viewprofilepage/user-id/4316593)

Good question!

Also, you can use [$BITBUCKET_EXIT_CODE variable](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Default-variables) in the [after-script](https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/#after-script) section.

**$BITBUCKET_EXIT_CODE** - the exit code of a step, if step success value setup to 0

with pipes for notification, alerting, monitoring

atlassian/datadog-send-event

```
script:
  - <some build logic>
after-script:
  - ALERT_TYPE="success"
  - if [[ $BITBUCKET_EXIT_CODE -ne 0 ]]; then ALERT_TYPE="error" ; fi
  - pipe: atlassian/datadog-send-event:1.1.2
    variables:
      API_KEY: $API_KEY
      ALERT_TYPE: $ALERT_TYPE
```

or atlassian/email-notify

```
build: &build
  step:
    name: Build
    script:
      - echo "Starting build..."
      #- <your build logic>
    after-script:
      - ALERT_TYPE="success"
      - if [[ $BITBUCKET_EXIT_CODE -ne 0 ]]; then ALERT_TYPE="error" ; fi
      - pipe: atlassian/email-notify:0.3.12
        variables:
          USERNAME: 'myemail@example.com'
          PASSWORD: $PASSWORD
          FROM: 'myemail@example.com'
          TO: 'example1@example.com, example2@example.com'
          HOST: 'smtp.gmail.com'
          SUBJECT: '${ALERT_TYPE}:Bitbucket Pipe Notification for ${BITBUCKET_BRANCH}'
```

To discover more pipes, visit **[Bitbucket Pipes Marketplace](https://bitbucket.org/product/features/pipelines/integrations)**.

Best regards,  
Oleksandr Kyrdan


author: Syahrul
body: Hi [@Devendra Sharma](/t5/user/viewprofilepage/user-id/4316593)

G'Day!

Currently, we don't have the feature to create a custom email notification for specific error messages in Bitbucket pipeline build logs.

We only sent email notification for failed build at this moment, but I have proceeded to raise a feature request on behalf of you, please **Vote** and **Watch** the feature request so that you'll receive an update whenever we have any at:

* <https://jira.atlassian.com/browse/BCLOUD-21569>

I hope this helps.

Cheers,  
Syahrul

comments: author: nareshit61
body: [@Syahrul](/t5/user/viewprofilepage/user-id/744536) Hello Folks, Is this issue fixed?

What I'm looking for in the pipeline? I'm running a set of Terraform commands in the bit bucket pipeline, Which will kick off commands one after another:

Terraform init

Terraform validate

Terraform plan

1. Plan success-\> send an email to a list of users-\>
2. Wait for approval that has been created Via.., "Jira service management"
3. maybe a Day/2 and \<Once approved, the pipeline has to kick the tf apply command.
4. Once the approved button is hit, Pipeline should have to run the "**terraform apply".**

Which i followed link on internet:

1. [@o_kyrdan](/t5/user/viewprofilepage/user-id/2862136) Is this worked?   
<https://community.atlassian.com/t5/Bitbucket-questions/Send-notification-if-build-failure-log-contains-specific-error/qaq-p/1878124>

2. [@Syahrul](/t5/user/viewprofilepage/user-id/744536) Will this works?  
<https://community.atlassian.com/t5/Bitbucket-questions/Bitbucket-Pipelines-amp-approvals/qaq-p/1659160>

i. <https://bitbucket.org/blog/a-modern-approach-to-change-management-with-> bitbucket-and-jira-service-management


Tags: bitbucket-pipelines

datadog

email

notification