- Lab
- A Cloud Guru
Triggering a Cloud Function with Cloud Build
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.
Path Info
Table of Contents
-
Challenge
Enable APIs
- 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
- From the Google Cloud console's main navigation, visit APIs & Services > Library to enable the following services:
-
Challenge
Create a Google Cloud Source Repository
-
Activate a Cloud Shell.
-
Create a Source Repository:
gcloud source repos create gcpro_repo
-
Clone the repository to the shell:
gcloud source repos clone gcpro_repo
-
Clone repository from GitHub:
git clone https://github.com/linuxacademy/content-gcpro-developer
-
Copy files to shell repo:
cp content-gcpro-developer/cicd-lab/* gcpro_repo
cd gcpro_repo
-
-
Challenge
Move Files to Source Repository
-
Use the following commands to commit the files in the shell repo to the newly created Source Repository:
git remote add google https://source.developers.google.com/p/[PROJECT_ID]/r/gcpro_repo git add . git config --global user.email "[EMAIL_ADDRESS]" git config --global user.name "[NAME]" git commit -m "Initial Commit" git push --all google
-
Visit the Source Repositories dashboard and click
gcpro_repo
to confirm the files.
-
-
Challenge
Create Cloud Function
- From the main navigation, go to Cloud Functions.
- Choose Create function.
- 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
- Leave all other fields with their default values and click Deploy.
- Once the function spins up, click to open it.
- Select the Trigger tab, and click the listed URL. It will open a new window with the default greeting.
- Back in the GCP console, click the Testing tab.
- In the Triggering window, enter
{"name":"<YOUR_NAME>"}
- Click Test the function.
- In the Output box, note the results.
-
Challenge
Set IAM Permissions
- From the navigation, go to Cloud Build > Settings.
- In the Service Account Permissins section, change the status of the Cloud Functions Developer role to Enabled.
- In the Additional steps may be required dialog, choose Grant Access to All Service Accounts.
- Note that the status of the Service Account User role has changed to Enabled.
-
Challenge
Define a Cloud Build Trigger
- From the side navigation of the Cloud Build console, go to Triggers.
- Click Create trigger.
- 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.
- Leave all other fields with their default values, and click Create.
-
Challenge
Verify Trigger
- From the Cloud Build Triggers page, click the Run Trigger option associated with the newly defined trigger.
- Test the trigger by using the Cloud Shell editor to alter the
main.py
file and pushing to the Source Repository. - Visit Cloud Build > History and, if necessary, refresh the page to confirm the new build.
What's a lab?
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.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.