Custom Slack Notifications for Approvals

There are newer abilities in Slack Notification step that will allow for custom approval notifications.

In some scenarios you may want to create a flexible Slack notification that will be sent to a specific Slack channel.

In Codefresh Steps Marketplace there is a step slack-notifier meant for this purpose.

It allows for flexible Slack notifications from a step in the pipeline using a message template.

You can also choose to set up these notifications using hooks to base the notification on the results of a pipeline or step.

Recently the slack-notifier step was expanded to include an “actions” definition.

Slack has a convenient Slack Message Builder tool available to help you build a message template to your liking and with that you can send a bespoke Slack notification to a specific Slack channel.

When you have a Slack channel incoming webhooks configured you can then use that to plug in the slack-notifier step to begin sending notification.

Now that we have actions available in the slack-notifier step you can send approval notifications to specific Slack channels.

Before your pending-approval step simply insert the slack-notifier step into your pipeline.

This will send a Slack Message with Approval buttons to a specific Slack channel.

SlackApprovalNotification:

type: slack-notifier

title: Sending notification to Slack...

arguments:

MODE: template

SLACK_TEXT: ${{CF_COMMIT_MESSAGE}}

SLACK_HOOK_URL: ${{SLACK_HOOK_URL}}

SLACK_TEMPLATE_BODY: '{"text": "<${{CF_BUILD_URL}}|Codefresh Build ${{CF_BUILD_ID}}>","attachments": [{"text": "","attachment_type": "default","fields": [],"actions": []}]}'

SLACK_TEMPLATE_FIELDS: '[{"title": "Repository","value": "${{CF_REPO_NAME}}","short": "true"},{"title": "Branch","value": "${{CF_BRANCH}}","short": "true"},{"title": "SHA", "value": "<${{CF_COMMIT_URL}}|${{CF_REVISION}}>","short": "true"},{ "title": "Pipeline","value": "${{CF_PIPELINE_NAME}}","short": "true"}]'

SLACK_TEMPLATE_ACTIONS: '[{"name": "approve","type": "button","text": "Approve","url": "${{CF_URL}}/build/${{CF_BUILD_ID}}/pending-approval/approve","style": "primary"},{"name": "deny","type": "button","text": "Deny","url": "${{CF_URL}}/build/${{CF_BUILD_ID}}/pending-approval/deny","style": "danger"}]'

PauseForApproval:

type: pending-approval

title: Waiting for Approval...

The result is a new message in the Slack channel configured for the webhook stored under the variable SLACK_HOOK_URL.

Which looks like…

A user can then Approve or Deny the pipeline from that Slack message and this will review the user’s permissions and make sure they have the appropriate Approve permission for the pipeline and either continue or terminate the pipeline.