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.

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 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 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 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'

Go

steps:
  - name: 'mirror.gcr.io/library/golang'
    args: ['go', 'version']
  - name: 'mirror.gcr.io/library/golang'
    args: ['go', '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 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 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.

  1. Find your project number:

    gcloud projects describe PROJECT_ID
    

    For more information about how to identify your project, see Creating and Managing Projects.

  2. 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
    
  3. 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.

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Functions Documentation