CI/CD

Continuous Integration and Deployment (CI/CD) pipelines help to ensure that your functions work both locally and in a test environment on GCP.

This guide explains how to set up a CI/CD pipeline with Cloud Build to run your existing Cloud Functions tests on an ongoing basis.

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 Platform 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 Platform services and libraries installed using package managers such as pip.

Go

The examples in this guide use the standard library testing 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 Platform services and other packages you've downloaded.

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

steps:
- name: 'gcr.io/cloud-builders/yarn'
  args: ['install']
  dir: 'functions/autodeploy'
- name: 'gcr.io/cloud-builders/npm'
  args: ['test']
  dir: 'functions/autodeploy'
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['functions', 'deploy', '[YOUR_DEPLOYED_FUNCTION_NAME]', '[YOUR_FUNCTION_TRIGGER]', '--runtime', '[YOUR_RUNTIME]', '--entry-point', '[YOUR_FUNCTION_NAME_IN_CODE]']
  dir: 'functions/autodeploy'

Replace:

  • [YOUR_FUNCTION_NAME] with the name of your Cloud Functions to be used in the Google Cloud Platform Console, Cloud SDK, and URL.
  • [YOUR_FUNCTION_TRIGGER] with the appropriate trigger value such as --trigger-http.
  • [YOUR_RUNTIME] with the runtime identifier such as nodejs10.
  • [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_FUNCTION_NAME].

If Cloud Build doesn't suit your needs, see this list of continuous integration platforms.

Granting permissions to run builds and deployments

If you are using Cloud Build, you need to grant permissions to the Cloud Build service account.

For example:

  • To deploy Cloud Functions, you might want to assign the Cloud Functions Developer role to the Cloud Build service account (PROJECT_NUMBER@cloudbuild.gserviceaccount.com).
  • If you use the Cloud Functions Developer role, you also need to grant the Cloud Functions Runtime service account (PROJECT_ID@appspot.gserviceaccount.com) the IAM Service Account User role.

Now whenever you push to the repository with Cloud Build enabled, the steps in the cloudbuild.yaml will be run.

Hai trovato utile questa pagina? Facci sapere cosa ne pensi:

Invia feedback per...

Cloud Functions Documentation