|
| 1 | +# Sending notifications via webhooks |
| 2 | + |
| 3 | +It is possible to send notifications to external systems whenever an apply is being done. |
| 4 | + |
| 5 | +You can make requests to any HTTP endpoint or send messages directly to your Slack channel. |
| 6 | + |
| 7 | +::: tip NOTE |
| 8 | +Currently only `apply` events are supported. |
| 9 | +::: |
| 10 | + |
| 11 | +## Configuration |
| 12 | + |
| 13 | +Webhooks are configured in Atlantis [server-side configuration](server-configuration.md). |
| 14 | +There can be many webhooks: sending notifications to different destinations or for different |
| 15 | +workspaces/branches. Here is example configuration to send Slack messages for every apply: |
| 16 | + |
| 17 | +```yaml |
| 18 | +webhooks: |
| 19 | +- event: apply |
| 20 | + kind: slack |
| 21 | + channel: my-channel-id |
| 22 | +``` |
| 23 | +
|
| 24 | +If you are deploying Atlantis as a Helm chart, this can be implemented via the `config` parameter available for [chart customizations](https://github.com/runatlantis/helm-charts#customization): |
| 25 | + |
| 26 | +```yaml |
| 27 | +## Use Server Side Config, |
| 28 | +## ref: https://www.runatlantis.io/docs/server-configuration.html |
| 29 | +config: | |
| 30 | + --- |
| 31 | + webhooks: |
| 32 | + - event: apply |
| 33 | + kind: slack |
| 34 | + channel: my-channel-id |
| 35 | +``` |
| 36 | + |
| 37 | +### Filter on workspace/branch |
| 38 | + |
| 39 | +To limit notifications to particular workspaces or branches, use `workspace-regex` or `branch-regex` parameters. |
| 40 | +If the workspace **and** branch matches respective regex, an event will be sent. Note that empty regular expression |
| 41 | +(a result of unset parameter) matches every string. |
| 42 | + |
| 43 | +## Using HTTP webhooks |
| 44 | + |
| 45 | +You can send POST requests with JSON payload to any HTTP/HTTPS server. |
| 46 | + |
| 47 | +### Configuring Atlantis |
| 48 | + |
| 49 | +In your Atlantis [server-side configuration](server-configuration.md) you can add the following: |
| 50 | + |
| 51 | +```yaml |
| 52 | +webhooks: |
| 53 | +- event: apply |
| 54 | + kind: http |
| 55 | + url: https://example.com/hooks |
| 56 | +``` |
| 57 | + |
| 58 | +The `apply` event information will be POSTed to `https://example.com/hooks`. |
| 59 | + |
| 60 | +You can supply any additional headers with `--webhook-http-headers` parameter (or environment variable), |
| 61 | +for example for authentication purposes. See [webhook-http-headers](server-configuration.md#webhook-http-headers) for details. |
| 62 | + |
| 63 | +### JSON payload |
| 64 | + |
| 65 | +The payload is a JSON-marshalled [ApplyResult](https://pkg.go.dev/github.com/runatlantis/atlantis/server/events/webhooks#ApplyResult) struct. |
| 66 | + |
| 67 | +Example payload: |
| 68 | +```json |
| 69 | +{ |
| 70 | + "Workspace": "default", |
| 71 | + "Repo": { |
| 72 | + "FullName": "octocat/Hello-World", |
| 73 | + "Owner": "octocat", |
| 74 | + "Name": "Hello-World", |
| 75 | + "CloneURL": "https://:@github.com/octocat/Hello-World.git", |
| 76 | + "SanitizedCloneURL": "https://:<redacted>@github.com/octocat/Hello-World.git", |
| 77 | + "VCSHost": { |
| 78 | + "Hostname": "github.com", |
| 79 | + "Type": 0 |
| 80 | + } |
| 81 | + }, |
| 82 | + "Pull": { |
| 83 | + "Num": 2137, |
| 84 | + "HeadCommit": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", |
| 85 | + "URL": "https://github.com/octocat/Hello-World/pull/2137", |
| 86 | + "HeadBranch": "feature/some-branch", |
| 87 | + "BaseBranch": "main", |
| 88 | + "Author": "octocat", |
| 89 | + "State": 0, |
| 90 | + "BaseRepo": { |
| 91 | + "FullName": "octocat/Hello-World", |
| 92 | + "Owner": "octocat", |
| 93 | + "Name": "Hello-World", |
| 94 | + "CloneURL": "https://:@github.com/octocat/Hello-World.git", |
| 95 | + "SanitizedCloneURL": "https://:<redacted>@github.com/octocat/Hello-World.git", |
| 96 | + "VCSHost": { |
| 97 | + "Hostname": "github.com", |
| 98 | + "Type": 0 |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + "User": { |
| 103 | + "Username": "octocat", |
| 104 | + "Teams": null |
| 105 | + }, |
| 106 | + "Success": true, |
| 107 | + "Directory": "terraform/example", |
| 108 | + "ProjectName": "example-project" |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Using Slack hooks |
| 113 | + |
| 114 | +For this you'll need to: |
| 115 | + |
| 116 | +* Create a Bot user in Slack |
| 117 | +* Configure Atlantis to send notifications to Slack. |
| 118 | + |
| 119 | +### Configuring Slack for Atlantis |
| 120 | + |
| 121 | +* Go to [Slack: Apps](https://api.slack.com/apps) |
| 122 | +* Click the `Create New App` button |
| 123 | +* Select `From scratch` in the dialog that opens |
| 124 | +* Give it a name, e.g. `atlantis-bot`. |
| 125 | +* Select your Slack workspace |
| 126 | +* Click `Create App` |
| 127 | +* On the left go to `oAuth & Permissions` |
| 128 | +* Scroll down to Scopes | Bot Token Scopes and add the following OAuth scopes: |
| 129 | + * `channels:read` |
| 130 | + * `chat:write` |
| 131 | + * `groups:read` |
| 132 | + * `incoming-webhook` |
| 133 | + * `mpim:read` |
| 134 | +* Install the app onto your Slack workspace |
| 135 | +* Copy the `Bot User OAuth Token` and provide it to Atlantis by using `--slack-token=xoxb-xxxxxxxxxxx` or via the environment `ATLANTIS_SLACK_TOKEN=xoxb-xxxxxxxxxxx`. |
| 136 | +* Create a channel in your Slack workspace (e.g. `my-channel`) or use existing |
| 137 | +* Add the app to Created channel or existing channel ( click channel name then tab integrations, there Click "Add apps" |
| 138 | + |
| 139 | +### Configuring Atlantis |
| 140 | + |
| 141 | +After following the above steps it is time to configure Atlantis. Assuming you have already provided the `slack-token` (via parameter or environment variable) you can now instruct Atlantis to send `apply` events to Slack. |
| 142 | + |
| 143 | +In your Atlantis [server-side configuration](server-configuration.md) you can now add the following: |
| 144 | + |
| 145 | +```yaml |
| 146 | +webhooks: |
| 147 | +- event: apply |
| 148 | + kind: slack |
| 149 | + channel: my-channel-id |
| 150 | +``` |
0 commit comments