Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion slack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,15 @@ func (s *slackNotifier) writeMessage() (*slack.WebhookMessage, error) {
return nil, fmt.Errorf("failed to unmarshal templating JSON: %w", err)
}

return &slack.WebhookMessage{Attachments: []slack.Attachment{{Color: clr, Blocks: blocks}}}, nil
// Add Text field for Slack push notification preview
triggerName := build.Substitutions["TRIGGER_NAME"]
if triggerName == "" {
triggerName = build.ProjectId
}
previewText := fmt.Sprintf("Cloud Build %s: %s", build.Status, triggerName)

return &slack.WebhookMessage{
Text: previewText,
Attachments: []slack.Attachment{{Color: clr, Blocks: blocks}},
}, nil
}
1 change: 1 addition & 0 deletions slack/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestWriteMessage(t *testing.T) {
}

want := &slack.WebhookMessage{
Text: "Cloud Build SUCCESS: hello-world-123",
Attachments: []slack.Attachment{{
Color: "#22bb33",
Blocks: slack.Blocks{
Expand Down
31 changes: 31 additions & 0 deletions slack/my-slack-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: cloud-build-notifiers/v1
kind: SlackNotifier
metadata:
name: slack-notifier
spec:
notification:
filter: build.status in [Build.Status.WORKING, Build.Status.SUCCESS, Build.Status.FAILURE, Build.Status.TIMEOUT, Build.Status.INTERNAL_ERROR]
delivery:
webhookUrl:
secretRef: webhook-url
template:
type: golang
uri: gs://awesome-nimbus-461900-c0-notifiers-config/slack.json

secrets:
- name: webhook-url
value: projects/awesome-nimbus-461900-c0/secrets/slack_webhook/versions/latest
58 changes: 39 additions & 19 deletions slack/slack.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
[
{
"type": "section",
"text": {
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":cloud: *Cloud Build - {{.Build.Status}}*"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Trigger:*\n{{.Build.Substitutions.TRIGGER_NAME}}"
},
{
"type": "mrkdwn",
"text": "Cloud Build {{.Build.ProjectId}} {{.Build.Id}} {{.Params.buildStatus}}"
"text": "*Branch:*\n{{.Build.Substitutions.BRANCH_NAME}}"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "View Build Logs"
"text": "*Project:*\n{{.Build.ProjectId}}"
},
"accessory": {
{
"type": "mrkdwn",
"text": "*Build ID:*\n{{.Build.Id}}"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Logs"
"text": "View Logs",
"emoji": true
},
"value": "click_me_123",
"url": "{{.Build.LogUrl}}",
"action_id": "button-action"
"style": "primary"
}
}
]
]
}
]
2 changes: 1 addition & 1 deletion slack/slack.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ metadata:
name: example-slack-notifier
spec:
notification:
filter: build.status == Build.Status.SUCCESS
filter: build.status in [Build.Status.SUCCESS, Build.Status.FAILURE, Build.Status.TIMEOUT, Build.Status.INTERNAL_ERROR]
params:
buildStatus: $(build.status)
delivery:
Expand Down