Slack
Provides helpers for sending Slack notifications via the Slack Web API.
Usage
yaml
include:
- project: hosstio/gitlab-pipelines
file: helpers/slack.yamlVariables
| Variable | Default | Description |
|---|---|---|
SLACK_AUTHENTICATION_TOKEN | Slack Bot OAuth token; set as a masked CI/CD variable | |
SLACK_AUTHENTICATION_HEADER | Authorization: Bearer $SLACK_AUTHENTICATION_TOKEN | Authorization header |
SLACK_MESSAGE_TEXT | Hello world! | Message text |
SLACK_MESSAGE_CHANNEL | $CI_PROJECT_NAME | Channel to post to |
SLACK_MESSAGE_UNFURL_LINKS | false | Expand links in the message |
SLACK_MESSAGE_UNFURL_MEDIA | false | Expand media in the message |
SLACK_MESSAGE_BRANDING | by <https://gitlabpipelines.com|gitlabpipelines.com> | Branding text in the context block |
SLACK_MESSAGE_BLOCKS_BRANDING | Context block appended to SLACK_MESSAGE_BLOCKS; override to customise or remove the footer branding | |
SLACK_MESSAGE_BLOCKS | Block Kit blocks (JSON); defaults to SLACK_MESSAGE_BLOCKS_SIMPLE | |
SLACK_MESSAGE_BLOCKS_SIMPLE | Compact layout: message text + branding footer. Used by preview deployments | |
SLACK_MESSAGE_ATTACHMENTS | "" | Block Kit attachments (JSON) |
SLACK_MESSAGE_USER | "" | Sender name shown instead of the app's name; needs the chat:write.customize scope |
SLACK_MESSAGE_USER_ICON_URL | "" | URL of an image shown as the sender's icon instead of the app's; needs the chat:write.customize scope |
SLACK_MESSAGE_EMOTICON | "" | Emoji to use as the bot icon (e.g. :rocket:); overrides SLACK_MESSAGE_USER_ICON_URL and needs the chat:write.customize scope |
SLACK_ENDPOINT_AUTH_TEST | https://slack.com/api/auth.test | Endpoint used to verify the token |
SLACK_ENDPOINT_POST_MESSAGE | https://slack.com/api/chat.postMessage | Endpoint used to post messages |
Base (.slack)
Base job using the cloud-toolbox:base image. Extend this for custom Slack integrations.
Examples
Auth test (.slack_test)
Verifies the Slack Bot OAuth token against the auth.test endpoint.
Examples
yaml
verify-slack:
extends: .slack_testMessage (.slack_message)
Sends a Block Kit message to a Slack channel. Skips silently if SLACK_AUTHENTICATION_TOKEN is not set.
Examples
As a standalone notification job:
yaml
notify:
extends: .slack_message
variables:
SLACK_MESSAGE_CHANNEL: deployments
SLACK_MESSAGE_TEXT: "Deployed $CI_COMMIT_REF_NAME to production"Wired into an existing job's after_script via !reference so the notification fires on both success and failure:
yaml
deploy:
extends: .helm
after_script:
- export SLACK_MESSAGE_TEXT="Deployed $CI_COMMIT_REF_NAME to $ENVIRONMENT"
- !reference [.slack_message, script]