slack-github-action icon indicating copy to clipboard operation
slack-github-action copied to clipboard

channel-id with environmental variable

Open jku-sr opened this issue 4 years ago • 3 comments

Description

can Channel_ID be set using an environmental variable?

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x ] question
  • [ ] documentation related
  • [ ] example code related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [ x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x ] I've read and agree to the Code of Conduct.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

my workflow YAML

- name: Post to Slack
        id: slackinitial     
        uses: slackapi/[email protected]
        with:
          channel-id: $env:channel_name
          slack-message: ""I am a Test message"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
          channel_name: ${{ github.event.client_payload.channel_name }}

Expected result:

channel-id will pickup the environmental variable. Looking at the output the environmental variable is being picked up but channel-id I guess only takes Strings literally.

Actual result:

`Run slackapi/[email protected]
  with:
    channel-id: $env:channel_name
    slack-message: "I am a Test message"
  env:
    SLACK_BOT_TOKEN: ***
    channel_name: test-channel
Error: Error: An API error occurred: channel_not_found`

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

jku-sr avatar Aug 03 '22 19:08 jku-sr

Hey @jku-sr 👋🏻 Thanks for the question!

You can use environment variables to set the channel-id parameter, but we don't have a first-class CHANNEL_ID environment variable. Here's a Stack Overflow article that explains how to access environment variables as parameter values.

Your implementation may look something like this using the environment variable CHANNEL_ID:

- name: Post to Slack
        id: slackinitial     
        uses: slackapi/[email protected]
        with:
          channel-id: ${{ env.CHANNEL_NAME }}
          slack-message: ""I am a Test message"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
          channel_name: ${{ github.event.client_payload.channel_name }}

In your example, you used the syntax $env:channel_name. This only works on a Windows runner. On Linux or macOS you will want to use ${{ env.CHANNEL_NAME }}.

mwbrooks avatar Aug 03 '22 22:08 mwbrooks

my runner is Windows based.

However like the error above when setting it as:


  with:
    channel-id: $env:channel_name

I get the error: Error: An API error occurred: channel_not_found`

When I set it like your example with ${{ env.CHANNEL_NAME }} it worked even though my runner is not linux based. So i guess Windows Runners also want the ENV in that format.

Here's my final example

- name: Post to Slack
        id: slackinitial     
        uses: slackapi/[email protected]
        with:
          channel-id: ${{ env.CHANNEL_NAME }} 
          slack-message: ""I am a Test message"
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
          channel_name: ${{ github.event.client_payload.channel_name }}

for others that come across this my client_payload comes from a JSON based HTTP Post from a web endpoint

Thank you for the assistance! Maybe we can update the readme with this example for others to come across.

jku-sr avatar Aug 04 '22 14:08 jku-sr

Hey @jku-sr! So glad to hear we could help. Since the specific issue seems to be resolved I'll close it off.

Feel free to reopen it if there's still any problems with this specific issue. If you need help with something else you can create a new issue 👍

kian2attari avatar Aug 04 '22 22:08 kian2attari