Exporting a Cloud Build Pipeline

Export a Cloud Build config file that runs kpt functions

In this tutorial, you will pull an example package that declares Kubernetes resources and two kpt functions. Then you will export a pipeline that runs the functions against the resources on Cloud Build. This tutorial takes about 5 minutes.

A kpt version v0.32.0 or higher is required.

Before you begin

Unfamiliar with Cloud Build? Here is Cloud Build Quickstarts.

On your local machine, create an empty directory:

mkdir function-export-example
cd function-export-example

All commands must be run at the root of this directory.

Use kpt pkg get to fetch source files of this tutorial:

# Fetch source files
kpt pkg get https://github.com/GoogleContainerTools/kpt/package-examples/function-export example-package

Then you will get an example-package directory:

  • resources/resources.yaml: declares a Deployment and a Namespace.
  • resources/constraints/: declares constraints used by the gatekeeper-validate function.
  • functions.yaml: runs two functions declaratively:
    • gatekeeper-validate enforces constraints over all resources.
    • label-namespace adds a label to all Namespaces.

Exporting a pipeline

kpt fn export example-package --workflow cloud-build --output cloudbuild.yaml

Running this command will generate a cloudbuild.yaml like this:

  - name: gcr.io/kpt-dev/kpt:latest
      - fn
      - run
      - exmaple-package

Integrating with your existing pipeline

Now you can manually copy and paste the generated content into your existing build config file.

If you do not have one yet, you can simply put the file in the root of your project. It is fully functional.

If you want to see the diff after running kpt functions, append a kpt pkg diff step to make your cloudbuild.yaml look like this:

  - name: gcr.io/kpt-dev/kpt:latest
      - fn
      - run
      - example-package
  - name: gcr.io/kpt-dev/kpt:latest
      - pkg
      - diff
      - example-package
      - --diff-tool
      - git
      - --diff-tool-opts
      - "--no-pager diff"

Viewing the result on Cloud Build

Running this command will trigger a build:

gcloud builds submit .

Then you can view the result on Build History.

Next step

Try to remove the owner: alice line in example-package/resources/resources.yaml.

Submit again, then view how the pipeline fails on Cloud Build.

Last modified November 9, 2020: docs: consumer guide nits and fixes. (0afc440d)