diff options
-rw-r--r--[-rwxr-xr-x] | GitAutoDeploy.py | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | docs/Configuration.md | 94 | ||||
-rw-r--r-- | gitautodeploy/httpserver.py | 35 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlab.py | 8 |
5 files changed, 135 insertions, 5 deletions
diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py index 7d9ce7c..199ed2d 100755..100644 --- a/GitAutoDeploy.py +++ b/GitAutoDeploy.py @@ -6,3 +6,4 @@ if __name__ == '__main__': import gitautodeploy sys.stderr.write("\033[1;33m[WARNING]\033[0;33m GitAutoDeploy.py is deprecated. Please use \033[1;33m'python gitautodeploy%s'\033[0;33m instead.\033[0m\n" % (' ' + ' '.join(sys.argv[1:])).strip()) gitautodeploy.main() + @@ -22,6 +22,8 @@ Additionally, ```Git-Auto-Deploy``` can be configured to execute a shell command * 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 diff --git a/gitautodeploy/httpserver.py b/gitautodeploy/httpserver.py index a0ae4c5..1e0c198 100644 --- a/gitautodeploy/httpserver.py +++ b/gitautodeploy/httpserver.py @@ -39,6 +39,8 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): # Could be GitHubParser, GitLabParser or other repo_configs, ref, action = ServiceRequestParser(self._config).get_repo_params_from_request(request_headers, request_body) + logger.info("Event details - ref: %s; action: %s" % (ref or "master", action)) + #if success: # print "Successfullt handled request using %s" % ServiceHandler.__name__ #else: @@ -126,14 +128,37 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): # 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 filter['ref'] == ref and filter['action'] == action: + + # Filter with both action and ref? + if 'action' in filter and 'ref' in filter: + + if filter['action'] == action and filter['ref'] == ref: + # This filter is a match, OK to proceed continue + raise FilterMatchError() - else: - logger.error('Unrecognized filter: ' % filter) + + # Filter with only action? + if 'action' in filter: + + if filter['action'] == action: + # This filter is a match, OK to proceed + continue + raise FilterMatchError() - + + # Filter with only ref? + if 'ref' in filter: + + if filter['ref'] == ref: + # This filter is a match, OK to proceed + continue + + raise FilterMatchError() + + # Filter does not match, do not process this repo config + raise FilterMatchError() + except FilterMatchError as e: continue diff --git a/gitautodeploy/parsers/gitlab.py b/gitautodeploy/parsers/gitlab.py index 6ebdb6f..580fed2 100644 --- a/gitautodeploy/parsers/gitlab.py +++ b/gitautodeploy/parsers/gitlab.py @@ -26,6 +26,14 @@ class GitLabRequestParser(WebhookRequestParser): 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'] + # Get a list of configured repositories that matches the incoming web hook reqeust repo_configs = self.get_matching_repo_configs(repo_urls) |