Github Actions: Post to slack on test failure

Natarajan Santhosh
1 min readJul 18, 2022

--

How to use conditional with github actions to post to slack

Prerequisites

  • Namespace - passed as an input from upstream job
  • smoke_test_status - from a previous job inside same actions yaml
  • Create a slack app - to get a bot token. enabled `chat.postMessage` permission
post-slack-extract-logs:
name: Post to Slack & Extract logs
runs-on: ubuntu-latest
needs: smoke-test
if: success() || failure() # don't use always(), that prevents workflow from ever being cancelled
steps:
- name: Post to a Slack channel only on failure in staging
run: |
echo "Namespace: ${{ inputs.namespace }}"
echo "Test status: ${{ needs.smoke-test.outputs.smoke_test_status }}"
echo "Is the namespace staging? ${{ inputs.namespace == 'staging' }}"
echo "Smoke test status failed? ${{ needs.smoke-test.outputs.smoke_test_status == 'failure' }}"
if [ "${{ inputs.namespace }}" == 'staging' ] && [ "${{ needs.smoke-test.outputs.smoke_test_status }}" == 'failure' ]; then
curl -v POST https://slack.com/api/chat.postMessage -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" -H "Content-type: application/json" -d '{"channel": "${{ secrets.SLACK_CHANNEL }}","text": "staging integration test status: ${{ needs.smoke-test.outputs.smoke_test_status }}\n"}'
fi

--

--

No responses yet