Skip to content
This repository was archived by the owner on Jun 21, 2019. It is now read-only.

Commit b37f77d

Browse files
committed
barebones webhook for docker hub automated builds
1 parent ebbcdb6 commit b37f77d

File tree

8 files changed

+146
-25
lines changed

8 files changed

+146
-25
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "semistandard"
3+
}

.gitignore

-25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
1-
# Logs
2-
logs
3-
*.log
41
npm-debug.log*
5-
6-
# Runtime data
7-
pids
8-
*.pid
9-
*.seed
10-
11-
# Directory for instrumented libs generated by jscoverage/JSCover
12-
lib-cov
13-
14-
# Coverage directory used by tools like istanbul
15-
coverage
16-
17-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18-
.grunt
19-
20-
# node-waf configuration
21-
.lock-wscript
22-
23-
# Compiled binary addons (http://nodejs.org/api/addons.html)
24-
build/Release
25-
26-
# Dependency directory
272
node_modules
283

294
# Optional npm cache directory

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v4.3.1

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: node_js

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# docker-hub-hook
22
Webhook for Docker Hub Automated Builds
3+
4+
## Environment Variables
5+
6+
- **DHH_PORT** - port for HTTP server to listen on
7+
- **DHH_CMD** - command to execute
8+
- **DHH_CWD** - current working directory for command
9+
- **DHH_TOKEN** - secret for simple auth
10+
- **DHH_REPO** - repository name on Docker Hub
11+
- **DHH_TAG** - tag for repository on Docker Hub
12+
13+
```
14+
DHH_PORT=4321 DHH_CMD='ls -al' DHH_CWD='/home/' DHH_TOKEN=abc123 DHH_TAG node index.js
15+
```
16+
17+
```
18+
curl -H "Content-Type: application/json" -X POST -d '{"push_data": { "tag": "master" }, "repository": { "repo_name": "jsperf/jsperf.com"}, "callback_url": "https://www.google.com/"}' http://127.0.0.1:4321/hook?token=abc123
19+
```

index.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const http = require('http');
2+
const https = require('https');
3+
const url = require('url');
4+
const cp = require('child_process');
5+
6+
function reply (res, code) {
7+
res.writeHead(code);
8+
res.end(http.STATUS_CODES[code]);
9+
}
10+
11+
function validate (u, state, cb) {
12+
console.log('validating webhook...');
13+
const cbUrl = url.parse(u);
14+
15+
const req = https.request({
16+
method: 'POST',
17+
hostname: cbUrl.hostname,
18+
path: cbUrl.path
19+
}, (res) => {
20+
cb(null, res.statusCode);
21+
});
22+
23+
req.on('error', cb);
24+
25+
req.write(JSON.stringify({ state: state }));
26+
req.end();
27+
}
28+
29+
http.createServer(function (req, res) {
30+
if (req.method === 'POST') {
31+
const parsedUrl = url.parse(req.url, true);
32+
33+
if (parsedUrl.pathname === '/hook' && parsedUrl.query.token === process.env.DHH_TOKEN) {
34+
var body = '';
35+
36+
req.on('data', (chunk) => {
37+
body += chunk;
38+
});
39+
40+
req.on('end', () => {
41+
try {
42+
const payload = JSON.parse(body);
43+
if (payload.repository.repo_name === process.env.DHH_REPO && payload.push_data.tag === process.env.DHH_TAG) {
44+
console.log('executing command...');
45+
cp.exec(process.env.DHH_CMD, {
46+
cwd: process.env.DHH_CWD
47+
}, function (err, stdout, stderr) {
48+
var state = 'success';
49+
var code = 200;
50+
51+
if (err) {
52+
console.error(err);
53+
state = 'failure';
54+
code = 500;
55+
} else {
56+
console.log(stdout);
57+
console.log(stderr);
58+
}
59+
60+
validate(payload.callback_url, state, (err, statusCode) => {
61+
if (err) {
62+
console.error(err);
63+
}
64+
console.log(`webhook callback response: ${statusCode}`);
65+
reply(res, code);
66+
});
67+
});
68+
} else {
69+
throw new Error('Mismatched repo or tag in payload');
70+
}
71+
} catch (e) {
72+
console.error(e);
73+
reply(res, 400);
74+
}
75+
});
76+
} else {
77+
console.log(`Ignoring ${req.method} ${req.url}`);
78+
reply(res, 404);
79+
}
80+
} else {
81+
console.log(`Ignoring ${req.method} ${req.url}`);
82+
reply(res, 404);
83+
}
84+
}).listen(process.env.DHH_PORT, () => {
85+
console.log('Server listening...');
86+
});

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "docker-hub-hook",
3+
"version": "1.0.0",
4+
"description": "Webhook for Docker Hub Automated Builds",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "eslint ."
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/jsperf/docker-hub-hook.git"
12+
},
13+
"keywords": [
14+
"jsperf",
15+
"docker"
16+
],
17+
"author": "Max Beatty",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/jsperf/docker-hub-hook/issues"
21+
},
22+
"homepage": "https://github.com/jsperf/docker-hub-hook#readme",
23+
"devDependencies": {
24+
"eslint": "1.10.3",
25+
"eslint-config-semistandard": "5.0.0",
26+
"eslint-config-standard": "4.4.0",
27+
"eslint-plugin-standard": "1.3.2"
28+
}
29+
}

0 commit comments

Comments
 (0)