Is there a way to create a slack subscription based off of a specific build rule?
Asked by Vincent Jong:

So we have a test repo that has a variety of pipelines based on test type, etc. We're using the slack subscription plugin and that seems every pipeline when run fires a message to the slack channel. Is there a way to add another subscription that only fires when a specific pipeline is run to another channel?

Sub 1 - repository wide, to slack_channel_1

can have a  

Sub 2 - pipeline 5, to slack_channel_2

?


Answers:
author: Theodora Boudale
body: Hi Vincent,

I assume you are using Atlassian's integration with Slack and you have set up subscriptions from **Repository settings \> SLACK Settings**?

If so, that should be possible as long as this specific pipeline runs on a certain branch. For example, it could be the default pipeline running on this specific branch, let's say develop, or a branch pipeline for develop branch.

You could then add a second subscription that sends messages to a different channel, and in that subscription remove the repository-wide notifications and add the branch develop with the relevant notifications for pipeline.

Please keep in mind that when a build runs on develop, the notifications will be sent to both channels (since the first channel has the repository-wide notifications set up).

Is this what you are looking for?

Kind regards,  
Theodora

comments: author: Vincent Jong
body: **Repository settings \> SLACK Settings? - correct.**   

Sort of... our test E2E pipelines all run against the main branch, so it sounds like you're saying what we want is not possible as set up. Is it possible to craft a workaround? You're correct that a run on this would sent to both channels but that's ok as the channels are set to different audiences...


author: Vincent Jong
body: Like maybe:

* Create a new branch to run the pipeline against
* In the new branch, pull from main branch when it has merged changes, or when the pipeline on the new branch is called to run?


author: Theodora Boudale
body: Hi Vincent,

Thank you for your answer.

You mention that your pipelines all run against the main branch. In the yml file you can specify only one pipeline for a certain branch (that runs on a **branches** definition). Are your multiple pipelines custom ones (see link below on what custom pipelines are) that you trigger manually or on schedule?

* <https://support.atlassian.com/bitbucket-cloud/docs/pipeline-start-conditions/#Custom--manual--pipelines>

I'm asking to better understand your use case and then create a feature request for our development team to consider and also to understand if the workaround you suggested would work.

If you indeed have custom pipelines, then the workaround with the new branch would work. You would need to merge the changes from the main branch before running the pipeline, to ensure the new branch has the latest changes of main branch. It would be good to communicate this set up with the rest of your team, to avoid any confusion about the purpose of this branch.

Kind regards,  
Theodora


author: Vincent Jong
body: So yes, all our rules in the pipelines yml are custom, except for the default which is run when a pr is created. The custom pipelines themselves are scheduled and manual where you can specify the branch. Scheduled ones are are all specified to target the main branch. And manual, when wanting to run are targeting whatever branch...

So what needs to be added to the default pull request target (\*\*) in bitbucket to achieve this? Or added to the specific targets that is in question.


author: Theodora Boudale
body: Hi Vincent,

If I understand correctly, you don't want the custom pipelines to post to a different Slack channel, but the **pull-requests** pipeline?

If you have a definition like this:

```
pipelines:
  pull-requests:
    '**':
```

this is going to run on all pull requests. The '\*\*' part indicates the source branch of the PR (and not the destination branch).

You don't need to change anything in the bitbucket-pipelines.yml file configuration, but in the SLACK Settings, you would need to match the names of the source branches.

For example, if your source branches have the prefixes bugfix/\* and feature/\*, you can add another subscription in SLACK Settings. In that subscription, add the branch pattern bugfix/\* and another row for branch pattern feature/\* with the pipelines notifications enabled.

When a **pull-requests** pipeline runs with a source branch that matches either of these two patterns, a notification will be sent to that second channel. Please keep in mind that if you run a custom pipeline for such a branch, the notification will also be sent in the second channel.

Is this what you are looking for?

Kind regards,  
Theodora


author: Vincent Jong
body: Not quite... I think it's along the lines of the PR default when after it runs, it would then call something that would update the branch to pull from main into the branch. A subscription to the branch would need to be set up where the branch subscription would notify its own slack channel...


author: Theodora Boudale
body: Hi Vincent,

Your setup and requirements are not very clear to me. I would like to ask a few questions to better understand what you are trying to do:

**1.** Can you please confirm that you are using a PR definition as follows or something different?

```
pipelines:
  pull-requests:
    '**':
```

**2.** Which **pull-requests** pipelines do you want to notify a different Slack channel? All of them? Or ones with a specific source branch?

**3.** Do all your pull requests have the same destination branch, e.g. main? Or can this be different?

I'm asking because the **pull-requests** pipelines work differently than pipelines that run on branches.

A pull-requests pipeline will merge the destination branch into the source branch before it runs. This is done in the **Build Setup** if you look at the Build Log on Bitbucket website. So, if you have a pull-requests pipeline for a PR with destination branch main, the source code of main is already merged into the source branch during the **Build Setup**.

Kind regards,  
Theodora


author: Vincent Jong
body: the yaml:

```
pipelines:
pull-requests:
  '**':
    - parallel: # these 2 steps will run in parallel
      steps:
        - step: *testEndToEndInDev
        - step: *testEndToEndInStg
  custom:
      e2e-smoke-dev:
        - step: *testSmokeDev
      e2e-smoke-stg:
        - step: *testSmokeStg
```

The smoke rules, we want to have these notify a different channel than the '\*\*' PR rule. If the smoke rules need to target a branch, they would need to be updated with the code in main. So the rules in '\*\*' would need to update the branch the smoke rules target by adding some command to the end of the '\*\*' rule I imagine?

But to directly answer **#2**, right now the rules themselves do not target any branch directly. The branch used is set when running a pipeline manually and when the PR rule is run automatically chooses the main branch. We don't set the branch directly in yaml.


author: Theodora Boudale
body: Hi Vincent,

Thank you for the details. So, I understand that you want the custom pipelines to target a different Slack channel than the pull-requests pipelines.
> If the smoke rules need to target a branch, they would need to be updated with the code in main.

Is that a requirement that is not related to Slack channel notifications?

<br />

I just thought of a different workaround, that will not involve creating additional branches.

If your Pipelines builds run on Atlassian's infrastructure or if you are using a self-hosted Linux Docker runner, you can use the following pipe to send a Slack notification (scroll down to see the pipe's README.md):

* <https://bitbucket.org/atlassian/slack-notify/src/master/>

What you could do is:

* set up only repository-wide notifications (from **Repository settings \> SLACK Settings**) to Slack channel 1 (all pipelines notifications will go there)
* edit the steps **testSmokeDev** and **testSmokeStg** in your bitbucket-pipelines.yml file and use the pipe to send a Slack notification to Slack channel 2

When you set up a Slack webhook to use with the pipe, you can configure which channel you want it to post.

You could use the pipe in the **after-script** of the steps so that it sends the notification both on success and failure. The default variable BITBUCKET_EXIT_CODE is available in the after-script, so you can adjust the message sent in Slack depending on the step's exit code. More details on the after-script here:

* <https://support.atlassian.com/bitbucket-cloud/docs/step-options/#After-script>

Would that work for you?

Kind regards,  
Theodora


author: Vincent Jong
body: Sorry for the dead time...  

> * set up only repository-wide notifications (from **Repository settings \> SLACK Settings**) to Slack channel 1 (all pipelines notifications will go there)
> * edit the steps **testSmokeDev** and **testSmokeStg** in your bitbucket-pipelines.yml file and use the pipe to send a Slack notification to Slack channel 2

The first we had already. The second, is that in the definitions or pipelines of the bit bucket yaml?


author: Syahrul
body: Hey [@Vincent Jong](/t5/user/viewprofilepage/user-id/5225653)

I believe what [@Theodora Boudale](/t5/user/viewprofilepage/user-id/818086) mentioned in the second step is to add the [Slack pipe](https://bitbucket.org/atlassian/slack-notify/src/master/) to notify your second Slack channel in YAML.

Let us know if you have any further questions.

Regards,  
Syahrul


Tags: bitbucket-cloud

cloud