diff options
-rwxr-xr-x | GitAutoDeploy.py | 17 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | docs/Configuration.md | 94 |
3 files changed, 110 insertions, 3 deletions
diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py index 7deddac..8827ca0 100755 --- a/GitAutoDeploy.py +++ b/GitAutoDeploy.py @@ -228,6 +228,14 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): if k in data['repository']: repo_urls.append(data['repository'][k]) + # extract the branch + if 'ref' in data: + ref = data['ref'] + + # set the action + if 'object_kind' in data: + action = data['object_kind'] + # Assume GitHub if the X-GitHub-Event HTTP header is set elif github_event: @@ -311,6 +319,7 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): else: logger.error("ERROR - Unable to recognize request origin. Don't know how to handle the request.") + logger.info("Event details - ref: %s; action: %s" % (ref or "master", action)) return repo_urls, ref or "master", action @@ -405,13 +414,15 @@ class GitAutoDeploy(object): # Verify that all filters matches the request if specified if 'filters' in repo_config: for filter in repo_config['filters']: - if filter['type'] == 'pull-request-filter': + if 'type' in filter and filter['type'] == 'pull-request-filter': if filter['ref'] == ref and filter['action'] == action: continue raise FilterMatchError() else: - logger.error('Unrecognized filter: ' % filter) - raise FilterMatchError() + if 'action' in filter and filter['action'] != action: + raise FilterMatchError() + if 'ref' in filter and filter['ref'] != ref: + raise FilterMatchError() except FilterMatchError as e: continue @@ -22,6 +22,8 @@ Additionally, ```GitAutoDeploy.py``` can be configured to execute a shell comman * Make sure that the ```pidfilepath``` path is writable for the user running the script, as well as any other path configured for your repositories. * If you don't want to execute ```git pull``` after webhook was fired, you can leave field ```"path"``` empty. +See the [Configuration](./docs/Configuration.md) documentation for more details. + ### Logging To start logging you can define ```"logfilepath": "/home/hermes/gitautodeploy.log"```. Note that you can`t see triggered command output when log is defined, only script output. If you leave ```"logfilepath"``` empty - everething will work as usual (without log). diff --git a/docs/Configuration.md b/docs/Configuration.md new file mode 100644 index 0000000..c1be297 --- /dev/null +++ b/docs/Configuration.md @@ -0,0 +1,94 @@ +# Configuration +The configuration file is formatted in `JSON`. The possible root elements are +as follow: + + - **pidfilepath**: The path where `pid` files are kept. + - **logfilepath**: To enable logging, set this to a valid file path. + - **host**: What IP address to listen on. + - **port**: The port for the web server to listen on. + - **global_deploy**: An array of two specific commands or path to scripts + to be executed for all repositories defined: + - `[0]` = The pre-deploy script. + - `[1]` = The post-deploy script. + - **repositories**: An array of repository configurations. + +## Repositories +Repository configurations are comprised of the following elements: + + - **url**: The URL to the repository. + - **branch**: The branch which will be checked out. + - **remote**: The name of the remote to use. + - **path**: Path to clone the repository to. If omitted, the repository won't + be cloned, only the deploy scripts will be executed. + - **deploy**: A command to be executed. If `path` is set, the command is + executed after a successfull `pull`. + - **filters**: Filters to apply to the web hook events so that only the desired + events result in executing the deploy actions. See section *Filters* for more + details. + +## Filters +*(Currently only supported for GitHub and GitLab)* + +With filter, it is possible to trigger the deploy only if the criteria are met. +For example, deploy on `push` to the `master` branch only, ignore other branches. + +Filters are defined by providing an `action` and/or a `ref` element (most the +time both would be used in the filter ) + +A special filter syntax also exist to take care of GitHub pull-requests. See +[Continuous Delivery via Pull requests](./Continuous Delivery via Pull requests.md) +for details on how it works. + +# Examples + +Execute pre-deploy script, don't `pull` the repository but execute a deploy +script, and finish with a post-deploy script. Execute only for `push` events on +the `master` branch. + +```json +{ + "pidfilepath": "~/.gitautodeploy.pid", + "host": "0.0.0.0", + "port": 8080, + "global_deploy": [ + "echo Pre-deploy script", + "echo Post-deploy script" + ], + "repositories": [ + { + "url": "http://gitlab/playground/hooktest.git", + "deploy": "echo deploying", + "filters": [ + { + "action": "push", + "ref": "refs/heads/master" + } + ] + } + ] +} +``` + +Clone repository on `push` to `master`. + +```json +{ + "pidfilepath": "~/.gitautodeploy.pid", + "host": "0.0.0.0", + "port": 8080, + "repositories": [ + { + "url": "http://gitlab/playground/hooktest.git", + "branch": "master", + "remote": "origin", + "path": "~/repositories/hooktest", + "filters": [ + { + "action": "push", + "ref": "refs/heads/master" + } + ] + } + ] +} +```
\ No newline at end of file |