What Is Google Functions and How to Use It with Codeship

Integrations

Reading Time: 5 minutes

You may not have heard of Google Functions, since Google has been unusually quiet about its great new Google Cloud service. Even if you have heard about it, you may be a bit confused as to what exactly it is. It took me a bit of time to fully understand it, even though it’s fundamentally very simple.

Google Functions, like its cousin AWS Lambda, represents something of a shift in application development by truly leveraging the increasing power and scope of an integrated cloud services platform, such as Google Cloud.

Google Functions Is All About Events

Want to have something automated happen any time a file is changed or added? You could handle this inside your application, or you could use monitoring or cron jobs. Or you could use Google Functions!

Functions aims to make these single-task, event-based actions much easier to write and maintain by abstracting them out to their own service with easy configuration for triggers, from direct integrations into events across the Google Cloud platform stack (making the power of the combined Google services stack increasingly powerful in aggregate) down to simple webhook-based events.

Writing Functions

Functions are written as Node.js modules, making them super simple to write and contain for the single tasks they serve. Functions can interact with the Google Cloud library and the resources available and can access data from the triggering event.

For instance, here’s an example function from the Google Functions documentation written based on a change to Google Cloud Storage:

/**
 * Background Cloud Function to be triggered by Cloud Storage.
 *
 * @param {object} event The Cloud Functions event.
 * @param {function} callback The callback function.
 */
exports.helloGCS = function (event, callback) {
  const file = event.data;

  if (file.resourceState === 'not_exists') {
    console.log(`File ${file.name} deleted.`);
  } else if (file.metageneration === '1') {
    // metageneration attribute is updated on metadata changes.
    // on create value is 1
    console.log(`File ${file.name} uploaded.`);
  } else {
    console.log(`File ${file.name} metadata updated.`);
  }

  callback();
};

While I won’t get into what is specifically happening here, the main takeaway is that this is simple JavaScript. A complete function can run an important, automated task in just a few lines of code with no internal application dependencies.

For more specific recipes and explanation, Google Functions has surprisingly good documentation for being such a young service.

!Sign up for a free Codeship Account

Deploying Google Functions With Codeship Pro

If you want to start testing out Google Functions, one important step is getting them out into the wild. You may want to combine this with some JavaScript linting or with your larger application or infrastructure deployments, as well.

Fortunately (as you might expect here on the Codeship blog), Codeship makes it easy to deploy Google Functions. We’re going to use Codeship Pro to show you how to do this.

Google’s deployment service

Since Codeship Pro uses Docker images you define to run all of your commands, one of the important parts of any Codeship Pro-based deployment is the Docker image that will be your deployment environment. In this case, that means a container running from an image built with the Google Cloud CLI and authentication setup defined.

Since we know that’s important, we’ve gone ahead and built an image and a starter repo that you can use to jumpstart any Google Cloud deployment.

The containers your commands run in are coordinated via your codeship-services.yml file, so you will need to define a Services file that includes at least one container for your application as well as the Google Cloud deployment service. Here’s an example:

app:
  build:
    image: your-name/your-image
    path: .
    dockerfile: Dockerfile
  encrypted_env_file: env.encrypted
  add_docker: true
  volumes:
    - ./deployment/:/deploy

deployment:
  image: codeship/google-cloud-deployment
  encrypted_env_file: google-auths.env
  add_docker: true
  volumes:
    - ./deployment/:/deploy

For a more complete breakdown on what’s happening, you can read through the codeship-services.yml file documentation. The important part is that the app service will run any code or tests we have, while the deployment service is pulling down the codeship/google-cloud-deployment image we maintain to build a container you can use in your CI/CD pipeline.

Authentication

In the above example, you are building a service named deployment from the codeship/google-cloud-deployment image that we maintain. This service will be the container that actually executes all of your Google Cloud commands. That works because the image provided installs the Google Cloud CLI and is configured to use the Google credentials you pass in as env vars for authentication.

Where the service says encrypted_env_file: google-auths.env, it is referencing a file you will need to create named google-auths.env. This file contains the Google account credentials you will be logging in with, which must be pegged to a Google-side Services Account with appropriate permissions.

Once you have dropped those credentials into a document, formatted as environment variables, you can use Codeship’s local CLI to encrypt them and keep everything secure. This encrypted version is what we are referencing in the line encrypted_env_file: google-auths.env.

Commands

Once you have the necessary services defined in your codeship-services.yml file, you will next need to create the file that passes those services all the commands you are trying to run. This file is called the codeship-steps.yml file.

In this case, we will write a simple example assuming you have Google Functions deployment commands written to a script in your repository, alongside the function module.

- name: google-functions-deployment
  service: deployment
  command: google-deploy.sh

This command is telling the deployment service defined earlier to run the google-deploy.sh script. Inside that script, you will have something similar to:

#!/bin/bash

# Authenticate with the Google Services
codeship_google authenticate

# switch to the directory containing your app.yml (or similar) configuration file
# note that your repository is mounted as a volume to the /deploy directory
cd /deploy/

# deploy the application
gcloud beta functions deploy "${CLOUD_FUNCTION_NAME}" --stage-bucket "${GOOGLE_CLOUD_BUCKET_NAME}" <TRIGGER>

The specifics of your deployment script will depend on what your Google Function is doing. As you can see, Functions has a deploy command in the CLI with required variables depending on the task.

More Google Functions Resources

This is a very high-level look at Google Functions, as well as deploying them via Codeship Pro. There is much more to know, including specifics around writing your functions as well as the deployment scripts you will need to run them. To learn more, we recommend checking out these resources:

Subscribe via Email

Over 60,000 people from companies like Netflix, Apple, Spotify and O'Reilly are reading our articles.
Subscribe to receive a weekly newsletter with articles around Continuous Integration, Docker, and software development best practices.



We promise that we won't spam you. You can unsubscribe any time.

Join the Discussion

Leave us some comments on what you think about this topic or if you like to add something.