We have lots of builds running all day long, but we don’t want to flood the same Slack channel with build results for every branch. Here’s how we avoid the noise and make sure only the appropriate channels or individuals get notified.
First, we can add an app environment variable that defines which are the “main” (i.e. non-development) branches, using a simple regular expression:
In this example, it will provide us with the condition we check in order to choose the notification target.
Then, we add a “Script” step to our workflow, somewhere towards the end:
In this step, we can test the condition (are we on a “main” branch?), choose the appropriate Slack target, and finally use
envman to export it as an environment variable that can be used later:
#!/usr/bin/env bash set -ex if [[ $BITRISE_GIT_BRANCH =~ ^$MAIN_BRANCHES_REGEXP$ ]]; then SLACK_TARGET="#myapp_ci_builds" else SLACK_TARGET="@$(git log -1 --format=%ae | cut -d @ -f 1)" fi envman add --key SLACK_TARGET --value "$SLACK_TARGET"
Note that the
BITRISE_GIT_BRANCH environment variable is available generally only when the workflow is triggered by a push event. One can easily adapt the script for pull requests, tags, etc. by modifying or adding conditions to check the relevant environment variables.
In the above script, if there was a push to a “main” branch, then we’ll want to notify a dedicated channel. Otherwise, we assume the last commit author is the relevant notification target, and we transform their email address into their Slack username. (This works in my organization, but it might be less straightforward for others.) Finally, we use
envman to export the variable.
Finally, we insert the “Slack message” step and simply set the “target” to
bitrise.yml, it should look something like this:
# ... other stuff - script: title: Set Slack target is_skippable: true inputs: - content: |- #!/usr/bin/env bash set -ex if [[ $BITRISE_GIT_BRANCH =~ ^$MAIN_BRANCHES_REGEXP$ ]]; then SLACK_TARGET="#myapp_ci_builds" else SLACK_TARGET="@$(git log -1 --format=%ae | cut -d @ -f 1)" fi envman add --key SLACK_TARGET --value "$SLACK_TARGET" is_always_run: true - slack: inputs: - channel: "$SLACK_TARGET" # ... other stuff
Hope this helps someone, and feel free to comment if you find any improvements.