From 7b03e2812d28eb457805c674c8bf9a199a25a0e1 Mon Sep 17 00:00:00 2001 From: Wang Hailong Date: Sun, 9 Dec 2018 23:07:52 +0800 Subject: [PATCH 1/3] ADD: monitoring branch --- README.md | 2 +- docker-hook | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 114dfd8..bcaffa5 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 -b -t -c ``` ##### Auth-Token diff --git a/docker-hook b/docker-hook index 147ac07..cc9fdf2 100755 --- a/docker-hook +++ b/docker-hook @@ -40,9 +40,24 @@ 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() + 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) + self.send_response(200, "OK") if 'callback_url' in json_params: # Make a callback to Docker Hub @@ -89,6 +104,10 @@ 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("--port", dest="port", type=int, From 944b6ddf47a46a0fe59593126bf13aa8e003f64a Mon Sep 17 00:00:00 2001 From: Wang Hailong Date: Sun, 9 Dec 2018 23:13:51 +0800 Subject: [PATCH 2/3] ADD: readme --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bcaffa5..35d9882 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 -b -t -c +$ docker-hook -t -c -b ``` ##### Auth-Token @@ -37,6 +37,10 @@ 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. + ### 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`. From 98901a18101b96724b28a4e882da39c8acbdcfe2 Mon Sep 17 00:00:00 2001 From: Wang Hailong Date: Mon, 10 Dec 2018 05:51:17 +0800 Subject: [PATCH 3/3] ADD: show detail to github --- README.md | 6 +++++- docker-hook | 24 +++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 35d9882..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 -b +$ docker-hook -t -c -b -s ``` ##### Auth-Token @@ -41,6 +41,10 @@ The `command` can be any bash command of your choice. See the following [example `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 cc9fdf2..beef456 100755 --- a/docker-hook +++ b/docker-hook @@ -57,8 +57,14 @@ class RequestHandler(BaseHTTPRequestHandler): Popen(args.cmd, env=env).wait() else: logging.info("Branch is not '%s'" % args.branch) - - self.send_response(200, "OK") + 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'} @@ -68,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: @@ -108,6 +121,11 @@ def get_parser(): 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,