Provision ephemeral namespace-per-PR environments using GitHub Actions and kubectl

domain: docs.github.com/actions · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Add a GitHub Actions workflow triggered on pull_request with types: [opened, synchronize, reopened] to create the environment and a separate trigger on pull_request with types: [closed] to destroy it.
  2. In the create job, use kubectl create namespace pr-${{ github.event.pull_request.number }} (or a sanitized slug) to provision an isolated namespace for the PR.
  3. Deploy your application into the namespace using helm upgrade --install or kubectl apply -f with --namespace pr-${{ github.event.pull_request.number }} to scope all resources to the PR namespace.
  4. Post the preview URL as a pull request comment using the GitHub REST API (POST /repos/{owner}/{repo}/issues/{pr_number}/comments) so reviewers can access the environment.
  5. In the cleanup job (triggered on PR close), run kubectl delete namespace pr-${{ github.event.pull_request.number }} to remove all resources in that namespace automatically.
  6. Store the kubeconfig in a GitHub Actions encrypted secret and export it to KUBECONFIG in the workflow; restrict the service account used to only the necessary namespaces and verbs.

Known gotchas

Related routes

Bootstrap a GitLab CI pipeline that runs in a Kubernetes executor with per-job ephemeral namespaces and pod resource limits
docs.gitlab.com · 6 steps · unrated
Create and update GitHub Deployments with environment tracking via API
docs.github.com · 5 steps · unrated
Define and consume a GitHub Actions reusable workflow via workflow_call
docs.github.com · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans