Getting Started with Google Container Builder – Part 6

November 21, 2017

Note: This is the sixth part in a series of posts Getting Started with Google Container Builder


 

Add build notifications using Cloud Functions

In this part we will use Cloud functions to configure notifications for our builds (failed builds). In this example we will send the email notifications through Mailgun.

 

• Create a Google Storage bucket to store the function source code

gsutil mb gs://[BUCKET_NAME]

Image

 

• Create a folder to store the function files

mkdir ~/gcb_email
cd ~/gcb_email

Image

 

• Create the functions files below:

  {
     "MAILGUN_API_KEY":"<your-mailgun-api-key>",
     "MAILGUN_DOMAIN":"<your-mailgun-domain>",
     "MAILGUN_FROM":"<email-from-address>",
     "MAILGUN_TO":"<email-to-address>"
  }
  {
    "name": "google-container-email",
    "version": "0.0.1",
    "description": "Email integration for Google Cloud Container Builder, using Google Cloud Functions",
    "main": "index.js",
    "dependencies": {
      "humanize-duration": "3.10.0",
      "mailgun-js": "~0.11.2"
    },
    "devDependencies": {
      "async": "^2.1.5",
      "mocha": "3.2.0",
      "should": "11.2.1"
    }
  }
const Mailgun = require('mailgun-js');
const humanizeDuration = require('humanize-duration');
const config = require('./config.json');

module.exports.mailgun = new Mailgun({
  apiKey: config.MAILGUN_API_KEY,
  domain: config.MAILGUN_DOMAIN,
});

// subscribe is the main function called by Cloud Functions.
module.exports.subscribe = (event, callback) => {
  const build = module.exports.eventToBuild(event.data.data);

  // Skip if the current status is not in the status list.
  const status = ['FAILURE', 'INTERNAL_ERROR', 'TIMEOUT'];
  if (status.indexOf(build.status) === -1) {
    return callback();
  }

  // Send email.
  const message = module.exports.createEmail(build);
  module.exports.mailgun.messages().send(message, callback);
};
// eventToBuild transforms pubsub event message to a build object.
module.exports.eventToBuild = (data) => {
  return JSON.parse(new Buffer(data, 'base64').toString());
}
// createEmail create an email message from a build object.
module.exports.createEmail = (build) => {
  let duration = humanizeDuration(new Date(build.finishTime) - new Date(build.startTime));
  let content = `Build ${build.id} finished with status ${build.status}` + `\n` + `${build.logUrl}`;
  let message = {
    from: config.MAILGUN_FROM,
    to: config.MAILGUN_TO,
    subject: `Build ${build.id} finished`,
    text: content
  };
  return message
}

Image

 

• Upload the files to the bucket as a cloud function

gcloud beta functions deploy subscribe --stage-bucket gs://[BUCKET_NAME] --trigger-topic cloud-builds

Image

 

• Edit the primes.js test to cause the build to fail (change the number used for the test from 11 to 12)

var sut = require('../primes.js');
describe("when evaluating if a given number is a prime", function(){
  describe("when the number is a prime", function(){
    it("should return 'true'", function(){
      expect(sut.isPrime(12)).toBe(true);
    });
  });
});

 

• Commit and push the changes, the build will failed and you will receive an email notification

Image

 
 

Congratulations, you have already created your own CI/CD process within GCP using Google Container Builder including the creation of a custom builder and automatic email notifications.
For more information about Google Container Builder visit the Official Documentation

 

Add comment
facebook linkedin twitter email

Leave a Reply