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 nodesIn 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
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl config set-context: Command Reference for CI/CDReference for kubectl config set-context: create or modify a context and pin a default namespace so CI comman…
kubectl get: Command Reference for CI/CDReference for kubectl get: list resources, output formats, label and field selectors, jsonpath extraction, an…
kubectl auth can-i: Command Reference for CI/CDReference for kubectl auth can-i: check whether the current credentials may perform an action before attempti…