CircleCI-Public / slack-orb

Create custom Slack notifications for CircleCI job statuses
https://circleci.com/developer/orbs/orb/circleci/slack
MIT License
213 stars 205 forks source link

[Question / Request] Tokens / multiple channels #77

Closed timothyclarke closed 3 years ago

timothyclarke commented 5 years ago

I'm wanting to send slack messages to different teams in different channels.

It looks like a web hook is linked to a specific channel. Is there any way to use a token and then specify the channel?

https://get.slack.help/hc/en-gb/articles/215770388-Create-and-regenerate-API-tokens

KyleTryon commented 4 years ago

Hello @timothyclarke At the moment we don't support multiple webhooks as a parameter but I would be willing to take that as a feature request. At the moment I believe the best way to do this would to be to call the command twice, each with the different webhook value.

kamontat commented 4 years ago

I will try this. Maybe we can change to use apps token instead? for multiple channels in slack?

timothyclarke commented 4 years ago

Just to give this some more context. I'm not talking about 2 channels. I'm talking closer to 10.

As builds go into DEV environments the appropriate team needs to know. We're using micro services/ containers so as a build / container progress through the various environments to Production different teams should be aware. Eg QA

gmemstr commented 3 years ago

I wonder if contexts would help in this scenario.

timothyclarke commented 3 years ago

I think this one was solved with Orb version 3.4.0 (which introduced the channel param). I'll test and if so will close this

KyleTryon commented 3 years ago

So, I was looking into this specifically for the new version of the slack orb (version 4.0.0) due to be released next week, however I did not include the channel parameter because of this: image https://api.slack.com/messaging/webhooks

What I would recommend is creating separate webhooks for each channel and save the env vars separately in CircleCI (which could be done with contexts if desired) and when calling the orb, you can pass the channel to the webhook parameter.

slack/notify:
  webhook: $SLACK_CHANNEL_1
timothyclarke commented 3 years ago

@KyleTryon Thanks. One of the issues I'm trying to address is that multiple (currently 2) teams / channels want to be notified of Production releases.

Running a running a step twice with different inputs isn't an issue, running multiple jobs just for notifications become a little tedious. Parsing multiple webhooks also feels wrong

Is there any chance of using https://api.slack.com/messaging/sending#publishing ?

timothyclarke commented 3 years ago

I've been able to use curl to send a message to a custom (public) channel I've setup the App per step 1 of https://github.com/CircleCI-Public/slack-orb/tree/v3.4.2#how-to-get-your-slack-webhook I think I went to the oauth section https://api.slack.com/apps/<app_id>/oauth? and added the oauth scope chat:write.public

From there it was a simple curl -X POST -H 'Authorization: Bearer xoxb-###-###-***' -H 'Content-type: application/json' --data '{"channel": "#my_public_channel","text":"Hello, World!"}' https://slack.com/api/chat.postMessage

gmemstr commented 3 years ago

Hey @timothyclarke, we've dug a bit deeper and we're working on implementing this in v4 -- thanks for the pointers!

timothyclarke commented 3 years ago

@gmemstr Thank you for the update / feedback. Would you like me to close https://github.com/CircleCI-Public/slack-orb/pull/170?

gmemstr commented 3 years ago

@timothyclarke Since it's a breaking change anyways, we're going to opt to roll this into #160 instead (#172 wip). Go ahead and close the PR, unless we can find a way around making it a breaking change so we can potentially roll a minor release for 3.x users.

timothyclarke commented 3 years ago

Thank you @gmemstr Please you let me know when this is/will be in circleci/slack@dev:beta so I can test the functionality.

I tried a few minutes before posting this and the beta version looks to be still based on the webhook rather than the Bearer token.

gmemstr commented 3 years ago

@timothyclarke Thank you for the reminder! I've updated the dev:beta version with the latest version of the orb.

timothyclarke commented 3 years ago

@gmemstr Thank you. I've just tested and I'm getting branch does not match issue

Current branch does not match any item from the "branch_list" parameter
Current branch: JIRA-123

Note I've tweaked the branch name above, but kept the regex pattern The appropriate part of my .circleci/config.yml is

orbs:
  betaslack: circleci/slack@dev:beta

jobs:
  Notify Slack:
    docker:
      - image: alpine:3.12.0
    environment:
      SLACK_DEFAULT_CHANNEL: '#circle-test'
    steps:
      - betaslack/notify:
          event: pass
          template: success_tagged_deploy_1

Edit: I've also tried branches JIRA123 and testing/JIRA123 both failed

I've just noticed the following

Installing JQ for Alpine.
  0%                                              49% #####################                        73% ################################            % ############################################sh: =~: unknown operand
NO SLACK ALERT

Specifically sh: =~: unknown operand hidden of to the right of the screen

update It looks like you're using /bin/sh which must be interpreted as a POSIX only shell How about replacing if [[ "$CIRCLE_BRANCH" =~ ^${i}$ ]]; then in the shell script with if echo "$CIRCLE_BRANCH" | grep -Eq ^${i}$ ; then as [[, ]] and =~ are not POSIX compliant

KyleTryon commented 3 years ago

@timothyclarke thank you for the bug fix and a solution! You are wonderful people. We're taking a look now.

timothyclarke commented 3 years ago

:D glad that this is progressing. pls let me know when the updated beta is published so I can re-test. I just switched from

    environment:
      SLACK_DEFAULT_CHANNEL: '#circle-test'

to

    steps:
      - betaslack/notify:
          channel:  '#circle-test'

per the current code and received Unexpected argument(s): channel :(

KyleTryon commented 3 years ago

:D glad that this is progressing. pls let me know when the updated beta is published so I can re-test. I just switched from

    environment:
      SLACK_DEFAULT_CHANNEL: '#circle-test'

to

    steps:
      - betaslack/notify:
          channel:  '#circle-test'

per the current code and received Unexpected argument(s): channel :(

Hey @timothyclarke ,

Wanted your opinion as I am now trying to add support for alpine and make some POSIX changes. I realize on testing and research, it is common for alpine images to not include a root user or sudo privileges, which makes it impossible to install JQ on alpine most of the time.

We can probably still work on Alpine but with the requirement that CURL and JQ be installed in the image and we wouldnt attempt to install them for you on alpine.

Any thoughts?


Also for the channel, and we will add docs, you would not include the hashtag. The best method is using just the "channel id" rather than the text name.

timothyclarke commented 3 years ago

@KyleTryon Ensure that both CURL and JQ are documented. I'd put the attempt to install behind a flag (and default it to false).

I think this will fall into two camps, builds (or similar compilation jobs) and processes (Terraform, Ansible, Helm etc). I'd imagine that you should have a better idea on that point than me.

We're very careful about how the containers are run in our DEV / TEST / PRODUCTION environments and in the process jobs. When we're building code in a docker container that will be disposed of at the end of the run, we're not as concerned. The only things I'm keeping from build to build are

timothyclarke commented 3 years ago

@KyleTryon @gmemstr Is there any chance the beta orb can be updated to the current code ? I'm still getting the error described in https://github.com/CircleCI-Public/slack-orb/issues/77#issuecomment-701353116 and looking at the code in the job I'm still seeing if [[ "$CIRCLE_BRANCH" =~ ^${i}$ ]]; then

gmemstr commented 3 years ago

@timothyclarke Updated :) Should really look into automating this, but we're approaching a finalised version regardless.

timothyclarke commented 3 years ago

I've been able to

I've been able to use multiple contexts as well (which is one of the features I've been waiting a while for). Thanks for all the effort that has been put into this. Now I've just got to integrate this into the workflows of 5 teams

KyleTryon commented 3 years ago

@timothyclarke Thank you so much for testing this out and providing your feedback, it is incredibly valuable to us. Your help has been instrumental!

timothyclarke commented 3 years ago

I've re-tested after the 4.0 announcement. I'm happy that the functionality requested is now in the orb. The response from the CircleCI team has been very encouraging