Recently I had a task to integrate Slack notification in Github Action workflow. Thanks to the repository slack-github-action, the work becomes much easier.
The Slack integration works well when the payload only contains text
and block
. However, an error popped up while I changed the payload only with attachments
. (We can add color to attachments
which will highlight the message in Slack channel)
The error was
axios post failed, double check the payload being sent includes the keys Slack expects\n Error: no_text
The first reaction after seeing the error is that I should add a text
into the payload. But it turned out that the Slack message will only show the text content and ignore the attachment.
The solution is when we want to display attachments
, we also need to add a basic blocks
field instead of text
field. Something like
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "bar"
}
}
],
"attachments": [
{
"color": "#FF0000",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "foo"
}
}
]
}
]
}
If putting the above payload into the Slack build kit, it will be a misleading. That’s also why I stuck with the issue.
I would recommend to use chat.postMessage test to debug the payload. It works like a charm.
If with the above payload and env SLACK_WEBHOOK_URL
still do not work in Github Action integration, I would suggest to try to add another env SLACK_WEBHOOK_TYPE
like below
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
The completed Github Action step job looks like:
- name: Send custom JSON data to Slack workflow
id: slack
uses: slackapi/slack-github-action@v1.18.0
with:
# For posting a rich message using Block Kit
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "bar"
}
}
],
"attachments": [
{
"color": "#FF0000",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "foo"
}
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!