Triggering a Cloud Function with Cloud Build

45 minutes
  • 7 Learning Objectives

About this Hands-on Lab

Many organizations have adopted a CI/CD (continuous integration/continuous delivery) pipeline to maintain coding efficiency. Cloud Functions work well in a CI/CD environment and can be implemented as such through a Google Cloud service, Cloud Build. In this hands-on lab, you’ll gain the practical experience of setting up the Cloud Source Repository, Cloud Functions, and Cloud Build services to implement a CI/CD pipeline.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable APIs
  1. From the Google Cloud console’s main navigation, visit APIs & Services > Library to enable the following services:
    • Cloud Functions
    • Source Repositories
    • Cloud Build
    • Cloud Resource Manager API
Create a Google Cloud Source Repository
  1. Activate a Cloud Shell.

  2. Create a Source Repository:

    gcloud source repos create gcpro_repo
  3. Clone the repository to the shell:

    gcloud source repos clone gcpro_repo
  4. Clone repository from GitHub:

    git clone
  5. Copy files to shell repo:

    cp content-gcpro-developer/cicd-lab/* gcpro_repo
    cd gcpro_repo
Move Files to Source Repository
  1. Use the following commands to commit the files in the shell repo to the newly created Source Repository:

    git remote add google[PROJECT_ID]/r/gcpro_repo
    git add .
    git config --global "[EMAIL_ADDRESS]"
    git config --global "[NAME]"
    git commit -m "Initial Commit"
    git push --all google
  2. Visit the Source Repositories dashboard and click gcpro_repo to confirm the files.

Create Cloud Function
  1. From the main navigation, go to Cloud Functions.
  2. Choose Create function.
  3. Configure your function with the following values:
    • Environment: 1st gen
    • Name: repo-function-1
    • Trigger: HTTP
    • Authentication: Allow unauthenticated invocations
    • Click Save
    • Under Runtime, build, connections and security settings:
      • Maximum number of instances: 1
    • Click Next
    • Source Code: Cloud Source repository
    • Runtime: Python 3.7
    • Repository: gcpro_repo
    • Entry point: greetings_http
  4. Leave all other fields with their default values and click Deploy.
  5. Once the function spins up, click to open it.
  6. Select the Trigger tab, and click the listed URL. It will open a new window with the default greeting.
  7. Back in the GCP console, click the Testing tab.
  8. In the Triggering window, enter
  9. Click Test the function.
  10. In the Output box, note the results.
Set IAM Permissions
  1. From the navigation, go to Cloud Build > Settings.
  2. In the Service Account Permissins section, change the status of the Cloud Functions Developer role to Enabled.
  3. In the Additional steps may be required dialog, choose Grant Access to All Service Accounts.
  4. Note that the status of the Service Account User role has changed to Enabled.
Define a Cloud Build Trigger
  1. From the side navigation of the Cloud Build console, go to Triggers.
  2. Click Create trigger.
  3. Set the following values:
    • Repository: gcpro_repo.
    • Name: trigger-1
    • Build configuration: Cloud Build configuration file (yaml or json)
      • Make sure Cloud Build configuration file location autopopulates with / cloudbuild.yaml.
  4. Leave all other fields with their default values, and click Create.
Verify Trigger
  1. From the Cloud Build Triggers page, click the Run Trigger option associated with the newly defined trigger.
  2. Test the trigger by using the Cloud Shell editor to alter the file and pushing to the Source Repository.
  3. Visit Cloud Build > History and, if necessary, refresh the page to confirm the new build.

Additional Resources

Your team is using Cloud Functions as a vital part of their new app. You’ve been tasked to incorporate a test function into a CI/CD pipeline. To achieve this goal, you’ll need to be familiar with a variety of Google Cloud services, including Cloud Functions, Source Repositories, IAM, and Cloud Build.

To accomplish this task, you’ll need to complete the following steps:

  1. Enable necessary APIs.
  2. Create a Google Cloud Source Repository.
  3. Move files into the repository.
  4. Create a Cloud Function.
  5. Set IAM permissions for Cloud Build to access Cloud Functions.
  6. Create a Cloud Build trigger.
  7. Test the trigger.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?