Skip to content

Instantly share code, notes, and snippets.

@todkap
Last active August 29, 2018 15:05
Show Gist options
  • Save todkap/571b4a46325bbe9eb4a756dfc40aef7c to your computer and use it in GitHub Desktop.
Save todkap/571b4a46325bbe9eb4a756dfc40aef7c to your computer and use it in GitHub Desktop.
Node application representing playground deployer process flow
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const oauth_token = process.env.OAUTH_TOKEN;
const git_project = 'BlockchainInnovation'
const
const octokit = require('@octokit/rest')({
timeout: 0, // 0 means no request timeout
headers: {
accept: 'application/vnd.github.v3+json',
'user-agent': 'octokit/rest.js v1.2.3' // v1.2.3 will be current version
},
// custom GitHub Enterprise URL
baseUrl: 'https://github.ibm.com/api/v3',
// Node only: advanced request options can be passed as http(s) agent
agent: undefined
});
//token (https://github.com/settings/tokens)
octokit.authenticate({
type: 'oauth',
token: oauth_token
});
async function triggerDeployment(a) {
const webhookPayload = JSON.parse(a.payload);
const repoName = webhookPayload.repository.name
const commitTrigger = webhookPayload.commit
const masterBranch = await octokit.repos.getBranch({
owner: 'BlockchainInnovation',
repo: 'playground-wallet-deploy',
branch: "master"
})
const masterBranchSha = masterBranch.data.commit.sha;
const branchedFromTemplate = await octokit.gitdata.createReference({
owner: "BlockchainInnovation",
repo: "playground-wallet-deploy",
ref: "refs/heads/scratch-" + commitTrigger,
sha: masterBranchSha
})
const walletDeploymentTemplate = await octokit.repos.getContent({
owner: 'BlockchainInnovation',
repo: 'playground-wallet-deploy',
path: 'kubernetes/nodejs/wallet-deployment.yaml.tpl',
ref: "refs/heads/scratch-" + commitTrigger
})
const walletDeployment = await octokit.repos.getContent({
owner: 'BlockchainInnovation',
repo: 'playground-wallet-deploy',
path: 'kubernetes/nodejs/wallet-deployment.yaml',
ref: "refs/heads/scratch-" + commitTrigger
})
// content will be base64 encoded
const content = Buffer.from(walletDeploymentTemplate.data.content, 'base64').toString()
var re = /{IMAGE_VERSION}/g;
var updatedContent = content.replace(re, commitTrigger);
const updatedFile = await octokit.repos.updateFile({
owner: "BlockchainInnovation",
repo: "playground-wallet-deploy",
path: 'kubernetes/nodejs/wallet-deployment.yaml',
message: "Triggered deployment as result of commit on project: " + repoName + ":" + commitTrigger,
content: Buffer.from(updatedContent).toString('base64'),
sha: walletDeployment.data.sha,
branch: "refs/heads/scratch-" + commitTrigger
})
const mergeUpdate = await octokit.repos.merge({
owner: "BlockchainInnovation",
repo: "playground-wallet-deploy",
base: "master",
head: "scratch-" + commitTrigger,
commit_message: "Triggered deployment as result of commit on project: " + repoName + ":" + commitTrigger
})
const branchDeleted = await octokit.gitdata.deleteReference({
owner: "BlockchainInnovation",
repo: "playground-wallet-deploy",
ref: "heads/scratch-" + commitTrigger
})
}
app.use(bodyParser.urlencoded({
extended: true
}));
// Route that receives a POST request to the webhook endpoint
app.post('/', function(req, res) {
res.set('Content-Type', 'text/plain')
res.status(201).send('Triggering deployment');
triggerDeployment(req.body);
})
// Route that receives a GET request to the webhook endpoint
app.get('/', function(req, res) {
res.send("Testing")
})
// Tell our app to listen on port 3000
app.listen(3000, function(err) {
if (err) {
throw err
}
console.log('Server started on port 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment