Developers & Practitioners

Deploying to serverless platforms with GitHub Actions

Hero

Join us on December 16, 2020, 11am PT / 2pm ET to learn more about Automating CI/CD pipelines with GitHub Actions and Google Cloud.

Serverless computing hides away infrastructure allowing for developers to focus on building great applications. Google Cloud Platform offers 3 serverless compute platforms—Cloud Functions, App Engine, and Cloud Run—with the benefits of zero server management, no up-front provisioning, auto-scaling, and only paying for the resources used. 

Serverless applications are quick and easy to spin up, but a system for continuous integration and continuous delivery (CI/CD) is key for long-term operability. However, CI/CD systems tend to be known for their complexity, so GitHub Actions aim to reduce the overhead by abstracting away the test infrastructure and creating a developer-centric CI/CD system. You can get started quickly by adding a configuration file to your repo to automate your builds, testing, and deployments. Google wants to meet you on GitHub and provides Google Cloud Platform integrated GitHub actions.

Let’s walk through how to deploy to Google Cloud Platform's serverless options using the integrated GitHub actions. Learn more about Google’s serverless hosting options or Google Cloud Platform full range of hosting options to find which platform is right for you.

Cloud Functions

Cloud Functions is Google Cloud’s Function-as-a-Service platform that allows users to create single-purpose, stand-alone functions that respond to events and HTTP requests. Cloud Functions are great for pieces of code like sending notification emails, performing database sanitization and maintenance, integrating with webhooks, and processing background tasks.

Use the google-github-actions/deploy-cloud-functions to deploy an HTTP function by specifying the function name and runtime:

  - name: Deploy a Cloud Function
  id: deploy-function
  uses: google-github-actions/deploy-cloud-functions@v0.1.0
  with:
    name: my-function
    runtime: nodejs10
    project: ${{ secrets.gcp_project }}
    credentials: ${{ secrets.gcp_credentials }}

Or deploy a background function that can be triggered by events, such as Pub/Sub messages, Firebase events, or changes in a Cloud Storage bucket, by specifying trigger type and resource:

  - name: Deploy a Background Cloud Function
  id: deploy-background-function
  uses: google-github-actions/deploy-cloud-functions@v0.1.0
  with:
    name: my-background-function
    runtime: nodejs10
    credentials: ${{ secrets.gcp_credentials }}
    event_trigger_type: providers/cloud.pubsub/eventTypes/topic.publish
    event_trigger_resource: projects/${{ secrets.gcp_project }}/topics/my-pubsub-topic

A function triggered when a new object is created in Cloud Storage would be deployed with inputs like:

  event_trigger_type: providers/cloud.storage/eventTypes/object.finalize
event_trigger_resource: projects/_/buckets/my-bucket/objects/*

Learn more about specifying event triggers.

App Engine

App Engine is the original serverless platform for hosting modern web applications. App Engine allows you to deploy from source by selecting a runtime from a set of popular programming languages.

Deploy your source code with the google-github-actions/deploy-appengine action by specifying a service account key with permissions to deploy and the path to App Engine app's settings file, app.yaml which sits next to your application to define the service for the deployment.

  - name: Deploy an App Engine app
  id: deploy-app
  uses: google-github-actions/deploy-appengine@v0.1.0
  with:
    project: ${{ secrets.gcp_project }}
    deliverables: path/to/app.yaml
    credentials: ${{ secrets.gcp_credentials }}

The project ID is also set to ensure deployment to the correct project, since service accounts can be given permissions to separate projects.

Cloud Run

Cloud Run hosts stateless containers with any choice of language, library, or binary. Cloud Run is a great choice for REST API backends, lightweight data processing, and automated services like webhooks and scheduled tasks.

Use the google-github-actions/deploy-cloudrun action to deploy a container image hosted in Google Container Registry or Artifact Registry to Cloud Run to create a new service or a revision.

  - name: Deploy a Cloud Run service
  id: deploy-service
  uses: google-github-actions/deploy-cloudrun@v0.1.0
  with:
    image: gcr.io/cloudrun/hello
    service: hello-cloud-run
    project: ${{ secrets.gcp_project }}
    credentials: ${{ secrets.gcp_credentials }}

Need to build your image too? Use the built-in Docker tool to build and push your image or utilize the setup-gcloud action (below) to submit a Cloud Build using the gcloud CLI tool.

Setup Cloud SDK

The Cloud SDK, also known as the gcloud CLI tool, is commonly used to interact with Google Cloud Platform. Use the setup-gcloud action to add the gcloud CLI tool to the path to interact with many Google Cloud services. This action can be used to authenticate other Google Cloud Platform actions by utilizing export_default_credentials. This exports the path to Default Application Credentials as the environment variable GOOGLE_APPLICATION_CREDENTIALS for services to be automatically authenticated in later steps. The credentials input in the other actions, then can be omitted.

  name: Set up Cloud SDK
  uses: google-github-actions/setup-gcloud@v0.2.0
  with:
    project_id: ${{ secrets.gcp_project }}
    service_account_key: ${{ secrets.gcp_credentials }}
    export_default_credentials: true

Try it for yourself!

Google and GitHub are excited to make it easier for you to set up CI/CD pipelines. Try Google Cloud GitHub actions today:

Join us on December 16, 2020, 11am PT / 2pm ET to learn more about Automating CI/CD pipelines with GitHub Actions and Google Cloud. Save your seat!