if-success-failure-slack
Implement GitHub Actions to send Slack notifications for job success or failure. Configure alerts for your CI/CD pipelines with this guide.
GitHub Actions Slack Notification
Automate CI/CD Notifications with Slack
This guide demonstrates how to configure GitHub Actions to send automated Slack notifications based on the success or failure of your CI/CD workflows. By integrating Slack, your team can stay informed about build statuses, enabling quicker responses to issues and smoother deployment processes.
Workflow Trigger and Conditions
The provided GitHub Actions workflow is designed to trigger on a push event to the master
branch, specifically when changes are detected within the alertmanager/
directory. This ensures that the workflow runs only when relevant code is modified and merged.
Commit Message Exclusion
A crucial condition is applied to the master
job: if: "!contains(github.event.head_commit.message, '[skip-ci]')"
. This allows developers to bypass the CI/CD pipeline and Slack notifications by including the tag [skip-ci]
in their commit message, providing flexibility during development.
Executing Workflow Steps
The workflow consists of several steps executed on an ubuntu-latest
runner:
- Checkout: The
actions/checkout@v2
action is used to fetch the repository's code. - Run a single line: Demonstrates executing a simple command and accessing environment variables, including dummy secrets.
- Run a multi-line script: Shows how to execute a sequence of commands within a single step.
Slack Notification Integration
The core functionality involves integrating with Slack using the Ilshidur/action-slack@master
action. This action allows for conditional sending of messages based on job outcomes.
Slack Notification on Success
The Slack Notification on Success
step is configured to run only when the preceding job succeeds, using the condition if: ${{ success() }}
. It sends a success message to the builds-notifications
channel, including repository details and the actor who triggered the workflow.
Slack Notification on Failure
Conversely, the Slack Notification on Failure
step executes when the job fails, utilizing the condition if: ${{ failure() }}
. This step sends a failure alert to the same Slack channel, highlighting that the deployment was not successful and providing relevant context.
Both Slack notification steps utilize environment variables for configuration, including the SLACK_WEBHOOK
(a secret), SLACK_USERNAME
, SLACK_CHANNEL
, and a customizable SLACK_CUSTOM_PAYLOAD
for detailed message formatting.
For more information on GitHub Actions and Slack integrations, refer to the GitHub Action for Slack and the GitHub Actions context and expression syntax.
# this workflow will only trigger when the alertmanager directory is modified
# and will only be trigger when merged to master
name: alertmanager-deploys
on:
push:
branches:
- master
paths:
- 'alertmanager/*'
jobs:
master:
if: "!contains(github.event.head_commit.message, '[skip-ci]')"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: run a single line
run: echo "${GITHUB_RUN_ID}"
env:
DUMMY_SECRET: ${{ secrets.DUMMY_SECRET }}
- name: run a multi line
run: |
echo "starting"
echo "finished"
# https://github.com/marketplace/actions/github-action-for-slack
- name: Slack Notification on Success
# https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#job-status-check-functions
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idsteps
if: ${{ success() }}
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_USERNAME: AlertManager
SLACK_CHANNEL: builds-notifications
SLACK_AVATAR: repository # Optional. can be (repository, sender, an URL) (defaults to webhook app avatar)
SLACK_CUSTOM_PAYLOAD: '{"text":"[*SUCCESS*] Alertmanager from was deployed\n *Repo*: [{{ GITHUB_REPOSITORY }}]({{ GITHUB_REPOSITORY }})\n *User*: `{{ GITHUB_ACTOR }}`","username": "{{ GITHUB_ACTOR }}"}'
uses: Ilshidur/action-slack@master
- name: Slack Notification on Failure
if: ${{ failure() }}
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_USERNAME: AlertManager
SLACK_CHANNEL: builds-notifications
SLACK_AVATAR: repository # Optional. can be (repository, sender, an URL) (defaults to webhook app avatar)
SLACK_CUSTOM_PAYLOAD: '{"text":"[*FAILURE*] Alertmanager from was not deployed\n *Repo*: [{{ GITHUB_REPOSITORY }}]({{ GITHUB_REPOSITORY }})\n *User*: `{{ GITHUB_ACTOR }}`","username": "{{ GITHUB_ACTOR }}"}'
uses: Ilshidur/action-slack@master