Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to schedule a pipeline, and only have slack notifications if it fails?

Victor Hearn-Yeates June 5, 2020

I'm looking at replacing one of our jenkins pipelines with bitbucket. Currently, we have nightly builds, however these require someone to go and manually check whether they've been successful.

I would like the new scheduled pipeline to do a nightly build, but only trigger an alert into a slack channel if it fails. Is this possible?

2 answers

0 votes
chare
Contributor
June 5, 2020

You can also set the chat notifications for your repository after you integrate bitbucket into a slack channel.  This works great, and I get a pass/fail notice after every pipeline run in my slack channel.  

Check out https://confluence.atlassian.com/bitbucket/notifications-for-bitbucket-pipelines-857053284.html

ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2020

But how to set this up especially for custom pipelines only (here: that have been triggered by the scheduler)? I didn't find that option and the original poster (OP) asked for that. Maybe you know more?

chare
Contributor
June 8, 2020

Open the repository

Open Repository Settings

Open Chat Notifications

- if you don't already have a subscription configured, then you need to do that first

Configure the notifications you want, like "pipeline failed"Screen Shot 2020-06-08 at 11.00.57 AM.png

ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 8, 2020

@chare Yes that dialog, unfortunately there is no differentiation between the type of a pipeline possible, e.g. only failed custom pipelines.

chare
Contributor
June 8, 2020

Yes - that is true.  does it not work for scheduled pipelines?  We don't use those, so I don't have any experience with them.

ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 8, 2020

I think it does work, it's just then it's for all failed Pipelines which might be too much for specific messaging.

chare
Contributor
June 8, 2020

if it doesn't overload the messaging system, it might get annoying for the recipients ;-)

0 votes
ktomk
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2020

The Slack integration does not cover your specific case, so a solution does involve a slightly higher overhead, however curl and a slack bearer token should do it:

  • Create an after-script for your custom step pipeline.
  • If the BITBUCKET_EXIT_CODE environment variable is non-zero, the pipeline step failed
  • Fire the message with curl to Slack

After Script Example (Docs / Announcement Jan 2019)

image: node:10.15.0

pipelines:
custom: # Pipelines that are triggered manually
sonar: # The name that is displayed in the list in the Bitbucket Cloud GUI
- step:
script:
- echo "Manual triggers for Sonar are awesome!"
after-script:
- test $BITBUCKET_EXIT_CODE -ne 0 && curl ...

For larger command lines I often find YAML multiline strings useful for better readability.

Curl Example (from Slack docs)

curl -X POST -H 'Authorization: Bearer xoxb-1234-56789abcdefghijklmnop' \
-H 'Content-type: application/json' \
--data '{"channel":"C061EG9SL","text":"I hope the tour went well, Mr. Wonka.","attachments": [{"text":"Who wins the lifetime supply of chocolate?","fallback":"You could be telling the computer exactly what it can do with a lifetime supply of chocolate.","color":"#3AA3E3","attachment_type":"default","callback_id":"select_simple_1234","actions":[{"name":"winners_list","text":"Who should win?","type":"select","data_source":"users"}]}]}' \
https://slack.com/api/chat.postMessage

/Edit: There is a similar question regarding doing this, not with slack but email notifications which looks like another good reference:

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events