Goal
Add a webhook to a single project, or to all of your Platform.sh projects.
Assumptions
- One or more active projects on Platform.sh
- The Platform.sh CLI installed locally
Problems
You can use webhooks to integrate Platform.sh into your automation chain to trigger actions somewhere else. They can be configured to fire on all events, or specific events that happen on specific environments.
More information about Webhooks on Platform.sh is available in the public documentation.
Steps (Adding a webhook to a single project)
1. Get the project ID
If the local repository already has the remote project set, navigate to that directory. Otherwise, retrieve the project ID using platform project:list
2. Set up the webhook
Add the webhook for the desired URL that can receive posted JSON:
$ platform integration:add --type=webhook --project=<project ID> --url=A-URL-THAT-CAN-RECEIVE-THE-POSTED-JSON
Then specify the desired webhook properties:
Events to report (--events)
A list of events to report, e.g. environment.push
Default: *
Enter comma-separated values (or leave this blank)
> environment.push
States to report (--states)
A list of states to report, e.g. pending, in_progress, complete
Default: complete
Enter comma-separated values (or leave this blank)
> complete
Included environments (--environments)
The environment IDs to include
Default: *
Enter comma-separated values (or leave this blank)
> *
Excluded environments (--excluded-environments)
The environment IDs to exclude
Enter comma-separated values (or leave this blank)
>
Created integration wuw76ebyhb5ni (type: webhook)
+----------------------+-------------------------------------------------------+
| Property | Value |
+----------------------+-------------------------------------------------------+
| id | wuw76ebyhb5ni |
| type | webhook |
| events | - environment.push |
| environments | - '*' |
| excluded_environment | { } |
| s | |
| states | - complete |
| url | <url> |
+----------------------+-------------------------------------------------------+
You can also, evidently pass all the arguments on the command line, so this step itself can be automated.
Steps (Adding a webhook to all projects)
1. Add the webhooks in a single command
$ platform multi -p$(platform projects --my --columns id --pipe | paste -sd "," -) "integration:add --type webhook --url https://example.com/receiver --events * --states complete --environments * --excluded-environments ''"
The CLI command can be broken down into the following parts:
-
platform multi
runs CLI command on multiple projects -
platform projects --my --columns id --pipe
outputs the list of projects with only their IDs separated by newline -
paste -sd "," -
joins that array of project IDs with a comma -
integration:add --type webhook --url https://example.com/receiver --events * --states complete --environments * --excluded-environments ''
adds a webhook for all events and all environments
2. Bonus - add the integration to all projects that do not have one configured.
And what if you only want to add the integration if it does not already exist?
platform multi -p$(platform multi -p$(platform projects --my --columns id --pipe | paste -sd "," -) "integration:list --format=csv --no-header" 2>&1 | grep -B1 "No integrations found" |grep -o "\(([[:alnum:]]*)\)$" | cut -c2-14| paste -sd "," -) "integration:add --type webhook --url https://example.com --events * --states complete --environments * --excluded-environments ''"
But then again, bash one liners… you’d be better off at this point using the API directly rather then relying on my bash-foo.
Conclusions
- Using the Platform.sh CLI, webhook integrations can easily be added to single projects or even to every project.
- Bash is cool.