google-cloud-build-slack
                                
                                 google-cloud-build-slack copied to clipboard
                                
                                    google-cloud-build-slack copied to clipboard
                            
                            
                            
                        Send messages to different slack channel based on trigger
Hello,
Although having a default slack channel to send notifications is desired, it would be nice to be able to override the channel for specific repos. Agree? And if so any ideas on how to implement this?
Do you actually need this currently? In my experience, we always have a single room with all the notifications to avoid spamming many rooms.
The feedback I get from my colleagues is that our GCB status channel is swamped with too many messages and they often do not pay attention as it is too noisey, whereas if the repos they are concerned with's notifications were sent to the slack channel that concerns that specific service they would pay more attention.
Whta about adding "webhook rules" (Mapping of rules to webhook urls")  in the config.json file, if any are defined they will be applied at runtime, if no rule matches or no rules are defined it will default to the good ol' SLACK_WEBHOOK_URL  ... thoughts?
something along the lines of
{
  "SLACK_WEBHOOK_URL": "https://hooks.slack.com/services/XXX",
  "SLACK_WEBHOOK_RULES": [{
      "rule": {
        "repoName": "foo"
      },
      "slack_webhook_url": "https://hooks.slack.com/services/XYZ"
    },
    {
      "rule": {
        "repoName": "bar",
        "maybesomething": "maybesomethingelse"
      },
      "slack_webhook_url": "https://hooks.slack.com/services/ZZZ"
    }
  ]
}
https://github.com/slackapi/node-slack-sdk/issues/857 created to see if there is a supported approach to this
Edit: they say it should be done at the app level
I know this is probably late.... but they did it here https://jonstacks.com/2019/07/06/customizing-cloud-build-notifications
It's similar to what I do to disable slack notifications for apps that we don't want it for.
You can just check if the substitute is there, and then use that value to override the value you set in index.js
Better late than never :) Thanks for sharing @LanceSandino ! So, something like (taken from article but modified to suit this issue's request):
  if (build.substitutions.hasOwnProperty('_SLACK_WEBHOOK_URL')) {
    console.log(`Build(id=${build.id}) contains a specific slack webhook URL, overriding the default webhook url...`);
  }
Is that right?
Exactly.... it would check if your cloudbuild.yaml has that substitution and then if it does, do whatever you want, which would be override the slack URL.
I do something similar here:
  // don't send slack messages if we have _DISABLE_SLACK in 'cloudbuild.yaml' file -- any value is accepted
  // this is a replacement of 'ignore specific branches'
  if (build.substitutions.hasOwnProperty('_DISABLE_SLACK')) {
    if (build.substitutions['_DISABLE_SLACK']) {
      return;
    }
  }
  // ignore these repos and don't send slack message
  const ignoredRepos = ['repo', 'repo2'];
  if (ignoredRepos.includes(build.substitutions.REPO_NAME)) {
    return;
  }
That's great! Provided we get @Philmod 's blessing, why not hereby join efforts in providing this flexibility?
If all parties agree,
'_SLACK_WEBHOOK_URL_OVERRIDE'
'_DISABLE_SLACK_WEBHOOK'
would be slightly better names for these looked-for build substitutions IMHO.