diff --git a/README.md b/README.md index 114dfd8..89d979f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ $ curl https://raw.githubusercontent.com/schickling/docker-hook/master/docker-ho #### Start `docker-hook` ```sh -$ docker-hook -t -c +$ docker-hook -t -c -b -s ``` ##### Auth-Token @@ -37,6 +37,14 @@ Please choose a secure `auth-token` string or generate one with `$ uuidgen`. Kee The `command` can be any bash command of your choice. See the following [example](#example). This command will be triggered each time someone makes a HTTP request. +##### Branch + +`branch` is the branch from github, only when the branch event will trigger `command`. `all` for all branch. + +##### Show + +`-s` is whether response status to github + ### 2. Configuration On Docker Hub Add a webhook like on the following image. `example.com` can be the domain of your server or its ip address. `docker-hook` listens to port `8555`. Please replace `my-super-safe-token` with your `auth-token`. diff --git a/docker-hook b/docker-hook index 147ac07..beef456 100755 --- a/docker-hook +++ b/docker-hook @@ -40,10 +40,31 @@ class RequestHandler(BaseHTTPRequestHandler): # Check if the secret URL was called token = args.token or os.environ.get("DOCKER_AUTH_TOKEN") if token == self.path[1:]: - logging.info("Start executing '%s'" % args.cmd) + logging.info("Got update") try: - Popen(args.cmd, env=env).wait() - self.send_response(200, "OK") + branch = args.branch + needExec = False + updateBranch = json_params['ref'] + logging.info("Branch is '%s'" % updateBranch) + if branch != 'all': + branch = 'refs/heads/' + branch + if updateBranch == branch: + needExec = True + else: + needExec = True + if needExec: + logging.info("Start executing '%s'" % args.cmd) + Popen(args.cmd, env=env).wait() + else: + logging.info("Branch is not '%s'" % args.branch) + if args.show: + data = {"exec": needExec, "success": True} + self.send_response(200) + self.send_header('Content-type', 'application/json') + self.end_headers() + self.wfile.write(json.dumps(data)) + else: + self.send_response(200, "OK") if 'callback_url' in json_params: # Make a callback to Docker Hub data = {'state': 'success'} @@ -53,7 +74,14 @@ class RequestHandler(BaseHTTPRequestHandler): data=json.dumps(data), headers=headers) except OSError as err: - self.send_response(500, "OSError") + if args.show: + data = {"exec": needExec, "success": False, "error": str(err)} + self.send_response(500) + self.send_header('Content-type', 'application/json') + self.end_headers() + self.wfile.write(json.dumps(data)) + else: + self.send_response(500, "OSError") logging.error("You probably didn't use 'sh ./script.sh'.") logging.error(err) if 'callback_url' in json_params: @@ -89,6 +117,15 @@ def get_parser(): dest="addr", default="0.0.0.0", help="address where it listens") + parser.add_argument("-b", + dest="branch", + default="master", + help="set trigger branch, all for all commit") + parser.add_argument("-s", + dest="show", + default="True", + type=bool, + help="Whether response detail to github") parser.add_argument("--port", dest="port", type=int,