-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Adnan Hajdarevic <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,84 @@ | ||
# webhook | ||
# What is webhook? | ||
[webhook](https://github.com/adnanh/webhook/) is a lightweight configurable tool written in Go, that allows you to easily create HTTP endpoints (hooks) on your server, which you can use to execute configured commands. You can also pass data from the HTTP request (such as headers, payload or query variables) to your commands. [webhook](https://github.com/adnanh/webhook/) also allows you to specify rules which have to be satisfied in order for the hook to be triggered. | ||
|
||
## Installing | ||
*Please note:* Before installing the webhook, make sure you have installed `go` and properly set up your `$GOPATH` environment variable. | ||
For example, if you're using Github or Bitbucket, you can use [webhook](https://github.com/adnanh/webhook/) to set up a hook that runs a redeploy script for your project on your staging server, whenever you push changes to the master branch of your project. | ||
|
||
```go | ||
If you use Slack, you can set up an "Outgoing webhook integration" to run various commands on your server, which can then report back directly to your Slack channels using the "Incoming webhook integrations". | ||
|
||
[webhook](https://github.com/adnanh/webhook/) aims to do nothing more than it should do, and that is: | ||
1. receive the request, | ||
2. parse the headers, payload and query variables, | ||
3. check if the specified rules for the hook are satisfied, | ||
3. and finally, pass the specified arguments to the specified command. | ||
|
||
Everything else is the responsibility of the command's author. | ||
|
||
--- | ||
|
||
# Getting started | ||
To get started, first make sure you've properly set up your [Golang](http://golang.org/doc/install) environment and then run the | ||
```bash | ||
$ go get github.com/adnanh/webhook | ||
``` | ||
to get the latest version of the [webhook](https://github.com/adnanh/webhook/). | ||
|
||
## Updating | ||
```go | ||
$ go get -u github.com/adnanh/webhook | ||
``` | ||
Next step is to define some hooks you want [webhook](https://github.com/adnanh/webhook/) to serve. Begin by creating an empty file named `hooks.json`. This file will contain an array of hooks the [webhook](https://github.com/adnanh/webhook/) will serve. Check [Hook definition page](Hook-Definition) to see the detailed description of what properties a hook can contain, and how to use them. | ||
|
||
## Adding hooks | ||
Hooks are defined using JSON format. The _hooks file_ must contain an array of JSON formatted hooks. Here is an example of a valid _hooks file_ containing only one hook. The hook will be triggered whenever a push to the master branch occurrs. | ||
Let's define a simple hook named `redeploy-webhook` that will run a redeploy script located in `/var/scripts/redeploy.sh`. | ||
|
||
Our `hooks.json` file will now look like this: | ||
```json | ||
[ | ||
{ | ||
"id": "hook-1", | ||
"command": "OS command to be executed when the hook gets triggered", | ||
"args": [ | ||
"ref", | ||
"repository.owner.name" | ||
], | ||
"cwd": "current working directory under which the specified command will be executed (optional, defaults to the directory where the binary resides)", | ||
"secret": "secret key used to compute the hash of the payload (optional)", | ||
"trigger-rule": | ||
{ | ||
"match": | ||
{ | ||
"parameter": "ref", | ||
"value": "refs/heads/master" | ||
} | ||
} | ||
"id": "redeploy-webhook", | ||
"execute-command": "/var/scripts/redeploy.sh", | ||
"command-working-directory": "/var/webhook" | ||
} | ||
] | ||
``` | ||
## Trigger rules | ||
### And | ||
*And rule* will evaluate to _true_, if and only if all of the sub rules evaluate to _true_. | ||
```json | ||
{ | ||
"and": | ||
[ | ||
{ | ||
"match": | ||
{ | ||
"parameter": "ref", | ||
"value": "refs/heads/master" | ||
} | ||
}, | ||
{ | ||
"match": | ||
{ | ||
"parameter": "repository.owner.name", | ||
"value": "adnanh" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
### Or | ||
*Or rule* will evaluate to _true_, if any of the sub rules evaluate to _true_. | ||
```json | ||
{ | ||
"or": | ||
[ | ||
{ | ||
"match": | ||
{ | ||
"parameter": "ref", | ||
"value": "refs/heads/master" | ||
} | ||
}, | ||
{ | ||
"match": | ||
{ | ||
"parameter": "ref", | ||
"value": "refs/heads/development" | ||
} | ||
} | ||
] | ||
} | ||
|
||
You can now run [webhook](https://github.com/adnanh/webhook/) using | ||
```bash | ||
$ /path/to/webhook -h hooks.json | ||
``` | ||
### Not | ||
*Not rule* will evaluate to _true_, if and only if the sub rule evaluates to _false_. | ||
```json | ||
{ | ||
"not": | ||
{ | ||
"match": | ||
{ | ||
"parameter": "ref", | ||
"value": "refs/heads/master" | ||
} | ||
} | ||
} | ||
|
||
It will start up on default port 9000 and will provide you with one HTTP endpoint | ||
```http | ||
http://yourserver:9000/hooks/redeploy-webhook | ||
``` | ||
### Match | ||
*Match rule* will evaluate to _true_, if and only if the payload JSON object contains the key specified in the `parameter` field that has the same value as specified in the `value` field. | ||
|
||
*Please note:* Due to technical reasons, _number_ and _boolean_ values in the _hooks file_ must be wrapped around with a pair of quotes. | ||
Check [webhook parameters page](Webhook-Parameters) to see how to override the ip, port and other settings when starting the [webhook](https://github.com/adnanh/webhook/). | ||
|
||
```json | ||
{ | ||
"match": | ||
{ | ||
"parameter": "repository.id", | ||
"value": "123456" | ||
} | ||
} | ||
``` | ||
By performing a simple HTTP GET or POST request to that endpoint, your specified redeploy script would be executed. Neat! | ||
|
||
It is possible to specify the values deeper in the payload JSON object with the dot operator, and if a value of the specified key happens to be an array, it's possible to index the array values by using the number instead of a string as the key, which is shown in the following example: | ||
```json | ||
{ | ||
"match": | ||
{ | ||
"parameter": "commits.0.author.username", | ||
"value": "adnanh" | ||
} | ||
} | ||
``` | ||
## Running | ||
After installing webhook, in your `$GOPATH/bin` directory you should have `webhook` binary. | ||
However, hook defined like that could pose a security threat to your system, because anyone who knows your endpoint, can send a request and execute your command. To prevent that, you can use the `"trigger-rule"` property for your hook, to specify the exact circumstances under which the hook would be triggered. For example, you can use them to add a secret that you must supply as a parameter in order to successfully trigger the hook. Please check out the [Hook rules page](Hook-Rules) for detailed list of available rules and their usage. | ||
|
||
By simply running the binary using the `./webhook` command, the webhook will start with the default options. | ||
That means the webhook will listen on _all interfaces_ on port `9000`. It will try to read and parse `hooks.json` file from the same directory where the binary is located, and it will log everything to `stdout` and the file `webhook.log`. | ||
# Examples | ||
Check out [Hook examples page](Hook-Examples) for more complex examples of hooks. | ||
|
||
To override any of these options, you can use the following command line flags: | ||
```bash | ||
-hooks="hooks.json": path to the json file containing defined hooks the webhook should serve | ||
-ip="": ip the webhook server should listen on | ||
-log="webhook.log": path to the log file | ||
-port=9000: port the webhook server should listen on | ||
``` | ||
# Contributing | ||
Any form of contribution is welcome and highly appreciated. | ||
|
||
# License | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Adnan Hajdarevic <[email protected]> | ||
|
||
All hooks are served under the `http://ip:port/hook/:id`, where the `:id` corresponds to the hook *id* specified in _hooks file_. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
Visiting `http://ip:port` will show version, uptime and number of hooks the webhook is serving. | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
## Todo | ||
* Add support for ip white/black listing | ||
* Add "match-regex" rule | ||
* ??? | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |