Created
May 27, 2017 23:39
-
-
Save jpadilla/e2476e295bbd53bbeeab4f8c0ef7b44a to your computer and use it in GitHub Desktop.
Update K8S Deployment after image pushed to Docker Hub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "k8s-dockerhub-build-webhook", | |
"version": "1.0.0", | |
"description": "Update K8S Deployment after image pushed to Docker Hub", | |
"main": "server.js", | |
"engines": { | |
"node": "7.10.0" | |
}, | |
"dependencies": { | |
"body-parser": "^1.17.2", | |
"express": "^4.15.3", | |
"kubernetes-client": "^3.10.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const Api = require('kubernetes-client'); | |
// Settings | |
const DEPLOYMENT_NAME = '---YOUR-KUBERNETES-DEPLOYMENT-NAME---'; | |
const CONTAINER_NAME = '--YOUR-CONTAINER-NAME---'; | |
const IMAGE_NAME = '---YOUR-IMAGE-NAME--'; | |
const PORT = process.env.PORT || 3000; | |
const SECRET_KEY = process.env.SECRET_KEY; | |
const K8S_URL = process.env.K8S_URL; | |
const K8S_USERNAME = process.env.K8S_USERNAME; | |
const K8S_PASSWORD = process.env.K8S_PASSWORD; | |
const SETTINGS = { | |
SECRET_KEY, K8S_URL, K8S_USERNAME, K8S_PASSWORD | |
}; | |
Object.keys(SETTINGS) | |
.forEach((setting) => { | |
if (!SETTINGS[setting]) { | |
throw new Error(`Missing required setting: ${setting}`); | |
} | |
}); | |
const app = express(); | |
// Middlewares | |
app.use(bodyParser.json()); | |
function updateDeployments(tag) { | |
const ext = new Api.Extensions({ | |
url: K8S_URL, | |
namespace: 'default', | |
version: 'v1beta1', | |
auth: { | |
user: K8S_USERNAME, | |
pass: K8S_PASSWORD | |
} | |
}); | |
const patch = { | |
spec: { | |
template: { | |
spec: { | |
containers: [{ | |
name: CONTAINER_NAME, | |
image: `${IMAGE_NAME}:${tag}` | |
}] | |
} | |
} | |
} | |
}; | |
const options = { body: patch }; | |
return new Promise((resolve, reject) => { | |
const deployment = ext.namespaces.deployments(DEPLOYMENT_NAME); | |
deployment.patch(options, (err, result) => { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(result); | |
}); | |
}); | |
} | |
app.post('/webhook', async function(req, res) { | |
if (req.query.secret !== SECRET_KEY) { | |
return res.status(401).json({ | |
ok: false, | |
error: 'Unauthorized' | |
}); | |
} | |
const body = req.body || {}; | |
const pushData = body.push_data || {}; | |
const tag = pushData.tag; | |
if (!tag) { | |
return res.status(400).json({ | |
ok: false, | |
error: 'Invalid tag' | |
}); | |
} | |
try { | |
await updateDeployments(tag); | |
} catch (err) { | |
return res.status(500).json({ | |
ok: false, | |
error: `${err}` | |
}); | |
} | |
return res.json({ ok: true }); | |
}); | |
app.listen(PORT, function() { | |
console.log(`Listening on port ${PORT}...`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment