Skip to content

Commit 7197537

Browse files
Merge pull request #2 from mike-sheppard/support-multiple-slack-channels
Add support for multiple Slack orgs/channels
2 parents 928800b + e44fd32 commit 7197537

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

README.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
[![GitHub tag](https://img.shields.io/github/tag/ItinerisLtd/trellis-slack-webhook-notify-during-deploy.svg)](https://github.com/ItinerisLtd/trellis-slack-webhook-notify-during-deploy/tags)
44
[![license](https://img.shields.io/github/license/ItinerisLtd/trellis-slack-webhook-notify-during-deploy.svg)](https://github.com/ItinerisLtd/trellis-slack-webhook-notify-during-deploy/blob/master/LICENSE)
55

6-
76
Sends an initial deployment started and a deployment successful message to a Slack channel when [Trellis](https://github.com/roots/trellis) deploys [Bedrock](https://github.com/roots/bedrock).
87

98
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
10-
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
11-
9+
<!-- DON"T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1210

1311
- [Requirements](#requirements)
1412
- [Screenshots](#screenshots)
@@ -34,11 +32,13 @@ Sends an initial deployment started and a deployment successful message to a Sla
3432
- Valid Slack Webhook URL
3533

3634
## Screenshots
35+
3736
![Deployment Screenshot](deployment-screenshot.png)
3837

3938
## Installation
4039

4140
Add this role to `requirements.yml` or `galaxy.yml` in newer versions of Trellis:
41+
4242
```yaml
4343
# requirements.yml / galaxy.yml
4444
- name: slack-notify
@@ -47,19 +47,21 @@ Add this role to `requirements.yml` or `galaxy.yml` in newer versions of Trellis
4747
```
4848
4949
Run the command:
50+
5051
```bash
5152
➜ ansible-galaxy install -r requirements.yml --force
5253
```
5354

5455
With newer versions of Trellis, run:
56+
5557
```bash
5658
➜ ansible-galaxy install -r galaxy.yml --force
5759
```
5860

59-
6061
## Role Variables
6162

62-
Add these roles`deploy_before` & `deploy_after` to the deploy [hooks](https://roots.io/trellis/docs/deploys/#hooks):
63+
Add the start & success tasks to the `deploy_before` & `deploy_after` deploy [hooks](https://roots.io/trellis/docs/deploys/#hooks):
64+
6365
```yaml
6466
# roles/deploy/defaults/main.yml
6567
deploy_before:
@@ -71,13 +73,17 @@ deploy_after:
7173
- "{{ playbook_dir }}/vendor/roles/slack-notify/tasks/deploy_success.yml"
7274
```
7375
74-
Add your Webhook token (end of the Webhook URL), channel and projects github repo into `group_vars/all/main.yml`
76+
Add your Slack webhook token (end of the webhook URL) and channel into `group_vars/all/main.yml`
7577

7678
```yaml
7779
# group_vars/all/main.yml
78-
slack_webhook_token: XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX
79-
slack_webhook_channel: '#development-alerts'
80-
slack_git_repo_url: "https://github.com/username/repo-name"
80+
slack_notifications:
81+
- name: Organisation1
82+
token: XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX
83+
channel: "#site-status"
84+
- name: Organisation2
85+
token: XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX
86+
channel: "#deployments"
8187
```
8288

8389
## Usage

tasks/deploy_start.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
---
22
- name: Send initial deployment notification to Slack
33
slack:
4-
token: "{{ slack_webhook_token }}"
5-
channel: slack_webhook_channel
4+
token: "{{ item.token }}"
5+
channel: "{{ item.channel }}"
66
attachments:
77
- text: "*{{ lookup('pipe', 'git config user.name') }}* has started deploying {{ site }} to *<https://{{ project.site_hosts | map(attribute='canonical') | first }}|{{ env }}>*"
88
color: warning
9-
when: slack_webhook_token is defined and slack_webhook_token != ""
9+
loop: "{{ slack_notifications }}"
10+
when:
11+
- slack_notifications is defined
12+
- slack_notifications | length > 0
1013
delegate_to: localhost
1114
ignore_errors: yes

tasks/deploy_success.yml

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
---
2-
- name: Get slack & git user info
3-
set_fact:
4-
target_env_url: "https://{{ project.site_hosts | map(attribute='canonical') | first }}|{{ env }}"
5-
git_username: "{{ lookup('pipe', 'git config user.name') }}"
6-
using_github: "{{ project_git_repo.find('github') != -1 }}"
7-
ignore_errors: yes
2+
- name: Get slack & git user info
3+
set_fact:
4+
target_env_url: "https://{{ project.site_hosts | map(attribute='canonical') | first }}|{{ env }}"
5+
git_username: "{{ lookup('pipe', 'git config user.name') }}"
6+
using_github: "{{ project_git_repo.find('github') != -1 }}"
7+
ignore_errors: yes
88

9-
- name: Get github commit url
10-
set_fact:
11-
github_commit_url: "https://github.com/{{ project_git_repo | regex_replace('(?:^git@github.com:|https:\/\/github.com\/)(.*).git$', '\\1') }}/commit/{{ git_clone.after }}"
12-
when:
13-
- using_github
14-
ignore_errors: yes
9+
- name: Get github commit url
10+
set_fact:
11+
github_commit_url: "https://github.com/{{ project_git_repo | regex_replace('(?:^git@github.com:|https:\/\/github.com\/)(.*).git$', '\\1') }}/commit/{{ git_clone.after }}"
12+
when:
13+
- using_github
14+
ignore_errors: yes
1515

16-
- name: Send successful deployment notification to Slack
17-
slack:
18-
token: "{{ slack_webhook_token }}"
19-
channel: slack_webhook_channel
20-
attachments:
21-
- text: "*{{ git_username }}* successfully deployed {{ site }} to *<{{ target_env_url }}>* {% if using_github %} [<{{ github_commit_url }}|deployed commit>] {% else %} [{{ git_clone.after }}] {% endif %}"
22-
color: good
23-
when:
24-
- slack_webhook_token is defined
25-
- slack_webhook_token != ""
26-
delegate_to: localhost
27-
ignore_errors: yes
16+
- name: Send successful deployment notification to Slack
17+
slack:
18+
token: "{{ item.token }}"
19+
channel: "{{ item.channel }}"
20+
attachments:
21+
- text: "*{{ git_username }}* successfully deployed {{ site }} to *<{{ target_env_url }}>* {% if using_github %} [<{{ github_commit_url }}|deployed commit>] {% else %} [{{ git_clone.after }}] {% endif %}"
22+
color: good
23+
loop: "{{ slack_notifications }}"
24+
when:
25+
- slack_notifications is defined
26+
- slack_notifications | length > 0
27+
delegate_to: localhost
28+
ignore_errors: yes

0 commit comments

Comments
 (0)