LogoLogo
Bonsai (0.13) DocsGitHubDiscord CommunityGarden Enterprise
Edge Release
Edge Release
  • Welcome to Garden!
  • Overview
    • What is Garden
    • Use Cases
      • Isolated On-Demand Preview Environments
      • Fast, Portable CI Pipelines that Run Anywhere
      • Shift Testing Left
      • Local Development With Remote Clusters
      • Jumpstart your Internal Developer Platform
    • Garden vs Other Tools
  • Getting Started
    • Quickstart
    • Garden Basics
    • Next Steps
  • Tutorials
    • Your First Project
      • 1. Create a Garden Project
      • 2. Pick a Kubernetes Plugin
      • 3. Add Actions
      • 4. Add Tests
      • 5. Code Syncing (Hot Reload)
      • 6. Next Steps
    • Setting up a Kubernetes cluster
      • 1. Create a Cluster
        • AWS
        • GCP
        • Azure
      • 2. Configure Container Registry
        • AWS
        • GCP
        • Azure
        • Docker Hub
      • 3. Set Up Ingress, TLS and DNS
      • 4. Configure the Provider
  • Using Garden With
    • Containers
      • Using Remote Container Builder
      • Building Containers
    • Kubernetes
      • Using Remote Kubernetes
      • Using Local Kubernetes
      • Deploying K8s Resources
      • Installing Helm charts
      • Running Tests and Tasks
    • Terraform
      • Using Terraform
      • Applying Terrform Stacks
    • Pulumi
      • Using Pulumi
      • Applying Pulumi Stacks
    • Local Scripts
  • Features
    • Remote Container Builder
    • Team Caching
    • Variables and Templating
    • Config Templates
    • Workflows
    • Code Synchronization
    • Custom Commands
    • Remote Sources
  • Guides
    • Connecting a Project
    • Environments and Namespaces
    • Installing Garden
    • Including/Excluding files
    • Installing Local Kubernetes
    • Migrating from Docker Compose to Garden
    • Using the CLI
    • Using Garden in CircleCI
    • Minimal RBAC Configuration for Development Clusters
    • Deploying to Production
    • Using a Registry Mirror
  • Reference
    • Providers
      • container
      • exec
      • jib
      • kubernetes
      • local-kubernetes
      • otel-collector
      • pulumi
      • terraform
    • Action Types
      • Build
        • container Build
        • exec Build
        • jib-container Build
      • Deploy
        • container Deploy
        • exec Deploy
        • helm Deploy
        • kubernetes Deploy
        • pulumi Deploy
        • terraform Deploy
      • Run
        • container Run
        • exec Run
        • helm-pod Run
        • kubernetes-exec Run
        • kubernetes-pod Run
      • Test
        • container Test
        • exec Test
        • helm-pod Test
        • kubernetes-exec Test
        • kubernetes-pod Test
    • Template Strings
      • Project template context
      • Environment template context
      • Provider template context
      • Action (all fields) template context
      • Action spec template context
      • Module template context
      • Remote Source template context
      • Project Output template context
      • Custom Command template context
      • Workflow template context
      • Template Helper Functions
    • Commands
    • Project Configuration
    • ConfigTemplate Reference
    • RenderTemplate Reference
    • Workflow Configuration
    • Garden Containers on Docker Hub
    • Glossary
    • Module Template Configuration
    • Module Types
      • container
      • exec
      • helm
      • jib-container
      • kubernetes
      • pulumi
      • templated
      • terraform
  • Misc
    • FAQ
    • Troubleshooting
    • Telemetry
    • How Organizations Adopt Garden
    • New Garden Cloud Version
    • Migrating to Cedar
    • Migrating to Bonsai
    • Ingress NGINX Vulnerability
    • Deprecations
  • Contributing to Garden
    • Contributor Covenant Code of Conduct
    • Contributing to the Docs
    • Setting up Your Developer Environment
    • Developing Garden
    • Config Resolution
    • Graph Execution
Powered by GitBook
On this page
  • Prerequisites
  • Project overview
  • Configure remote environments
  • Configure the kubectl context
  • Running Garden commands in CircleCI

Was this helpful?

  1. Guides

Using Garden in CircleCI

PreviousUsing the CLINextMinimal RBAC Configuration for Development Clusters

Last updated 14 days ago

Was this helpful?

Prerequisites

In addition to the prerequisites in the doc.

For the purposes of this example we'll be using and deploying to a Google Kubernetes Engine (GKE) cluster.

Project overview

The project is based on our basic example, but configured for multiple environments. Additionally it contains a CircleCI config file. You'll find the entire source code .

The CI pipeline is configured so that Garden tests the project and deploys it to a preview environment on every pull request. Additionally, it tests the project and deploys it to a separate staging environment on every merge to the main branch.

To see it in action, you can fork the repository and follow the set-up steps below. Once you've set everything up, you can submit a pull request to the fork to trigger a CircleCI job which in turns deploys the project to your remote Kubernetes cluster.

Configure remote environments

Configuring Garden to work against a remote Kubernetes cluster is explained step by step in our .

For this project we're using three environments: local, preview and staging. The local environment is the default and is configured for a local Kubernetes cluster that runs on the user's machine. The other two run on remote clusters.

We deploy to the preview environment every time someone makes a pull request on Github. The configuration looks like this:

# garden.yml
apiVersion: garden.io/v2
kind: Project
name: ci-demo-project
environments:
  ...
  - name: preview
    defaultNamespace: preview-${local.env.CIRCLE_BRANCH || local.username}
providers:
  - name: kubernetes
    environments: [preview]
    context: my-preview-cluster
    defaultHostname: ${environment.namespace}.preview.my-domain
    buildMode: cluster-buildkit

Notice that we're using the CIRCLE_BRANCH environment variable to label the project namespace. This ensures that each pull request gets deployed into its own namespace.

The staging environment is configured in a similar manner. The relevant CI job is triggered on merges to the main branch.

Configure the kubectl context

We create a re-usable command for configuring the kubectl context:

# .circleci/config
commands:
  configure_kubectl_context:
    description: Configure the kubectl context so that we can access our remote cluster
    steps:
      - run:
          name: Configure kubectl context via gcloud
          command: |
            gcloud --quiet components update
            echo $GCLOUD_SERVICE_KEY | gcloud auth activate-service-account --key-file=-
            gcloud --quiet config set project $GCLOUD_PROJECT_ID && gcloud --quiet config set compute/zone $GCLOUD_COMPUTE_ZONE
            gcloud --quiet container clusters get-credentials $GCLOUD_CLUSTER_ID --zone $GCLOUD_COMPUTE_ZONE
            gcloud --quiet auth configure-docker
  • GCLOUD_PROJECT_ID, GCLOUD_COMPUTE_ZONE, and GCLOUD_CLUSTER_ID: These you'll find under the relevant project in your Google Cloud Platform console.

Running Garden commands in CircleCI

Here's what our preview job looks like:

# .circleci/config
jobs:
  preview:
    docker:
      - image: gardendev/garden-gcloud:bonsai-alpine
    environment:
      GARDEN_LOG_LEVEL: verbose # set the log level to your preference here
    steps:
      - checkout
      - configure_kubectl_context
      - run:
          name: Test project
          command: garden test --env=preview
      - run:
          name: Deploy project
          command: garden deploy --env=preview

Notice that there are no configuration steps outside of just configuring the kubectl context. And no matter how you change your stack, these steps will remain the same, making for a highly portable workflow—and much less fiddling around with CI!

You'll find the rest of the config .

We need to make sure that it can access our remote cluster. We do this by setting up a on the CI agent. How you set this up will vary by how and where you have deployed your cluster. What follows is specific to GKE.

The commands use the following environment variables that you can set on the Project Environment Variables page (see ) in the CircleCI dashboard:

GCLOUD_SERVICE_KEY: Follow to get a service account key.

Please refer to this for more information on using the Google Cloud SDK in CircleCI.

You'll find the entire CircleCI config for this project.

Now that we have everything set up, we can to CircleCI and start using Garden in our CI pipelines.

Note: Below we use the gardendev/garden-gcloud container image, that extends the standardgardendev/garden image to bundle the gcloud binary (Google Cloud CLI). For an overview of all official Garden convenience containers, please refer to .

Portable CI Pipelines that Run Anywhere
CircleCI
demo-project
here
Remote Kubernetes guide
here
kubectl context
here
these instructions
doc
here
add the project
the reference guide for DockerHub containers