rtCamp/action-slack-notify

SLACK_MESSAGE is shown as null

Naoki206 opened this issue · 5 comments

I'm using the latest version(v2.2.0) of Slack Notify, and in the Environment Variables, I didn't set any values on the SLACK_MESSAGE as it's recommended on the docs, but it is shown as null every time.
hope it will be resolved

I think I can guess what you're running into @Naoki206 since I just encountered it too.

When rtCamp/action-slack-notify fires off a message to slack, it populates the message via an environment variable -- SLACK_MESSAGE.

So where does that variable get set? You can see here in the accompanying shell script that its queried from the contents of the file specified by the path $GITHUB_EVENT_PATH. This in turn is a default variable provided by GitHub:
image

What's contained in the "webhook event payload"? Well that depends on what event triggered this GitHub action! In the shell script I linked to above, you can see from the snippet jq -r '.commits[-1].message' that they assume the event has a property commits on it. Turns on out there's only one trigger event that will have this property on it: push. So if you're triggering the GitHub Action with anything besides a push, then you won't see a commit message come through by default. Instead you'll have to do something like override SLACK_MESSAGE with a value gleaned from your triggering event's set of properties (e.g. the PR title if the triggering event was a PR).

The event payloads usually do not have the commit message, only the sha. The API probably changed leaving this package behind. I was able to work around this limitation like this:

  1. Grab the latest commit message and put it in a variable
- name: 'Get message'
  id: 'deploy-message'
  run: |
        echo "::set-output name=commit_msg::$(git log -1 --format=%B)"
        echo "::set-output name=commit_sha::$(git log -1 --format=%H)"

Read more about outputs

  1. Construct SLACK_MESSAGE
- name: 'Slack notify'
  uses: rtCamp/action-slack-notify@v2.2.0
  env:
    SLACK_WEBHOOK: my_webhook
    SLACK_CHANNEL: my_channel
    SLACK_USERNAME: username
    SLACK_TITLE: 'Title'
    SLACK_MESSAGE: ${{ steps.deploy-message.outputs.commit_msg }}

I hope this helps someone.

@eljhkrr this was helpful, but I'm not getting the message. just the branch hashes...

I am getting null when the action is executed in a PR. Should this PR fix that issue?