visit
Create a Slack App:
2. Create an Incoming Webhook:
.github/workflows/
directory: If your repository doesn’t have this directory, create it in the root of your project..github/workflows/
directory. For example, you can name it slack-notifications.yml
.main
branch.name: Send Slack Notification
on:
push:
branches:
- main
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Send Slack Notification
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
curl -X POST -H 'Content-type: application/json' --data '{"text":"A new push has been made to the main branch."}' $SLACK_WEBHOOK_URL
Settings > Secrets and variables > Actions
.SLACK_WEBHOOK_URL
and paste your Webhook URL into the value field.
Example of a Detailed Slack Notification:
name: Send Detailed Slack Notification
on:
push:
branches:
- main
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Send Detailed Slack Notification
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
curl -X POST -H 'Content-type: application/json' --data '{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*New Push to Main Branch*"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repository:*\n'${{ github.repository }}'"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n'${{ github.ref }}'"
},
{
"type": "mrkdwn",
"text": "*Commit Message:*\n'${{ github.event.head_commit.message }}'"
},
{
"type": "mrkdwn",
"text": "*Committer:*\n'${{ github.event.head_commit.committer.name }}'"
}
]
}
]
}' $SLACK_WEBHOOK_URL
Explanation:
${{ github.repository }}
, ${{ github.ref }}
, and other GitHub context variables to dynamically insert information into the Slack message.
This will send a detailed notification to your Slack channel every time there’s a push to the main
branch, including information like the repository name, branch, commit message, and committer.
Pull Requests:
on:
pull_request:
branches:
- main
Workflow Failures:
on:
workflow_run:
workflows: ["CI"]
types:
- completed
Scheduled Events:
on:
schedule:
- cron: '0 9 * * 1' # Every Monday at 9 AM
Example for Workflow Failure Notification: This example sends a notification only if a workflow fails.
name: Notify on Failure
on:
workflow_run:
workflows: ["CI"]
types:
- completed
jobs:
notify_on_failure:
if: ${{ failure() }}
runs-on: ubuntu-latest
steps:
- name: Notify Slack on Failure
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
curl -X POST -H 'Content-type: application/json' --data '{
"text": ":x: *CI Workflow Failed*",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Workflow:* CI\n*Status:* Failed\n*Repository:* ${{ github.repository }}"
}
}
]
}' $SLACK_WEBHOOK_URL
This sends a failure notification with details about the workflow and the repository, ensuring your team is immediately aware of any issues.
Example: Conditional Notifications:
name: Notify on Success or Failure
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Run Tests
run: |
echo "Running tests..."
# Simulate a test failure
exit 1
notify:
needs: build
runs-on: ubuntu-latest
steps:
- name: Notify Slack
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
if [ "${{ needs.build.result }}" == "success" ]; then
curl -X POST -H 'Content-type: application/json' --data '{"text":"Build Succeeded!"}' $SLACK_WEBHOOK_URL;
else
curl -X POST -H 'Content-type: application/json' --data '{"text":"Build Failed!"}' $SLACK_WEBHOOK_URL;
fi
In this example, the notification is sent based on whether the build was successful or failed.