Exporting a CircleCI Workflow
In this tutorial, you will pull an example package that declares Kubernetes resources and two kpt functions. Then you will export a workflow that runs the functions against the resources on CircleCI and merge it manually to your existing pipeline. This tutorial takes about 10 minutes.
A kpt version
v0.32.0 or higher is required.
Before you begin
Unfamiliar with CircleCI? Read Getting Started Introduction first.
Before diving into the following tutorial, you need to create a public repo on GitHub if you don’t have one yet, e.g.
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.
kpt pkg get to fetch source files of this tutorial:
# Init git git init git remote add origin https://github.com/<USER>/<REPO>.git # Fetch source files kpt pkg get https://github.com/GoogleContainerTools/kpt/package-examples/function-export example-package
Then you will get an
resources/resources.yaml: declares a
resources/constraints/: declares constraints used by the
functions.yaml: runs two functions declaratively:
gatekeeper-validateenforces constraints over all resources.
label-namespaceadds a label to all Namespaces.
Exporting a pipeline
kpt fn export example-package --workflow circleci --output config.yml
Running this command will get a
config.yml like this:
version: "2.1" orbs: kpt: executors: kpt-container: docker: - image: gcr.io/kpt-dev/kpt:latest commands: kpt-fn-run: steps: - run: kpt fn run example-package jobs: run-functions: executor: kpt-container steps: - setup_remote_docker - kpt-fn-run workflows: main: jobs: - kpt/run-functions
Integrating with your existing pipeline
To merge the exported file with your existing pipeline, you can:
- Copy and paste the
- Insert a
checkoutstep as the first step in the
- If you want to see the diff after running kpt functions, append a
run: git -no-pager diffstep in the
kpt/run-functionsto your workflow jobs.
Your final workflow may looks like this:
version: "2.1" orbs: kpt: executors: kpt-container: docker: - image: gcr.io/kpt-dev/kpt:latest commands: kpt-fn-run: steps: - run: kpt fn run example-package - run: git --no-pager diff jobs: run-functions: executor: kpt-container steps: - checkout - setup_remote_docker - kpt-fn-run workflows: main: jobs: - kpt/run-functions
If you don’t have one yet, you can do the following steps:
- Copy the exported
.circleci/config.ymlin your project root.
- Do the steps above to make the pipeline fully functional.
Once all changes are pushed into GitHub, you can do the following steps to setting up your project on CircleCI:
- Log into CircleCI and choose
Log In with GitHub.
- Select your own account as an organization if prompted.
- Choose your newly created repo and click
Set Up Project.
Use Existing Configsince you have already added
Viewing the result on CircleCI
git add . git commit -am 'Init pipeline' git push --set-upstream origin master
Once local changes have been pushed, you can see a latest build running on CircleCI like this:
Try to remove the
owner: alice line in
Once local changes are pushed, you can see how the pipeline fails on CircleCI.