Search code examples
slackgoogle-cloud-buildslack-block-kit

Google Cloud Build Slack Notifier message "no preview available"


The problem:

I have configure a "Slack Notifier" service in Google Cloud Run, following this guide: https://cloud.google.com/build/docs/configuring-notifications/configure-slack

Every thing is working pretty well, builds are triggered automatically and I do get my Slack notifications as configured. But the problem is that the notification popup in Slack (Windows Desktop client app) says "no preview available" and I can't figure out how to make that part work.

enter image description here

I've been "Googling" this for hours to no avail, so now I'm reaching out here in the hope that someone knows how to resolve this.

So far all I've found is that "no preview available" will show if the message has no text content, but mine has text, here's a preview of the full message content:

enter image description here

Further details:

Here is my Slack Notifier configuration yaml:

apiVersion: cloud-build-notifiers/v1
kind: SlackNotifier
metadata:
  name: example-slack-notifier
spec:
  notification:
    filter: build.status in [Build.Status.SUCCESS, Build.Status.FAILURE, Build.Status.TIMEOUT]
    params:
      buildStatus: $(build.status)
    delivery:
      webhookUrl:
        secretRef: webhook-url
    template:
      type: golang
      uri: gs://path-to-json-template.json
  secrets:
  - name: webhook-url
    value: projects/.../versions/latest

And here is the JSON template:

[
  {
    "type": "section",
    "text": {
      "type": "plain_text",
      "text": "Cloud Build Completed"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "*BUILD DETAILS:*"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Status: {{.Params.buildStatus}}\nEnvironment: {{.Build.Substitutions._ENVIRONMENT}}"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "*GITHUB DETAILS:*"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Repo: {{.Build.Substitutions.REPO_NAME}}\nBranch: {{.Build.Substitutions.BRANCH_NAME}}\nPR: #{{.Build.Substitutions._PR_NUMBER}}"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "You can access your service here: {{.Build.Substitutions._DEV_SERVICE_URL}}"
    }
  },
  {
    "type": "divider"
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "View Build Logs"
    },
    "accessory": {
      "type": "button",
      "text": {
        "type": "plain_text",
        "text": "Logs"
      },
      "value": "click_me_123",
      "url": "{{.Build.LogUrl}}",
      "action_id": "button-action"
    }
  }
]

If anyone can help me figure out how to replace "no preview available" with something meaningful like "Cloud Build Completed!" that would be greatly appreciated.


Solution

  • Looks like this is a Slack integration+Windows problem, looks like Windows Notifications don't support rich text elements, and/or Slack Windows client does not handle this properly and/or in correct way.

    I found similar error in some other project https://github.com/SmartAPI/smartAPI/issues/97

    Slack supposes that you while sending messages or posting webhooks with their API would supply it not with only blocks (array) or attachments (array) payload as you correctly provided in the second code snippet (also a second screenshot), but also a plain text string payload Slack will use as a fallback both for a webhook or chat.postMessage web API call.

    If you use and deploy a notifier code from their GitHub repository mentioned in your question, they call a PostWebHook with attachments element (array) but no text element provided.

    return &slack.WebhookMessage{Attachments: []slack.Attachment{{Color: clr, Blocks: blocks}}}, nil
    

    I guess this is a root cause of the problem, this is on the developer side, or you could modify the code yourself and add a Text string.