diff options
-rw-r--r-- | GitAutoDeploy.py | 1 | ||||
-rw-r--r-- | docs/Configuration.md | 54 | ||||
-rw-r--r-- | gitautodeploy/httpserver.py | 44 |
3 files changed, 60 insertions, 39 deletions
diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py index 199ed2d..7d9ce7c 100644 --- a/GitAutoDeploy.py +++ b/GitAutoDeploy.py @@ -6,4 +6,3 @@ 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() - diff --git a/docs/Configuration.md b/docs/Configuration.md index 788be3e..953385d 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -52,14 +52,25 @@ Repository configurations are comprised of the following elements: 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 ) +Filters are defined by providing keys/values to be looked up in the original +data sent by the web hook. -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. +For example, GitLab web hook data looks like this: + +```json + { + "object_kind":"build", + "ref":"master", + "tag":false, + ... + } +``` + +A filter can use `object_kind` and `ref` attributes for example to execute the +deploy action only on a `build` event on the `master` branch. # Examples +*(Note: the filter examples below are valid for GitLab)* 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 @@ -80,7 +91,7 @@ the `master` branch. "deploy": "echo deploying", "filters": [ { - "action": "push", + "object_kind": "push", "ref": "refs/heads/master" } ] @@ -104,7 +115,7 @@ Clone repository on `push` to `master`. "path": "~/repositories/hooktest", "filters": [ { - "action": "push", + "object_kind": "push", "ref": "refs/heads/master" } ] @@ -113,6 +124,33 @@ Clone repository on `push` to `master`. } ``` +Execute script upon GitLab CI successful build of `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 project!", + "filters": [ + { + "object_kind": "build", + "ref": "master", + "build_status": "success" + } + ] + } + ] +} +``` + # Repository configuration using environment variables It's possible to configure up to one repository using environment variables. This can be useful in some specific use cases where a full config file is undesired. @@ -123,4 +161,4 @@ GAD_REPO_URL | Repository URL GAD_REPO_BRANCH | GAD_REPO_REMOTE | GAD_REPO_PATH | Path to where ```Git-Auto-Deploy``` should clone and pull repository -GAD_REPO_DEPLOY | Deploy command
\ No newline at end of file +GAD_REPO_DEPLOY | Deploy command diff --git a/gitautodeploy/httpserver.py b/gitautodeploy/httpserver.py index 1e0c198..0760067 100644 --- a/gitautodeploy/httpserver.py +++ b/gitautodeploy/httpserver.py @@ -53,7 +53,7 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): # Wait one second before we do git pull (why?) Timer(1.0, self.process_repositories, (repo_configs, ref, - action)).start() + action, request_body)).start() def log_message(self, format, *args): """Overloads the default message logging method to allow messages to @@ -112,7 +112,7 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): return - def process_repositories(self, repo_configs, ref, action): + def process_repositories(self, repo_configs, ref, action, request_body): import os import time import logging @@ -120,49 +120,33 @@ class WebhookRequestHandler(BaseHTTPRequestHandler): from lock import Lock logger = logging.getLogger() + data = json.loads(request_body) # Process each matching repository for repo_config in repo_configs: try: - # Verify that all filters matches the request if specified + # Verify that all filters matches the request (if any filters are specified) if 'filters' in repo_config: - for filter in repo_config['filters']: - - # 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() - - # Filter with only action? - if 'action' in filter: - if filter['action'] == action: - # This filter is a match, OK to proceed - continue - - raise FilterMatchError() + # at least one filter must match + for filter in repo_config['filters']: - # Filter with only ref? - if 'ref' in filter: + # all options specified in the filter must match + for filter_key, filter_value in filter.iteritems(): - if filter['ref'] == ref: - # This filter is a match, OK to proceed + # support for earlier version so it's non-breaking functionality + if filter_key == 'action' and filter_value == action: continue - raise FilterMatchError() - - # Filter does not match, do not process this repo config - raise FilterMatchError() + if filter_key not in data or filter_value != data[filter_key]: + raise FilterMatchError() except FilterMatchError as e: + + # Filter does not match, do not process this repo config continue - # In case there is no path configured for the repository, no pull will # be made. if not 'path' in repo_config: |