Skip to content
Latchkey

kubectl config use-context: Select Clusters in CI

Set the active kubeconfig context so subsequent commands target the right cluster.

kubectl config use-context selects which cluster/user/namespace tuple kubectl talks to. In multi-cluster CI it is the safety step before any apply, ensuring you deploy to the intended cluster. The context name must already exist in kubeconfig (added by get-credentials).

Common flags

  • CONTEXT_NAME - the context to activate (positional)
  • kubectl config get-contexts - list available contexts
  • kubectl config current-context - print the active one

Example in CI

Switch to the prod context, then confirm it.

shell
kubectl config use-context prod-cluster
kubectl config current-context

Common errors in CI

  • error: no context exists with the name: "X" - context not in kubeconfig (run get-credentials first)
  • error: current-context is not set - no active context selected
  • The connection to the server localhost:8080 was refused - kubeconfig empty/unset

Key takeaways

  • Selects the active cluster context before any deploy command.
  • In multi-cluster CI it prevents deploying to the wrong cluster.
  • The context must already exist; get-credentials adds it.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →