Coder Social home page Coder Social logo

ravsamhq / notify-slack-action Goto Github PK

View Code? Open in Web Editor NEW
162.0 2.0 57.0 311 KB

๐Ÿ”” Send a Slack Notification from Github Actions regarding failure, warnings, or even success.

Home Page: https://ravsam.in/blog/send-slack-notification-when-github-actions-fails/

License: MIT License

TypeScript 98.15% JavaScript 1.85%
github-actions github-action slack-bot slack-actions workflow-status-notifications slack workflow notifications actions github

notify-slack-action's People

Contributors

bmbferreira avatar borda avatar cread avatar dependabot[bot] avatar joshmfrankel avatar ravgeetdhillon avatar ravsam-bot[bot] avatar renovate-bot avatar renovate[bot] avatar vladkens avatar vmaerten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

notify-slack-action's Issues

requests.exceptions.MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None?

Not sure what I'm doing wrong, I have 2 notifications in a specific job (start and end), the first one works fine and the second one fails with this:

Traceback (most recent call last): File "/app/main.py", line 200, in <module> main() File "/app/main.py", line 195, in main notify_slack(payload) File "/app/main.py", line 169, in notify_slack requests.post(url, data=payload, headers=headers) File "/app/requests/api.py", line 117, in post return request('post', url, data=data, json=json, **kwargs) File "/app/requests/api.py", line 61, in request return session.request(method=method, url=url, **kwargs) File "/app/requests/sessions.py", line 528, in request prep = self.prepare_request(req) File "/app/requests/sessions.py", line 466, in prepare_request hooks=merge_hooks(request.hooks, self.hooks), File "/app/requests/models.py", line 316, in prepare self.prepare_url(url, params) File "/app/requests/models.py", line [39](https://github.com/niio-art/one/runs/5212989943?check_suite_focus=true#step:16:39)0, in prepare_url raise MissingSchema(error) requests.exceptions.MissingSchema: Invalid URL 'None': No schema supplied. Perhaps you meant http://None?

The second notification is as simple as this:
- name: Slack notification if: always() uses: ravsamhq/notify-slack-action@v1 with: token: ${{ secrets.GITHUB_TOKEN }} status: ${{ job.status }} notification_title: 'Build finished'

Slack notification not working

I have tried to send the slack notification with slack webhook but it stopped working from last week. My action code is as below.

  - name: slack-notify
    uses: ravsamhq/notify-slack-action@v1
    if: always()
    with:
      status: Notification
      notification_title: 'New code in pull-request is {status_message} in SonarQube analysis, need review for pull request approval'
      message_format: '{emoji} Workflow *{workflow}* has {status_message} in <{repo_url}|{repo}>. Static Code Analysis results are in URL: https://sonarcloud.io/dashboard?id=${{ github.event.repository.name }}&branch=${GITHUB_REF#refs/heads/}'
    env:  
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

Request for "{branch_url}" OR usage of environment variables in the message_format

I think this variable will be very useful.
It is basically:
{repo_url}/tree/{branch}, just need to remove the "refs/heads/" prefix from the branch variable.

Alternatively, if I can use environment variables, it will also solve my issue, I could construct the URL with my "CI_BRANCH" env var, like so:
message_format: 'Branch <{repo_url}/tree/$CI_BRANCH|{branch}> on commit <{commit_url}|{commit_sha}>'

Using @channel for notifications

Hi, I have the following

      - name: Report Status Failure
        uses: ravsamhq/notify-slack-action@v1
        if: failure()
        with:
          status: ${{ job.status }}
          notification_title: '@channel {workflow} has {status_message}'
          message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}> - <{run_url}|View Run>'
          footer: 'Linked Repo <{repo_url}|{repo}>'
          notify_when: 'failure'
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

that just pastes raw "@channel" rather than actually notifiying. I followed this issue to attempt to get it to work right but still no success

Publish a docker image

Would it be possible to publish a docker image so using this action won't require building the docker image each time?

Thanks for supporting this GHA!

`notify_when: 'failure'` notifies when workflow passes with warnings

We are seeing notifications for passing builds with "warnings" in Slack when we have notify_when: 'failure' set.

This only happens on a job that runs a Bash script in which there's a fallback for the initial action failing.

Screen Shot 2022-07-26 at 12 30 33 PM

Our workflow looks like this:

name: Mergedown
on:
  push:
    branches:
      - main
    paths-ignore:
      - '*/CHANGELOG.md'
      - CHANGELOG.md
      - package.json
jobs:
  mergedown:
    name: Mergedown
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Source Code
        uses: actions/[email protected]
        with:
          fetch-depth: 0
          token: ${{ secrets.MERGEDOWN_TOKEN }}
      - name: Merge Updates from Main into Release Branches
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          git config user.name "${{ github.actor }}"
          git config user.email "${{ github.actor}}@users.noreply.github.com"
          for BRANCH in $(git branch --all --list | grep remotes/origin/release | sed 's/remotes\/origin\///g')
          do
            echo "Merging 'main' into '$BRANCH'..."
            git checkout $BRANCH
            git merge --no-verify origin/main && git push origin $BRANCH || (
              echo "Automatic mergedown failed for $BRANCH. Finding PR..."

              PR=$(
                gh api graphql \
                  --jq '.data.repository.pullRequests.nodes[0].id' \
                  -F branch="$BRANCH" \
                  -f query='
                    query FindPullRequest($branch: String!) {
                      repository(owner: "[REDACTED]", name: "[REDACTED]") {
                        pullRequests(headRefName: $branch, first: 1) {
                          nodes {
                            id
                          }
                        }
                      }
                    }
                  '
              )

              if [[ ! -z $PR ]]; then
                echo "Commenting on pull request with global ID $PR"

                gh api graphql \
                  -F pr=$PR \
                  -f query='
                    mutation AddComment($pr: ID!) {
                      addComment(input: {
                        body: "Mergedown of ${{ github.sha }} has failed.",
                        subjectId: $pr
                      }) {
                        clientMutationId
                      }
                    }'
              fi

              git merge --abort
            )
          done
      - name: Report Workflow Failure to Slack
        if: always()
        uses: ravsamhq/notify-slack-action@v1
        with:
          status: ${{ steps.decider.outputs.result }}
          notify_when: 'failure'
          message_format: '{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}>'
          notification_title: Mergedown failed. This is probably an issue with the workflow, not an individual branch.
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_SOFTWARE_WEBHOOK_URL }}

This job passes in GitHub, and it's unclear from the output why it is throwing any warnings:

Screen Shot 2022-07-26 at 12 31 21 PM

I suspect it has something to do with run's output and me misunderstanding how the (do this) || (or this if that doesn't work) technique in bash for falling back to another command if the first one fails, but either way I do see that notify_when allows me to specify if I want "passed with warnings" to be one of the statuses, so I'm thinking this action shouldn't be sending me the notification regardless.

edit:

This line of code might be the culprit: status: ${{ steps.decider.outputs.result }}

I copy/pasted this particular action from another workflow which has a "decider" step, but this one doesn't. I guess GitHub still lets this through but might just set this to status: null? Anyway, this could be the issue right here, as when I changed this to just use ${{ job.status }}, my warning notifications went away.

Is it possible that this action threw the warning, and it was just buried because it was the one actually doing the work of notifying me?

Security warnings with pip.

Screenshot 2021-10-12 at 15 01 41

Notice in the 4th step out of 9, it says that pip :

  1. Should not have root access.
  2. Used version of pip is outdated.

Feature request: Send file to slack channel

I want to send a text file to slack channel on specific event. Right now I am not sure whether this action provides option to send file to slack channel. Kindly confirm if there is such option available or if not can we implement it.

Request for workflow_url in message

Hi ๐Ÿ‘‹

I have a small request, could you please add support for having GHA workflow_url in message as var available for formatting?

In addition, I might just have identified a bug in {repo_url} that points out or refs/heads instead of master. Due to that I cannot navigate through repo and need to change a branch manually.

That would be appreciated.
Thanks,
Przemek

Support mention_groups: !channel

Hey and thanks for your nice plugin.

I would like to mention all users in a channel like @channel in slack. this could be done programmatically using <!channel>. The current implementation of mention_groups appends !subteam^ to the group name. It would be great if it only appends if the group doesn't start with !

Error: No SLACK_WEBHOOK_URL provided after upgrading to v2?

Hello,

I just upgraded to ravsamhq/notify-slack-action@v2 and am now seeing failures in the announce to slack action with the error thrown reading: Error: No SLACK_WEBHOOK_URL

We are however providing a SLACK_WEBHOOK_URL as seen in our config below:

- name: Announce on Slack
        if: always()
        uses: ravsamhq/notify-slack-action@v2
        with:
          status: ${{ job.status }}
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

Might you be able to provide any insights?
Screen Shot 2022-09-09 at 4 05 33 PM

Unable to run the actions getting docker error

Build container for action use: '/home/runner/work/_actions/ravsamhq/notify-slack-action/master/Dockerfile'.
/usr/bin/docker build -t 8a33c1:65b5d5e5223c45dbaa102693b1c509c8 -f "/home/runner/work/_actions/ravsamhq/notify-slack-action/master/Dockerfile" "/home/runner/work/_actions/ravsamhq/notify-slack-action/master"

ERROR: Cannot install -r requirements.txt (line 10) and astroid==2.5.7 because these package versions have conflicting dependencies.

Please add support for Windows/Mac OS X

This seems like an awesome utility, but doesn't seem to work at all on Windows/Mac OS X because I'm getting the error: Error: Container action is only supported on Linux

Ability to print the "name" property of the job

We have reusable job which runs on different parameters and also the name of the job is generated dynamically.

jobs:
  test:
    name: Run ${{ inputs.param1 }} tests on ${{ inputs.param2 }}
    runs-on: ubuntu-latest

With a job setup something lie this, the name always prints as "test" in the slack notification instead of the name generated at runtime

image

Job status shows always as success in Composite action

Hi,

I am using this action inside a composite action. When I pass the job status from the triggering workflow to the composite action, it always comes up as success.

Is there any way to set/get job status correctly when using this action inside a composite action?

Thanks

Request: Add "skipped" status

Hi @ravgeetdhillon, many thanks for sharing this super-useful GitHub action with us!

On the job.status, it looks like "skipped" status has been missed out as a potential option. "skipped" is a valid, documented result/conclusion for both jobs and steps contexts so it'd be really appreciated if it could be captured similar to "cancelled".

It might seem odd, but I leverage notify-slack-action@v2 as the last in a chain of jobs which outputs their overall status like shown below. So that how's we can potentially end up with the "skipped" status needing to notify Slack.

status: ${{ needs.build.result || needs.scan.result || needs.deploy.result }}

Cheers,
Rishav

Canceled runs are notified as "passed with warnings"

I'm using notify-slack-action@v2. When I manually interrupt or cancel the workflow, a wrong notification is made saying the job has been passed with warnings. It should instead say something like the job was cancelled or failed.

:large_orange_diamond: Run Tests passed with warnings in
      - name: Report status to Slack
        if: always()
        uses: ravsamhq/notify-slack-action@v2
        with:
          status: ${{ job.status }}
          notify_when: "failure,cancelled"
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.ACTION_NOTIFICATION_SLACK }}

Unable to resolve action at version v2.3.0

Screenshot 2023-05-25 at 8 41 37 AM
Hello,

I'm trying to use version v2.3.0 of the ravsamhq/notify-slack-action in my GitHub Actions workflow, but I'm encountering an issue where the runner can't seem to find this version. Here's the error message I receive:

Error: Unable to resolve action ravsamhq/[email protected], unable to find version v2.3.0.

I've confirmed that version v2.3.0 does exist in the releases section of your repository. My workflow file references the action as follows:

I would appreciate any assistance in resolving this issue. Thanks!

Screenshot 2023-05-25 at 7 37 43 AM

Is there a way to enable caching?

Screenshot 2022-06-08 at 17 36 55

This step (above) takes 16 seconds each time the Github Action is run.
If caching is enabled then this time is likely to reduce drastically.

Example:

- uses: actions/setup-node@v3
   with:
      node-version: '16.15.1'
      cache: 'yarn'

Kindly let us know if it possible already or if it can be put in place.

Conditional post w.r.t. branch

Very useful action, thanks! I was wondering whether it's possible to post to Slack only if the workflow is triggered for a certain branch, e. g. main?

Improve message, p2

Hello,
please improve this action a little bit:

Regards,
Andrii

Provide more documentation for usage

Hello! I'm new to GitHub actions and I'm confused how this should be implemented. When I set it up per the sample repo, I got a notification that this action was checked out properly, but it didn't notify me about the actual workflow step I wanted to receive notifications for.

Would it be all right to request some more docs?

randomly the pre-action docker build fails

Once in a while Build ravsamhq/notify-slack-action@v1 fails with the following

  /usr/bin/docker build -t 5eae13:03b041c796e34d099842964119063d7c -f "/path/actions-runner/_actions/ravsamhq/notify-slack-action/v1/Dockerfile" "/path/actions-runner/_actions/ravsamhq/notify-slack-action/v1"
  #1 [internal] load build definition from Dockerfile
  #1 sha256:76500476c126f4b5da5ec50421f6358c73853c0dbe71984004e53b6c61712ad7
  #1 transferring dockerfile: 38B done
  #1 DONE 0.0s
  
  #2 [internal] load .dockerignore
  #2 sha256:b78347fbd62912480d9947bb0aeef597af6c1ea1d2cd959bdcca6fed7f3e9e50
  #2 transferring context: 2B done
  #2 DONE 0.0s
  
  #4 [internal] load metadata for public.ecr.aws/docker/library/python:3-slim
  #4 sha256:88f83c7105d19eeb7cd4d26d9ce011e2df8ab9dd0dad2e9ce84771f012f3b2e0
  #4 ERROR: unexpected status code [manifests 3-slim]: 400 Bad Request
  
  #3 [internal] load metadata for gcr.io/distroless/python3-debian10:latest
  #3 sha256:847d648b5ae1c07de239ad1cf534dcbbc90f051f65da51ccb91ed47a5bce342c
  #3 CANCELED
  ------
   > [internal] load metadata for public.ecr.aws/docker/library/python:3-slim:
  ------
  failed to solve with frontend dockerfile.v0: failed to create LLB definition: unexpected status code [manifests 3-slim]: 400 Bad Request
  Warning: Docker build failed with exit code 1, back off 7.569 seconds before retry.
  /usr/bin/docker build -t 5eae13:03b041c796e34d099842964119063d7c -f "/path/actions-runner/_actions/ravsamhq/notify-slack-action/v1/Dockerfile" "/path/actions-runner/_actions/ravsamhq/notify-slack-action/v1"
  #1 [internal] load build definition from Dockerfile
  #1 sha256:d08f37c7dc52a4acce3f973a8fc7e2cd213ce57277830df51bc2006e3e390f0b
  #1 transferring dockerfile: 38B done
  #1 DONE 0.0s
  
  #2 [internal] load .dockerignore
  #2 sha256:cd044e6bb5820c413cbbf9ac9ce721aba7162651f3994d5eecaabcd0413ba007
  #2 transferring context: 2B done
  #2 DONE 0.0s
  
  #4 [internal] load metadata for public.ecr.aws/docker/library/python:3-slim
  #4 sha256:88f83c7105d19eeb7cd4d26d9ce011e2df8ab9dd0dad2e9ce84771f012f3b2e0
  #4 ERROR: unexpected status code [manifests 3-slim]: 400 Bad Request
  
  #3 [internal] load metadata for gcr.io/distroless/python3-debian10:latest
  #3 sha256:847d648b5ae1c07de239ad1cf534dcbbc90f051f65da51ccb91ed47a5bce342c
  #3 CANCELED
  ------
   > [internal] load metadata for public.ecr.aws/docker/library/python:3-slim:
  ------
  failed to solve with frontend dockerfile.v0: failed to create LLB definition: unexpected status code [manifests 3-slim]: 400 Bad Request
  Warning: Docker build failed with exit code 1, back off 2.354 seconds before retry.

Is there any way (like back off and retry) to work around this temporary issue (seems to be a network issue)

Git commit message as oneline

Thanks for the great GitHub Action! I was able to get it up and running immediately on the first try!

From the docs I know that we can get the git commit message as ${{ github.event.head_commit.message }}, but I was wondering if there's a way that we can get just the first line of the commit message?

Also, as an FYI, the head_commit.message property only exists on some workflow payloads. You might have to access it differently depending on how the workflow is triggered. Might be worth adding at link to the Workflow Payload doc in the README?

mac runner not able to parse workflow_url

workflow url is not working for some reason.

"Github Action <{workflow_url}|{workflow}> workflow has {status_message}"

this is the message i get from slack:
Github Action <|Smoke Test> workflow has passed

Migrate Action to Javascript from Python

The current implementation uses Python and Docker is required for building the action. Docker builds take about 15-20s for each build which is too much as compared to the actions written in Javascript (1-2s).

Less time on builds means more money savings.

I'll try to migrate this action to Javascript and get rid of the Docker builds and release the new action as v2.

If anyone has a better alternative/suggestion, please comment below. I'd be more than happy to take your advice.

Thanks.

Expose {actor} variable

It would be useful to have access to GITHUB_ACTOR to display the name of the person or app that initiated the workflow.

This is similar to a Slack User mention, however it is not always necessary to notify users under all circumstances.

Feature request: Notify user who made commit

Right now you can notify slack users for when the pipeline fails, would be nice if there was an option to map the GitHub users with the Slack username so only the person committing get's tagged in the message.

Need notification for cancelled runs

I noticed that if I cancel a running action, I don't get any Slack notification about it.
I'm using if: always() on all of my notifications.
Am I missing something? or is this not supported?

Reporting a vulnerability

Hello!

I hope you are doing well!

We are a security research team. Our tool automatically detected a vulnerability in this repository. We want to disclose it responsibly. GitHub has a feature called Private vulnerability reporting, which enables security research to privately disclose a vulnerability. Unfortunately, it is not enabled for this repository.

Can you enable it, so that we can report it?

Thanks in advance!

PS: you can read about how to enable private vulnerability reporting here: https://docs.github.com/en/code-security/security-advisories/repository-security-advisories/configuring-private-vulnerability-reporting-for-a-repository

string pattern only get replaced once

Hi,

I am constructing a workflow with slack notification requirement and come across this great action library today ๐Ÿ‘
However I find a very small issue, the string pattern only get replaced once

This is possibly due to makeMessage() is using string.replace instead of string.replaceAll
https://github.com/ravsamhq/notify-slack-action/blob/master/src/main.ts#L30

For this reason, pattern cannot be used more than once
eg: notification_title: '{emoji} Server Deploy Failed {emoji}'
only the first {emoji} will get replaced

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.