Skip to content
Latchkey

kubectl config use-context: Command Reference for CI/CD

Select which cluster and user kubectl talks to.

kubectl config use-context switches the active context so subsequent commands hit the right cluster. In CI it is the first deploy step, and the one you should log to prove you are on the intended cluster. This reference covers it and its verification commands.

Common flags and usage

  • config use-context <name>: set the active context
  • config get-contexts: list available contexts
  • config current-context: print the active context (log this in CI)
  • --context <name> on any command: override per-invocation
  • Set KUBECONFIG to point at the credentials CI wrote

Example

shell
export KUBECONFIG=${{ runner.temp }}/kubeconfig
kubectl config use-context prod-cluster
kubectl config current-context   # log it: prove the target
kubectl get nodes

In CI

A runner often has no ~/.kube/config, so set KUBECONFIG to the path you wrote credentials to, or pass --context per command. "no context exists with the name" means the context is not in the loaded kubeconfig. Echo current-context early so a wrong-cluster deploy fails loudly instead of silently hitting the wrong default namespace.

Key takeaways

  • Set KUBECONFIG explicitly in CI; runners have no default kubeconfig.
  • Log current-context so a wrong-cluster deploy is obvious.
  • --context overrides per command without changing the active one.

Related guides

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