CI/CD
Continuous Integration and Deployment (CI/CD) pipelines help to ensure that your functions work both locally and in a test environment on Google Cloud.
Once you finish developing locally, you can configure a CI/CD platform such as Cloud Build to run your existing Cloud Functions tests on an ongoing basis. Continuous testing helps ensure that your code continues to work as intended and that your dependencies remain up-to-date. As Cloud Functions are not updated automatically, you can also configure CI/CD pipelines (including those built on top of Cloud Build) to automatically test and redeploy your functions from a source repository such as GitHub, Bitbucket, or Cloud Source Repositories.
Before you begin
Before starting this guide, set up your environment.
Testing frameworks
Testing frameworks provide an execution environment for automated tests, which allow you to maintain thoroughly-tested production deployments with very little per-deployment effort. Mocking frameworks let you mock external dependencies. An external dependency is a dependency that your function relies on that isn't a part of your function's code.
Node.js
The examples in this guide use Mocha as a test framework to run tests and Sinon as a mocking framework to mock external dependencies.An external dependency is a dependency that your function relies on that isn't a part of your function's code. Common examples of external dependencies are other Google Cloud services and libraries installed using package managers such as npm, PyPI, or Maven.
Python
The examples in this guide use Pytest as a test framework to run tests and unittest as a mocking framework to mock external dependencies.
An external dependency is a dependency that your function relies on that isn't a part of your function's code. Common examples of external dependencies are other Google Cloud services and libraries installed using package managers such as pip.
Go
The examples in this guide use the standard librarytesting
package to run tests. However,
the system tests have external dependencies.
An external dependency is a dependency that your function relies on that isn't a part of your function's code. Common examples of external dependencies are other Google Cloud services and other packages you've downloaded.
Java
The examples in this guide use the JUnit library to run tests, and Mockito as a mocking framework to mock external dependencies.An external dependency is a dependency that your function relies on that isn't a part of your function's code.
Setting up Cloud Build
Follow the instructions in the
Automating Builds using Build Triggers
guide using the cloudbuild.yaml
build config file below to configure
Cloud Build to automatically test and deploy your function.
Node.js
Python
Go
Java
Replace:
[YOUR_DEPLOYED_FUNCTION_NAME]
with the name of your Cloud Functions to be used in the Google Cloud console, Google Cloud CLI, and URL.[YOUR_FUNCTION_TRIGGER]
with the appropriate trigger value such as--trigger-http
.[YOUR_RUNTIME]
with the runtime identifier such asnodejs10
.[YOUR_FUNCTION_NAME_IN_CODE]
with the function name as it appears in code.--entry-point
is only needed if this value is different than[YOUR_DEPLOYED_FUNCTION_NAME]
.function/dir/from/repo/root
with the path to the directory containing your function and its dependency listing(s).
Granting permissions to run builds and deployments
If you are using Cloud Build to deploy Cloud Functions, you
need to grant permissions to the
Cloud Build service account
(PROJECT_NUMBER@cloudbuild.gserviceaccount.com
).
The Cloud Build service account is used to execute builds on your
behalf.
Find your project number:
gcloud projects describe PROJECT_ID
For more information about how to identify your project, see Creating and Managing Projects.
Allow the Cloud Build service account to act as the Cloud Functions Runtime service account:
gcloud iam service-accounts add-iam-policy-binding PROJECT_ID@appspot.gserviceaccount.com \ --member serviceAccount:PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role roles/iam.serviceAccountUser
Assign the Cloud Functions Developer role to the Cloud Build service account, which allows Cloud Build to deploy Cloud Functions:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member serviceAccount:PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role roles/cloudfunctions.developer
For more information, see Cloud Functions IAM Roles.
Now whenever you push to the repository with
Cloud Build enabled,
the steps in the cloudbuild.yaml
file will be run.