Skip to content

Commit

Permalink
[OPS-5938] Convert a Slack Notify action to a Flowdock Notify action.
Browse files Browse the repository at this point in the history
  • Loading branch information
cafuego committed Oct 16, 2019
1 parent 542342c commit 6cc6d01
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 166 deletions.
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ FROM golang:1.11-alpine3.9@sha256:7a0bf914dd581a35afb054bc02c6b7a3fa31ed6398adf9

LABEL "com.github.actions.icon"="bell"
LABEL "com.github.actions.color"="yellow"
LABEL "com.github.actions.name"="Slack Notify"
LABEL "com.github.actions.description"="This action will send notification to Slack"
LABEL "com.github.actions.name"="Flowdockk Notify"
LABEL "com.github.actions.description"="This action will send notification to Flowdockk"


WORKDIR ${GOPATH}/src/github.com/rtcamp/action-slack-notify
COPY main.go ${GOPATH}/src/github.com/rtcamp/action-slack-notify
WORKDIR ${GOPATH}/src/github.com/UN-OCHA/gha-flowdock-notify
COPY main.go ${GOPATH}/src/github.com/UN-OCHA/gha-flowdock-notify

ENV CGO_ENABLED 0
ENV GOOS linux

RUN go get -v ./...
RUN go build -a -installsuffix cgo -ldflags '-w -extldflags "-static"' -o /go/bin/slack-notify .
RUN go build -a -installsuffix cgo -ldflags '-w -extldflags "-static"' -o /go/bin/flowdock-notify .

# alpine:latest at 2019-01-04T21:27:39IST
FROM alpine@sha256:46e71df1e5191ab8b8034c5189e325258ec44ea739bba1e5645cff83c9048ff1

COPY --from=builder /go/bin/slack-notify /usr/bin/slack-notify
COPY --from=builder /go/bin/flowdock-notify /usr/bin/flowdock-notify

ENV VAULT_VERSION 1.0.2

Expand Down
84 changes: 34 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,60 @@
> **⚠️ Note:** To use this GitHub Action, you must have access to GitHub Actions. GitHub Actions are currently only available in public beta (you must apply for access).
This action is a part of [GitHub Actions Library](https://github.com/rtCamp/github-actions-library/) created by [rtCamp](https://github.com/rtCamp/).
This action is a modified version of the Slack Notify action, which is part of [GitHub Actions Library](https://github.com/rtCamp/github-actions-library/) created by [rtCamp](https://github.com/rtCamp/).

# Slack Notify - GitHub Action
# Flowdock Notify - GitHub Action

A [GitHub Action](https://github.com/features/actions) to send a message to a Slack channel.

**Screenshot**

<img width="485" alt="action-slack-notify-rtcamp" src="https://user-images.githubusercontent.com/4115/54996943-9d38c700-4ff0-11e9-9d35-7e2c16ef0d62.png">

The `Site` and `SSH Host` details are only available if this action is run after [Deploy WordPress GitHub action](https://github.com/rtCamp/action-deploy-wordpress).
A [GitHub Action](https://github.com/features/actions) to send a message to a Flowdock channel.

## Usage

You can use this action after any other action. Here is an example setup of this action:

1. Create a `.github/workflows/slack-notify.yml` file in your GitHub repo.
2. Add the following code to the `slack-notify.yml` file.
1. Create a `.github/workflows/flowdock-notify.yml` file in your GitHub repo.
2. Add the following code to the `flowdock-notify.yml` file.

```yml
on: push
name: Slack Notification Demo
name: Flowdock Notification Demo
jobs:
slackNotification:
name: Slack Notification
flowdockNotification:
name: Flowdock Notification
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Slack Notification
uses: rtCamp/action-slack-notify@master
- name: Flowdock Notification
uses: UN-OCHA/gha-flowdock-notify@master
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
FLOWDOCK_TOKEN: ${{ secrets.FLOWDOCK_TOKEN }}
```
3. Create `SLACK_WEBHOOK` secret using [GitHub Action's Secret](https://developer.github.com/actions/creating-workflows/storing-secrets). You can [generate a Slack incoming webhook token from here](https://slack.com/apps/A0F7XDUAZ-incoming-webhooks).
3. Create `FLOWDOCK_TOKEN` secret using [GitHub Action's Secret](https://developer.github.com/actions/creating-workflows/storing-secrets).


## Environment Variables

By default, action is designed to run with minimal configuration but you can alter Slack notification using following environment variables:

Variable | Default | Purpose
---------------|-------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------
SLACK_CHANNEL | Set during Slack webhook creation | Specify Slack channel in which message needs to be sent
SLACK_USERNAME | `rtBot` | The name of the sender of the message. Does not need to be a "real" username
SLACK_ICON | ![rtBot Avatar](https://github.com/rtBot.png?size=32) | User/Bot icon shown with Slack message
SLACK_COLOR | `good` (green) | You can pass an RGB value like `#efefef` which would change color on left side vertical line of Slack message.
SLACK_MESSAGE | Generated from git commit message. | The main Slack message in attachment. It is advised not to override this.
SLACK_TITLE | Message | Title to use before main Slack message
Variable | Default | Purpose
------------------|-------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------
FLOWDOCK_USERNAME | `fdBot` | The name of the sender of the message. Does not need to be a "real" username
FLOWDOCK_THREAD | Empty | The thread ID you wnt to post the message to
FLOWDOCK_MESSAGE | Generated from git commit message. | The main Flowdock message.
FLOWDOCK_TAGS | Empty | Hash tags to tag the message

You can see the action block with all variables as below:

```yml
- name: Slack Notification
uses: rtCamp/action-slack-notify@master
- name: Flowdock Notification
uses: UN-OCHA/gha-flowdock-notify@master
env:
SLACK_CHANNEL: general
SLACK_COLOR: '#3278BD'
SLACK_ICON: https://github.com/rtCamp.png?size=48
SLACK_MESSAGE: 'Post Content :rocket:'
SLACK_TITLE: Post Title
SLACK_USERNAME: rtCamp
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
FLOWDOCK_USERNAME: 'Jebb'
FLOWDOCK_THREAD: 'Owc8UJJ5773F-PIdEwK8LtT5QM7'
FLOWDOCK_MESSAGE: 'Post Content :rocket:'
FLOWDOCK_TAGS: 'lol,spam'
FLOWDOCK_TOKEN: ${{ secrets.FLOWDOCK_TOKEN }}
```

Below screenshot help you visualize message part controlled by different variables:

<img width="600" alt="Screenshot_2019-03-26_at_5_56_05_PM" src="https://user-images.githubusercontent.com/4115/54997488-d1f94e00-4ff1-11e9-897f-a35ab90f525f.png">

The `Site` and `SSH Host` details are only available if this action is run after [Deploy WordPress GitHub action](https://github.com/rtCamp/action-deploy-wordpress).

## Hashicorp Vault (Optional)

This GitHub action supports [Hashicorp Vault](https://www.vaultproject.io/).
Expand All @@ -82,31 +66,31 @@ Variable | Purpose
`VAULT_ADDR` | [Vault server address](https://www.vaultproject.io/docs/commands/#vault_addr) | `https://example.com:8200`
`VAULT_TOKEN` | [Vault token](https://www.vaultproject.io/docs/concepts/tokens.html) | `s.gIX5MKov9TUp7iiIqhrP1HgN`

You will need to change `secrets` line in `slack-notify.yml` file to look like below.
You will need to change `secrets` line in `flowdock-notify.yml` file to look like below.

```yml
on: push
name: Slack Notification Demo
name: Flowdock Notification Demo
jobs:
slackNotification:
name: Slack Notification
flowdockNotification:
name: Flowdock Notification
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Slack Notification
uses: rtCamp/action-slack-notify@master
- name: Flowdock Notification
uses: UN-OCHA/gha-flowdock-notify@master
env:
VAULT_ADDR: ${{ secrets.VAULT_ADDR }}
VAULT_TOKEN: ${{ secrets.VAULT_TOKEN }}
```

GitHub action uses `VAULT_TOKEN` to connect to `VAULT_ADDR` to retrieve slack webhook from Vault.
GitHub action uses `VAULT_TOKEN` to connect to `VAULT_ADDR` to retrieve Flowdock token from Vault.

In the Vault, the Slack webhook should be setup as field `webhook` on path `secret/slack`.
In the Vault, the Flowdock token should be setup as field `token` on path `secret/flowdock`.

## License

[MIT](LICENSE) © 2019 rtCamp
[MIT](LICENSE) © 2019 UN-OCHA

## Does this interest you?

Expand Down
4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bash

# custom path for files to override default files
custom_path="$GITHUB_WORKSPACE/.github/slack"
custom_path="$GITHUB_WORKSPACE/.github/flowdock"
main_script="/main.sh"

if [[ -d "$custom_path" ]]; then
rsync -av "$custom_path/" /
chmod +x /*.sh
fi

bash "$main_script"
bash "$main_script"
118 changes: 25 additions & 93 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,116 +6,48 @@ import (
"fmt"
"net/http"
"os"
"strings"
)

const (
EnvSlackWebhook = "SLACK_WEBHOOK"
EnvSlackIcon = "SLACK_ICON"
EnvSlackChannel = "SLACK_CHANNEL"
EnvSlackTitle = "SLACK_TITLE"
EnvSlackMessage = "SLACK_MESSAGE"
EnvSlackColor = "SLACK_COLOR"
EnvSlackUserName = "SLACK_USERNAME"
EnvGithubActor = "GITHUB_ACTOR"
EnvSiteName = "SITE_NAME"
EnvHostName = "HOST_NAME"
EnvDepolyPath = "DEPLOY_PATH"
EnvFlowdockToken = "FLOWDOCK_TOKEN"
EnvFlowdockMessage = "FLOWDOCK_MESSAGE"
EnvFlowdockThread = "FLOWDOCK_THREAD"
EnvFlowdockUserName = "FLOWDOCK_USERNAME"
EnvFlowdockTags = "FLOWDOCK_TAGS"
EnvGithubActor = "GITHUB_ACTOR"
)

type Webhook struct {
Text string `json:"text,omitempty"`
UserName string `json:"username,omitempty"`
IconURL string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
UnfurlLinks bool `json:"unfurl_links"`
Attachments []Attachment `json:"attachments,omitmepty"`
}

type Attachment struct {
Fallback string `json:"fallback"`
Pretext string `json:"pretext,omitempty"`
Color string `json:"color,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Footer string `json:"footer,omitempty"`
Fields []Field `json:"fields,omitempty"`

}

type Field struct {
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
Short bool `json:"short,omitempty"`
Event string `json:"event"`
UserName string `json:"external_user_name,omitempty"`
Content string `json:"content,omitempty"`
Thread string `json:"thread_id,omitempty"`
Tags []string `json:"tags,omitempty"`
}

func main() {
endpoint := os.Getenv(EnvSlackWebhook)
if endpoint == "" {
fmt.Fprintln(os.Stderr, "URL is required")
token := os.Getenv(EnvFlowdockToken)
if token == "" {
fmt.Fprintln(os.Stderr, "Token is required")
os.Exit(1)
}
text := os.Getenv(EnvSlackMessage)

endpoint := "https://api.flowdock.com/messages/chat/"
endpoint += token

text := os.Getenv(EnvFlowdockMessage)
if text == "" {
fmt.Fprintln(os.Stderr, "Message is required")
os.Exit(1)
}

fields:= []Field{
{
Title: "Ref",
Value: os.Getenv("GITHUB_REF"),
Short: true,
}, {
Title: "Event",
Value: os.Getenv("GITHUB_EVENT_NAME"),
Short: true,
},
{
Title: "Repo Action URL",
Value: "https://github.com/" + os.Getenv("GITHUB_REPOSITORY") + "/actions",
Short: false,
},
{
Title: os.Getenv(EnvSlackTitle),
Value: envOr(EnvSlackMessage, "EOM"),
Short: false,
},
}

hostName := os.Getenv(EnvHostName)
if hostName != "" {
newfields:= []Field{
{
Title: os.Getenv("SITE_TITLE"),
Value: os.Getenv(EnvSiteName),
Short: true,
},
{
Title: os.Getenv("HOST_TITLE"),
Value: os.Getenv(EnvHostName),
Short: true,
},
}
fields = append(newfields, fields...)
}

msg := Webhook{
UserName: os.Getenv(EnvSlackUserName),
IconURL: os.Getenv(EnvSlackIcon),
Channel: os.Getenv(EnvSlackChannel),
Attachments: []Attachment{
{
Fallback: envOr(EnvSlackMessage, "GITHUB_ACTION=" + os.Getenv("GITHUB_ACTION") + " \n GITHUB_ACTOR=" + os.Getenv("GITHUB_ACTOR") + " \n GITHUB_EVENT_NAME=" + os.Getenv("GITHUB_EVENT_NAME") + " \n GITHUB_REF=" + os.Getenv("GITHUB_REF") + " \n GITHUB_REPOSITORY=" + os.Getenv("GITHUB_REPOSITORY") + " \n GITHUB_WORKFLOW=" + os.Getenv("GITHUB_WORKFLOW")),
Color: envOr(EnvSlackColor, "good"),
AuthorName: envOr(EnvGithubActor, ""),
AuthorLink: "http://github.com/" + os.Getenv(EnvGithubActor),
AuthorIcon: "http://github.com/" + os.Getenv(EnvGithubActor) + ".png?size=32",
Footer: "<https://github.com/rtCamp/github-actions-library|Powered By rtCamp's GitHub Actions Library>",
Fields: fields,
},
},
Event: "message",
UserName: os.Getenv(EnvFlowdockUserName),
Thread: os.Getenv(EnvFlowdockThread),
Content: text,
Tags: strings.Split(os.Getenv(EnvFlowdockTags), ","),
}

if err := send(endpoint, msg); err != nil {
Expand Down
21 changes: 6 additions & 15 deletions main.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
#!/usr/bin/env bash

export GITHUB_BRANCH=${GITHUB_REF##*heads/}
export SLACK_ICON=${SLACK_ICON:-"https://avatars0.githubusercontent.com/u/43742164"}
export SLACK_USERNAME=${SLACK_USERNAME:-"rtBot"}
export FLOWDOCK_USERNAME=${FLOWDOCK_USERNAME:-"fdBot"}
export CI_SCRIPT_OPTIONS="ci_script_options"
export SLACK_TITLE=${SLACK_TITLE:-"Message"}
export FLOWDOCK_MESSAGE=${FLOWDOCK_MESSAGE:-"Message"}
export COMMIT_MESSAGE=$(cat "/github/workflow/event.json" | jq .commits | jq '.[0].message' -r)

hosts_file="$GITHUB_WORKSPACE/.github/hosts.yml"

if [[ -z "$SLACK_CHANNEL" ]]; then
user_slack_channel=$(cat "$hosts_file" | shyaml get-value "$CI_SCRIPT_OPTIONS.slack-channel" | tr '[:upper:]' '[:lower:]')
fi

if [[ -n "$user_slack_channel" ]]; then
export SLACK_CHANNEL="$user_slack_channel"
fi

# Login to vault using GH Token
if [[ -n "$VAULT_GITHUB_TOKEN" ]]; then
unset VAULT_TOKEN
vault login -method=github token="$VAULT_GITHUB_TOKEN" > /dev/null
fi

if [[ -n "$VAULT_GITHUB_TOKEN" ]] || [[ -n "$VAULT_TOKEN" ]]; then
export SLACK_WEBHOOK=$(vault read -field=webhook secret/slack)
export FLOWDOCK_TOKEN=$(vault read -field=token secret/flowdock)
fi

if [[ -f "$hosts_file" ]]; then
Expand All @@ -51,8 +42,8 @@ if [[ -n "$SITE_NAME" ]]; then
fi


if [[ -z "$SLACK_MESSAGE" ]]; then
export SLACK_MESSAGE="$COMMIT_MESSAGE"
if [[ -z "$FLOWDOCK_MESSAGE" ]]; then
export FLOWDOCK_MESSAGE="$COMMIT_MESSAGE"
fi

slack-notify "$@"
flowdock-notify "$@"

0 comments on commit 6cc6d01

Please sign in to comment.