visit
Settings
> Secrets and variables
> Actions
.New repository secret
and add a new secret named SLACK_WEBHOOK_URL
with your Webhook URL as the value.Create a new file in your repository under .github/workflows/slack_notification.yml
.
name: Slack Notification
run-name: Pushing notification on Slack channel
on: [push]
name
: This defines the name of the workflow.run-name
: This is the name that will be displayed in the GitHub Actions interface when this workflow runs.on
: Specifies the event that triggers this workflow. In this case, it runs on every push.
jobs:
Notify-Slack-Channel:
runs-on: ubuntu-latest
jobs
: A workflow consists of one or more jobs that run sequentially or in parallel.Notify-Slack-Channel
: This is a custom name for our job.runs-on
: Specifies the type of runner to execute the job. Here, ubuntu-latest
is used.
steps:
- name: Calculate build start time
id: build_start_time
run: echo "BUILD_START_TIME=$(date +%s)" >> $GITHUB_ENV
steps
: A job contains a series of steps.name
: Describes what this step does.id
: Used to reference this step's outputs later.run
: Runs command-line scripts. Here, we use date +%s
to get the current Unix timestamp and save it to an environment variable ($GITHUB_ENV
).
- name: Checkout code
uses: actions/checkout@v2
uses
: Indicates the use of an action defined in another repository. actions/checkout@v2
checks out the repository code.
- name: Calculate build duration
id: calculate_duration
run: |
end_time=$(date +%s)
duration=$((end_time - $BUILD_START_TIME))
echo "duration=$duration" >> $GITHUB_ENV
echo "::set-output name=duration::$duration"
end_time=$(date +%s)
: Captures the current time at the end of the build.duration
: Computes the difference between the start and end times to get the build duration.$GITHUB_ENV
: Outputs the duration to the environment variable for use in later steps.
- name: Get short commit hash
id: short_commit
run: echo "SHORT_SHA=${GITHUB_SHA:0:7}" >> $GITHUB_ENV
SHORT_SHA=${GITHUB_SHA:0:7}
: Extracts the first 7 characters of the commit hash.Finally, use the slackapi/slack-github-action
to send the formatted Slack message:
- name: Send custom JSON data to Slack workflow
id: slack
uses: slackapi/[email protected]
with:
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*:white_check_mark: Succeeded GitHub Actions*"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repo*\n<//github.com/${{ github.repository }}|${{ github.repository }}>"
},
{
"type": "mrkdwn",
"text": "*Commit*\n<${{ github.event.head_commit.url }}|${{ env.SHORT_SHA }}>"
},
{
"type": "mrkdwn",
"text": "*Author*\n${{ github.event.head_commit.author.name }}"
},
{
"type": "mrkdwn",
"text": "*Job*\n`${{ github.job }}`"
},
{
"type": "mrkdwn",
"text": "*Event Name*\n`${{ github.event_name }}`"
},
{
"type": "mrkdwn",
"text": "*Workflow*\n`${{ github.workflow }}`"
},
{
"type": "mrkdwn",
"text": "*Build Logs*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Logs>"
},
{
"type": "mrkdwn",
"text": "*Took*\n`${{ steps.calculate_duration.outputs.duration }} sec`"
},
{
"type": "mrkdwn",
"text": "*Message*\n${{ github.event.head_commit.message }}"
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
with.payload
: The JSON-formatted payload that defines the structure and content of the Slack message.mrkdwn
: Slack's markup syntax to format text, which allows bolding with *text*
, links with <url|text>
, and more.env
: Environment variables used within the workflow.