Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Post to multiple slack channels
Browse files Browse the repository at this point in the history
Per a request from Donald Johnson, added support to ankh for
posting to multiple slack channels. Also added channels as an
array to the slack section in ankh config
  • Loading branch information
Steven E. Newton committed Feb 19, 2019
1 parent 3e7bf11 commit b859da8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ When invoked, Ankh will operate over both the `haste-server` and `myservice` cha
| format | string | Optional. Format of slack message that will be used. See available variables below. |
| rollbackFormat | string | Optional. Format of message for rollbacks that will be used. See available variables below. |
| pretext | string | Optional. Pretext for slack message. Default is `A new release notification has been received`. |
| channels | []string | Optional. Channel[s} to post the message to. |

##### `Slack Message Variables`
| Variable | Description
Expand Down
11 changes: 7 additions & 4 deletions ankh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func reconcileMissingConfigs(ctx *ankh.ExecutionContext, ankhFile *ankh.AnkhFile
}

// we should finally have a tag value
if len(ctx.SlackChannels) > 0 {
if len(ctx.AnkhConfig.Slack.Channels) > 0 {
ctx.SlackDeploymentVersion = *chart.Tag
}
}
Expand Down Expand Up @@ -414,7 +414,8 @@ func execute(ctx *ankh.ExecutionContext) {
executeContext(ctx, &rootAnkhFile)
}

if len(ctx.SlackChannels) > 0 {
if len(ctx.AnkhConfig.Slack.Channels) > 0 {
ctx.Logger.Debugf("Pinging %d slack channels %+v", len(ctx.AnkhConfig.Slack.Channels), ctx.AnkhConfig.Slack.Channels)
if ctx.Mode == ankh.Rollback {
ctx.SlackDeploymentVersion = "rollback"
}
Expand Down Expand Up @@ -889,7 +890,9 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Apply
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
if *slackChannel != "" {
ctx.AnkhConfig.Slack.Channels = []string{*slackChannel}
}
ctx.SlackMessageOverride = *slackMessageOverride
filters := []string{}
for _, filter := range *filter {
Expand Down Expand Up @@ -921,7 +924,7 @@ func main() {
ctx.LocalChart = true
}
ctx.Mode = ankh.Rollback
ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel)
ctx.AnkhConfig.Slack.Channels = append(ctx.AnkhConfig.Slack.Channels, *slackChannel)
ctx.SlackMessageOverride = *slackMessageOverride
ctx.Filters = []string{"deployment", "statfulset"}

Expand Down
15 changes: 8 additions & 7 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ExecutionContext struct {
DataDir string
HelmSetValues map[string]string

SlackChannels []string
SlackChannelsOverride []string
SlackMessageOverride string
SlackDeploymentVersion string

Expand Down Expand Up @@ -105,12 +105,13 @@ type DockerConfig struct {
}

type SlackConfig struct {
Token string `yaml:"token"`
Icon string `yaml:"icon-url"`
Username string `yaml:"username"`
Format string `yaml:"format"`
RollbackFormat string `yaml:"rollbackFormat"`
Pretext string `yaml:"pretext"`
Token string `yaml:"token"`
Icon string `yaml:"icon-url"`
Username string `yaml:"username"`
Format string `yaml:"format"`
RollbackFormat string `yaml:"rollbackFormat"`
Pretext string `yaml:"pretext"`
Channels []string `yaml:"channels"`
}

// AnkhConfig defines the shape of the ~/.ankh/config file used for global
Expand Down
2 changes: 1 addition & 1 deletion helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func Publish(ctx *ankh.ExecutionContext) error {
resp.Status, resp.StatusCode, upstreamTarballPath)
}

ctx.Logger.Debug("Helm registry PUT resp: %+v", resp)
ctx.Logger.Debugf("Helm registry PUT resp: %+v", resp)
ctx.Logger.Infof("Finished publishing '%v'", upstreamTarballPath)
return nil
}
Expand Down
26 changes: 13 additions & 13 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@ func PingSlackChannel(ctx *ankh.ExecutionContext) error {
Username: username,
}

channels := make(map[string]string, len(ctx.SlackChannels))
for _, ch := range ctx.SlackChannels {
channels := make(map[string]string, len(ctx.AnkhConfig.Slack.Channels))
for _, ch := range ctx.AnkhConfig.Slack.Channels {
channelId, err := getSlackChannelIDByName(api, ch)
if err != nil {
return err
}
channels[ch] = channelId
}
if err != nil {
return err
}
channels[ch] = channelId
}

for name, id := range channels {
if !ctx.DryRun {
_, _, err = api.PostMessage(id, slack.MsgOptionAttachments(attachment), slack.MsgOptionPostMessageParameters(messageParams))
} else {
ctx.Logger.Infof("--dry-run set so not sending message '%v' to slack channel %v", messageText, name)
}
}
if !ctx.DryRun {
_, _, err = api.PostMessage(id, slack.MsgOptionAttachments(attachment), slack.MsgOptionPostMessageParameters(messageParams))
} else {
ctx.Logger.Infof("--dry-run set so not sending message '%v' to slack channel %v", messageText, name)
}
}

return err
}
Expand Down

0 comments on commit b859da8

Please sign in to comment.