Custom GitHub Action for Slack Notification: Sends Slack notifications when failed build jobs become green again
- Connects to the GitHub API to fetch information about workflow runs and jobs.
- Sends Slack notifications using a Slack bot token for authentication.
- Notifies about failed jobs in a workflow run, with relevant information and links.
- Optionally sends notifications for successful workflow runs based on the SEND_SUCCESS_NOTIFICATIONS environment variable.
- Send a success message if the last build failed but the current build was successful.
- Send a success message if the current build is successful after retrying the same workflow run with previously failed jobs.
- Send a message if there was a failed build.
- Added build duration in a successful build
slack_notification:
needs: [first-job, second-job]
if: always()
runs-on: ubuntu-latest
steps:
- name: Checkout codee
uses: actions/checkout@v3
- name: Run Slack Notification
uses: rohammosalli/slack-action@master
env:
SLACK_BOT_TOKEN: ${{ secrets.MY_SLACK_TOKEN }}
SLACK_CHANNEL: "Your Channel"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_RUN_ID: ${{ github.run_id }}
REPO_OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
RUN_ID: ${{ github.run_id }}
SEND_SUCCESS_MESSAGE: "true"
To disable the sending of Slack messages upon successful completion, simply set the environment variable SEND_SUCCESS_MESSAGE to 'false'.
- Go to https://api.slack.com/apps and sign in to your Slack account.
- Click the "Create New App" button.
- Choose a name for your app and select the workspace where you want the app to be developed, then click "Create App".
- From the "Add features and functionality" section, click on "Bots".
- Click the "Add a Bot User" button to add a bot user to your app. Fill in the required details and click "Add Bot User".
- Go to the "OAuth & Permissions" page in the sidebar menu.
- Scroll down to the "Scopes" section and add the necessary bot token scopes. For this code, you will need to add the chat:write scope.
- Scroll back up and click "Install App to Workspace". You will be prompted to authorize the app in your workspace. Click "Allow".
- Once the app is installed, you will see the "Bot User OAuth Token" under the "OAuth & Permissions" page. Copy this token and set it as the SLACK_BOT_TOKEN environment variable in your code.
- Invite the bot to the desired channel in your Slack workspace by typing @bot_username and selecting the bot from the list. Click "Invite".
- Sign in to your GitHub account and go to your account settings by clicking on your profile picture in the top-right corner.
- In the settings sidebar, click on "Developer settings".
- In the "Developer settings" sidebar, click on "Personal access tokens".
- Click the "Generate new token" button.
- Give your token a descriptive name in the "Note" field, so you can remember its purpose later.
- Select the necessary scopes for your token. For the provided code, you need to grant the repo scope, which allows the token to access repositories and perform various actions.
- Click the "Generate token" button at the bottom of the page.
- After generating the token, copy the token value. Important: This is the only time you will be able to see the token value, so make sure you copy it now.
- Set the copied token value as the GITHUB_TOKEN environment variable in your code.