Skip to content

Instantly share code, notes, and snippets.

@ljvmiranda921
Created December 18, 2018 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljvmiranda921/419e7c078d98069e8fc145d6cf0b540c to your computer and use it in GitHub Desktop.
Save ljvmiranda921/419e7c078d98069e8fc145d6cf0b540c to your computer and use it in GitHub Desktop.
Cloud Function template for creating status badge indicators
const { Storage } = require("@google-cloud/storage");
/**
* Auto-generated from cloud-build-badge. To deploy this cloud function, execute
* the following command:
* gcloud functions deploy christmAIs \
* --runtime nodejs6 \
* --trigger-resource cloud-builds \
* --trigger-event google.pubsub.topic.publish
*
* @param {object} event Google Cloud Functions event
* @param {function} callback callback function for handling events
*/
exports.myFunction = (event, callback) => {
const pubsubMessage = event.data;
if (pubsubMessage.data) {
buildResource = JSON.parse(
Buffer.from(pubsubMessage.data, "base64").toString()
);
repo = buildResource.substitutions.REPO_NAME === "myRepo";
repoName = buildResource.substitutions.REPO_NAME;
branch = buildResource.substitutions.BRANCH_NAME;
status = buildResource.status;
if (["master"].includes(branch)) {
console.log("Creating badge for %s on branch %s", repoName, branch);
const filename = "build/myRepo-" + branch + ".svg";
console.log("Filename will be %s", filename);
const storage = new Storage();
if (repo && branch && status == "SUCCESS") {
console.log("Detected build success!");
storage
.bucket("my-bucket")
.file("build/success.svg")
.copy(storage.bucket("my-bucket").file(filename));
console.log("Switched badge to build success");
storage
.bucket("my-bucket")
.file(filename)
.makePublic(function(err, apiResponse) {});
console.log("Badge set to public");
}
if (repo && branch && status == "FAILURE") {
console.log("Detected build failure!");
storage
.bucket("my-bucket")
.file("build/failure.svg")
.copy(storage.bucket("my-bucket").file(filename));
console.log("Switched badge to build failure");
storage
.bucket("my-bucket")
.file(filename)
.makePublic(function(err, apiResponse) {});
console.log("Badge set to public");
}
}
}
callback();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment