Skip to content

Instantly share code, notes, and snippets.

@galenweber
Last active November 13, 2018 19:25
Show Gist options
  • Save galenweber/f610788ad239942e2d4f1b36209a94af to your computer and use it in GitHub Desktop.
Save galenweber/f610788ad239942e2d4f1b36209a94af to your computer and use it in GitHub Desktop.
Simple Google Cloud Function to trigger a Circle job when called
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
const https = require('https');
exports.triggerCircle = (req, res) => {
const VCS = 'github';
const ORG = 'atticuslabs';
const REPO = 'consumer-web';
const BRANCH = 'develop';
const CIRCLE_TOKEN = 'CIRCLE_TOKEN_OMITTED';
let CIRCLE_JOB = 'cypress-staging';
// Heroku constants
const HEROKU_PROD_APP_NAME = 'consumer-web-prod';
const { data } = req.body;
if (!data || data.status !== 'succeeded') {
res.status(400).send("no successful build detected");
return;
}
if (
data.app
&& (data.app.name === HEROKU_PROD_APP_NAME)
) {
CIRCLE_JOB = 'cypress-prod';
}
const options = {
hostname: 'circleci.com',
path: `/api/v1.1/project/${VCS}/${ORG}/${REPO}/tree/${BRANCH}?circle-token=${CIRCLE_TOKEN}`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
}
const postBody = JSON.stringify({
build_parameters: {
CIRCLE_JOB: CIRCLE_JOB
}
});
const request = https.request(options, (response) => {
res.status(200).json(`status code: ${response.statusCode}`);
});
request.on('error', (e) => {
res.status(400).json(e);
});
request.write(postBody);
request.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment