Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $ curl https://raw.githubusercontent.com/schickling/docker-hook/master/docker-ho
#### Start `docker-hook`

```sh
$ docker-hook -t <auth-token> -c <command>
$ docker-hook -t <auth-token> -c <command> -b <branch> -s <true|false>
```

##### Auth-Token
Expand All @@ -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`.
Expand Down
45 changes: 41 additions & 4 deletions docker-hook
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down