├── .gitignore ├── screenshot.jpg ├── .editorconfig ├── meta └── main.yml ├── tasks ├── deploy_start.yml └── deploy_success.yml ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # OS 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bluesky950520/trellis-slack-deploy-notifications/HEAD/screenshot.jpg -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 2 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | galaxy_info: 2 | description: Sends notifications to Slack before & after deployment via Trellis. 3 | license: MIT 4 | min_ansible_version: 2.6 5 | platforms: 6 | - name: Ubuntu 7 | versions: 8 | - xenial 9 | - bionic 10 | galaxy_tags: 11 | - trellis 12 | - wordpress 13 | - slack 14 | - webhook 15 | - notify 16 | dependencies: [] 17 | -------------------------------------------------------------------------------- /tasks/deploy_start.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Set ENV & Git vars 3 | set_fact: 4 | git_username: "{{ lookup('pipe', 'git config user.name') }}" 5 | branch_name: "{{ lookup('pipe', 'git branch --show-current') }}" 6 | target_env_url: "https://{{ project.site_hosts | map(attribute='canonical') | first }}|{{ site }} {{ env }}" 7 | ci_job_url: "{{ lookup('env', 'CI_JOB_URL') }}" 8 | ignore_errors: yes 9 | 10 | - name: Send initial deployment notification to Slack 11 | slack: 12 | token: "{{ slack_deploy_token }}" 13 | attachments: 14 | - text: "*{{ git_username }}* deploying 🔀`{{ branch_name }}` to *<{{ target_env_url }}>* {% if ci_job_url | length > 0 %}[<{{ ci_job_url }}|🔎>]{% endif %}" 15 | color: warning 16 | with_items: "{{ vault_wordpress_sites[site].slack_deploy_token }}" 17 | loop_control: 18 | loop_var: slack_deploy_token 19 | when: 20 | - vault_wordpress_sites[site].slack_deploy_token is defined 21 | - vault_wordpress_sites[site].slack_deploy_token | length > 0 22 | delegate_to: localhost 23 | ignore_errors: yes 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /tasks/deploy_success.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Set ENV & Git vars 3 | set_fact: 4 | git_username: "{{ lookup('pipe', 'git config user.name') }}" 5 | branch_name: "{{ lookup('pipe', 'git branch --show-current') }}" 6 | target_env_url: "https://{{ project.site_hosts | map(attribute='canonical') | first }}|{{ site }} {{ env }}" 7 | commit_sha: "{{ git_clone.after }}" 8 | using_github: "{{ project_git_repo.find('github') != -1 }}" 9 | ignore_errors: yes 10 | 11 | - name: Set Github commit URL 12 | set_fact: 13 | github_commit_url: "https://github.com/{{ project_git_repo | regex_replace('(?:^git@github.com:|https:\/\/github.com\/)(.*).git$', '\\1') }}/commit/{{ git_clone.after }}" 14 | when: 15 | - using_github 16 | ignore_errors: yes 17 | 18 | - name: Override commit_sha with link, if GitHub commit URL 19 | set_fact: 20 | commit_sha: "<{{ github_commit_url }}|📝>" 21 | when: 22 | - using_github 23 | - github_commit_url | length > 0 24 | ignore_errors: yes 25 | 26 | - name: Send successful deployment notification to Slack 27 | slack: 28 | token: "{{ slack_deploy_token }}" 29 | attachments: 30 | - text: "*{{ git_username }}* deployed 🔀`{{ branch_name }}` to *<{{ target_env_url }}>* [{{ commit_sha }}]" 31 | color: good 32 | with_items: "{{ vault_wordpress_sites[site].slack_deploy_token }}" 33 | loop_control: 34 | loop_var: slack_deploy_token 35 | when: 36 | - vault_wordpress_sites[site].slack_deploy_token is defined 37 | - vault_wordpress_sites[site].slack_deploy_token | length > 0 38 | delegate_to: localhost 39 | ignore_errors: yes 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Trellis Slack Deploy Notifications 2 | 3 | Sends notifications to Slack before & after deployments via [Trellis](https://github.com/roots/trellis). Forked from [trellis-slack-webhook-notify-during-deploy](https://github.com/ItinerisLtd/trellis-slack-webhook-notify-during-deploy). 4 | 5 | ## Requirements 6 | 7 | - Trellis v1.0.0 or later 8 | - Trellis CLI 9 | - Ansible v2.6 or later 10 | - Slack webhook token 11 | 12 | ## Screenshots 13 | 14 | ![Deployment Screenshot](./screenshot.jpg) 15 | 16 | ## Installation 17 | 18 | Add the role to `galaxy.yml` + [check for the latest version](https://github.com/smithfield-studio/trellis-slack-deploy-notifications/releases). 19 | 20 | ```yaml 21 | --- 22 | roles: 23 | #... 24 | - name: trellis-slack-deploy-notifications 25 | src: https://github.com/smithfield-studio/trellis-slack-deploy-notifications 26 | version: 1.0.1 27 | ``` 28 | 29 | Ensure you have [Trellis CLI](https://github.com/roots/trellis-cli) installed, then run: 30 | 31 | ```bash 32 | trellis galaxy install 33 | ``` 34 | 35 | ## Update Deploy Hooks 36 | 37 | Add the start & success tasks to the `deploy_before` & `deploy_after` [deploy hooks](https://roots.io/trellis/docs/deploys/#hooks) in `roles/deploy/defaults/main.yml`. 38 | 39 | ```yaml 40 | deploy_before: 41 | - '{{ playbook_dir }}/vendor/roles/trellis-slack-deploy-notifications/tasks/deploy_start.yml' 42 | 43 | #... 44 | 45 | deploy_after: 46 | - '{{ playbook_dir }}/vendor/roles/trellis-slack-deploy-notifications/tasks/deploy_success.yml' 47 | ``` 48 | 49 | Add your Slack webhook token(s) (end of the webhook URL) and channel into `group_vars/{environment}/vault.yml` 50 | 51 | ```yaml 52 | vault_wordpress_sites: 53 | example.com.au: 54 | slack_deploy_token: 55 | - xxx/xxx/xxxxx 56 | - xxx/xxx/xxxxx 57 | env: #... 58 | ``` 59 | 60 | ## Running via GitHub Actions / CI 61 | See [setup-trellis-cli](https://github.com/roots/setup-trellis-cli) to get the workflow setup. Then add the `CI_JOB_URL` to env var to your deploy workflow to include a link back to the running GitHub Action in the start notification. 62 | 63 | ```yaml 64 | name: Deploy site 65 | env: 66 | CI_JOB_URL: ${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }} 67 | ``` 68 | 69 | ## FAQs 70 | 71 | ### How do I get a Slack Webhook URL? 72 | 73 | 1. Visit the ["Your Apps"](https://api.slack.com/apps) page on Slack 74 | 2. Create an App (e.g. "Deployment alerts") and enable "Incoming Webhooks" 75 | 3. Setup a Webhook for your desired Workspace 76 | 4. Copy the token from the Webhook URL for use in [Installation](#installation) 77 | 78 | Note: Each Webhook can only post to one channel since Slack changed their API. You will need to set up a webhook per channel you wish to notify. 79 | 80 | ## See Also 81 | 82 | - [Sending messages using incoming webhooks](https://api.slack.com/messaging/webhooks) 83 | --------------------------------------------------------------------------------