kargo Slack Posting messages not received
Closed this issue · 15 comments
Discussed in #3165
Originally posted by creeram December 20, 2024
I'm trying to post messages to Slack. The promotion is successful, but I don't receive any notifications or messages in the channel.
The Slack credentials are valid and tested with the curl command, and I can see the messages in the Slack channel.
Kargo Verision:
Client Version: v1.1.1
Server Version: v1.1.1
Screenshot of Stage promotion successful run.
There are not error logs in Kargo API and controller pods.
apiVersion: kargo.akuity.io/v1alpha1
kind: Stage
metadata:
name: demo
namespace: kargo-demo
spec:
requestedFreight:
- origin:
kind: Warehouse
name: kargo-demo
sources:
direct: true
promotionTemplate:
spec:
vars:
- name: slackChannel
value: xxxxxx
steps:
- uses: git-clone
config:
repoURL: xxxxxxx
checkout:
- branch: main
path: ./src
- uses: helm-update-image
as: update-chart-image
config:
path: ./src/apps/demo/e2e.yaml
images:
- image: xxxxxxxxxxxxx
key: image.tag
value: Tag
- uses: git-commit
as: commit
config:
path: ./src/apps
messageFromSteps:
- update-chart-image
- uses: git-push
config:
path: ./src
targetBranch: main
- uses: http
config:
method: POST
url: https://slack.com/api/chat.postMessage
headers:
- name: Authorization
value: Bearer xoxb-xxxxxxx-xxxxxxx-xxxxxxx
- name: Content-type
value: application/json
body: |
${{ quote({
"channel": vars.slackChannel,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hi I am a bot that can post *_fancy_* messages to any public channel."
}
}
]
}) }}
```</div>
@creeram is there some possibility of posting to netcat or socat instead so you can see what's coming through?
@krancour What do you mean by posting to Netcat or Socat? could you give me some info on how to do that?
Depending on your OS and package manager, you should have at least one of the command line utilities netcat
, nc
, or socat
available to you.
The syntax is slightly different for each and you'll have to look it up, but all three should allow you to easily listen for TCP connections on a port of your choosing and echo whatever they receive.
This means if you send it an HTTP request it will show you method, path, HTTP version, all headers, and the message body.
Using nc
, for example:
nc -l 8080
Then alter your http
step so the url
begins with http://localhost:8080
(instead of https://slack.com
).
Run your promotion and see what, if anything nc
receives.
It seems the message is coming through with extra quotes:
POST / HTTP/1.1
Host: 192.168.2.120:8080
User-Agent: Go-http-client/1.1
Content-Length: 148
Content-Type: application/json
Accept-Encoding: gzip
Connection: close
"{"blocks":[{"text":{"text":"Updated test to use image public.ecr.aws/nginx/nginx:1.27.3","type":"mrkdwn"},"type":"section"}],"channel":"C123456"}"
There's some odd interplay between the |
and the quote()
function.
|
introduces a trailing newline that interferes with some other logic for interpreting strings that are explicitly enclosed in quotes.
I'm hoping for a patch release tomorrow and that's the milestone I've included this in.
@krancour With the new release the slack post message is working.
but for that i had to hardcode the slack token here
- uses: http
config:
method: POST
url: https://slack.com/api/chat.postMessage
headers:
- name: Authorization
value: Bearer xoxb-xxxxxxx-xxxxxxx-xxxxxxx
- name: Content-type
value: application/json
body: |
${{ quote({
"channel": vars.slackChannel,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hi I am a bot that can post *_fancy_* messages to any public channel."
}
}
]
}) }}
I tried creating a k8s secret for that as well but it didn't work. How can i make it work with K8 Secret?
- uses: http
config:
method: POST
url: https://slack.com/api/chat.postMessage
headers:
- name: Authorization
value: Bearer ${{ secrets.slack.token }}
- name: Content-type
value: application/json
body: |
${{ quote({
"channel": vars.slackChannel,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hi I am a bot that can post *_fancy_* messages to any public channel."
}
}
]
}) }}
the k8s secret used:
apiVersion: v1
kind: Secret
metadata:
name: slack
namespace: kargo-demo
type: Opaque
data:
slack.token: xxxxxxxxxxxxxxxxx(base64 encoded)
and how can we use variables inside that text block?
like ${{ imageFrom("public.ecr.aws/nginx/nginx", warehouse("my-warehouse")).Tag }}
This doesn't match the expression used for accessing the secret (${{ secrets.slack.token }}
):
apiVersion: v1
kind: Secret
metadata:
name: slack
namespace: kargo-demo
type: Opaque
data:
slack.token: xxxxxxxxxxxxxxxxx(base64 encoded)
This would:
apiVersion: v1
kind: Secret
metadata:
name: slack
namespace: kargo-demo
type: Opaque
data:
token: xxxxxxxxxxxxxxxxx(base64 encoded)
and how can we use variables inside that text block?
I'm afraid I don't understand the question, as I already see you successfully using vars.slackChannel
.
@krancour I tried both secrets but it didn't work.
apiVersion: v1
kind: Secret
metadata:
name: slack
namespace: kargo-demo
type: Opaque
data:
token: xxxxxxxxxxxxxxxxx(base64 encoded)
For variable how can I use it here
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "vars.variable."//it didn't work
}
}
]
@creeram here's a full working example:
apiVersion: kargo.akuity.io/v1alpha1
kind: Project
metadata:
name: kargo-demo-15
spec:
promotionPolicies:
- stage: test
autoPromotionEnabled: true
---
apiVersion: kargo.akuity.io/v1alpha1
kind: Warehouse
metadata:
name: kargo-demo
namespace: kargo-demo-15
spec:
subscriptions:
- image:
repoURL: public.ecr.aws/nginx/nginx
semverConstraint: ^1.24.0
---
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: slack
namespace: kargo-demo-15
stringData:
token: foo
---
apiVersion: kargo.akuity.io/v1alpha1
kind: Stage
metadata:
name: test
namespace: kargo-demo-15
spec:
requestedFreight:
- origin:
kind: Warehouse
name: kargo-demo
sources:
direct: true
promotionTemplate:
spec:
vars:
- name: repoURL
value: public.ecr.aws/nginx/nginx
- name: url
value: <url here>
- name: slackChannel
value: C123456
steps:
- uses: http
config:
method: POST
url: ${{ vars.url }}
headers:
- name: Content-Type
value: application/json
- name: Authorization
value: Bearer ${{ secrets.slack.token }}
body: |
${{ quote({
"channel": vars.slackChannel,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Updated " + ctx.stage + " to use image " + vars.repoURL + ":" + imageFrom(vars.repoURL).Tag
}
}
]
}) }}
Could you please help me with one more detail?
how can I get the info from a variable for who did the promotion?
That's not something that's currently exposed. It should be easy enough to add if you would please open an issue requesting it.