slackapi / slack-github-action

Send data into Slack using this GitHub Action!
MIT License
953 stars 147 forks source link

channel-id with environmental variable #115

Closed jku-sr closed 2 years ago

jku-sr commented 2 years ago

Description

can Channel_ID be set using an environmental variable?

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

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


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/slack-github-action@v1.21.0
        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/slack-github-action@v1.21.0
  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.

mwbrooks commented 2 years ago

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/slack-github-action@v1.21.0
        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 }}.

jku-sr commented 2 years ago

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/slack-github-action@v1.21.0
        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.

kian2attari commented 2 years ago

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 👍