forked from iaincollins/docker-deploy-webhook
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
93 lines (79 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* A service for automated deployment from Docker Hub to Docker Swarm
* https://docs.docker.com/docker-hub/webhooks/
*/
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const config = require('./lib/config')
const docker = require('./lib/docker')
const logger = require('./lib/logger')('DOCKER-DEPLOY')
const notify = require('./lib/notify')
const services = require(`./config/config.json`)[config.whichConfig]
const { port, token } = config
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/webhook/:token', (req, res) => {
if (!checkToken(req.params.token, res)) {
return
}
// Send response back right away if token was valid
res.send('OK')
const payload = req.body
const imageName = `${payload.repository.repo_name}:${payload.push_data.tag}`
if (!services[imageName]) {
logger.log(`Received updated for "${imageName}" but not configured to handle updates for this image.`)
return
}
const image = services[imageName]
const service = image.service
if (image.options.pullButDontDeploy) {
docker.pull(imageName)
.then((msg) => {
notify('pull', true, msg, image)
})
.catch((err) => {
logger.error(err)
notify('pull', false, err, image)
})
return
}
docker.deploy(imageName, service)
.then((msg) => {
notify('pull', true, msg, image)
})
.catch((err) => {
logger.error(err)
notify('pull', false, err, image)
})
})
// default route
app.all('*', (req, res) => {
res.send('')
})
// start webserver
if (config.sslCert && config.sslKey) {
// start server with HTTPS only if SSL key & cert have been provided
const https = require('https')
const options = {
key: config.sslKey,
cert: config.sslCert
}
https.createServer(options, app).listen(port, serverStartCallback)
} else {
// start HTTP server
app.listen(port, serverStartCallback)
}
function serverStartCallback(err) {
if (err) throw new Error(`Couldn't start server: ${err}`)
const protocol = (config.sslCert && config.sslKey) ? 'https' : 'http'
logger.log(`Listening for webhooks on ${protocol}://localhost:${port}/webhook/${token}`)
}
function checkToken(tokenSent, res) {
if (tokenSent !== token) {
logger.log('Endpoint called with invalid or missing token.')
res.status(401).send('Access Denied: Token Invalid\n').end()
return false
}
return true
}