# Building Kubernetes PR Previews with Shared Pulumi Components

DevFeed: [Building Kubernetes PR Previews with Shared Pulumi Components](<https://devfeed.tech/articles/building-kubernetes-pr-previews-with-shared-pulumi-components-63794.md>)

Original publisher: [Read original article](<https://www.pulumi.com/blog/building-kubernetes-pr-previews-with-shared-pulumi-components/>)

Author: Sangharsh Agarwal

Published: 2026-10-01T00:00:00Z

Content type: tutorial

Language: en

Sources: [Pulumi](<https://devfeed.tech/sources/pulumi.md>)

Topics: [CI/CD](<https://devfeed.tech/topics/cicd.md>), [pulumi](<https://devfeed.tech/topics/pulumi.md>), [Service fabric](<https://devfeed.tech/topics/service-fabric.md>), [Pull Request](<https://devfeed.tech/topics/pull-request.md>)

Tags: [backend](<https://devfeed.tech/tags/backend.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [ci](<https://devfeed.tech/tags/ci.md>), [environment](<https://devfeed.tech/tags/environment.md>), [ephemeral-environments](<https://devfeed.tech/tags/ephemeral-environments.md>), [infrastructure](<https://devfeed.tech/tags/infrastructure.md>), [kubernetes](<https://devfeed.tech/tags/kubernetes.md>), [preview-environments](<https://devfeed.tech/tags/preview-environments.md>), [pull-request](<https://devfeed.tech/tags/pull-request.md>), [pulumi](<https://devfeed.tech/tags/pulumi.md>), [python](<https://devfeed.tech/tags/python.md>)

## AI overview

The article explains how a team uses shared Pulumi components and Kubernetes namespaces to create, update, and clean up ephemeral preview environments for pull requests. It covers CI lifecycle automation, serialized updates to avoid stack conflicts, scheduled reconciliation for missed cleanup, and handling infrastructure drift. The team reports an average of about four minutes from starting deployment until a preview is ready.

## Source excerpt

My team develops a microservices application on Kubernetes, with hundreds of PRs opened each day. To let engineers test and review those changes in isolation before they're merged, we give every pull request its own ephemeral environment. We use Pulumi to define those short-lived PR environments from a component resource that's shared with our long-lived Dev, Stage, Prod environments. Each PR gets its own Pulumi stack and Kubernetes namespace, which we tear down once the PR is merged or closed. In this post, I'll walk through how we've implemented this pattern and what we've learned from running it at scale. We use a self-managed Pulumi backend, so we manage the end-to-end lifecycle of PR environments ourselves. Pulumi Cloud users can instead use review stacks to automate the lifecycle of ephemeral environments tied to pull requests. How our ephemeral environments work A Kubernetes namespace gives each PR its own place to run, but to get to a working preview environment we need to do quite a bit on top of that. We still need to deploy the application into that namespace, make it reachable to reviewers, update it as new commits arrive, and remove it when the PR closes. And while a namespace separates the Kubernetes resources for each preview, things like database isolation, access controls, and network policies depend on the application and cluster. Pulumi gives us a way to define these environments once while managing each one independently. Dev, Stage, Prod, and each PR run the same Pulumi program in separate stacks. The program defines the infrastructure for an environment, while each stack maintains its own configuration and state. For each PR, our CI creates or updates its Pulumi stack as new commits arrive, then destroys it when the PR closes. Defining the environment Our implementation uses a single custom Pulumi component, but for the simplified example below, we've split it into two components to make the pattern easier to follow. StackInstance represents on