kubectl config use-context: Command Reference for CI/CD
By Kaveh Alemi·Latchkey
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.
Frequently asked questions
kubectl config use-context: Command Reference for CI/CD?
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.
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.
Latchkey managed runners at $0.0025/min at 2 vCPU, against $0.006 GitHub-hosted, and transient failures repaired on the runner.Start free →30-day trial · No credit card
Cookie Preferences
Choose which categories of cookies you want to allow. Essential cookies are always active as they are required for the site to function.
Essential
Required for the site to function.
Functional
Remembers your preferences like selected organization and dashboard settings.
Analytics
Helps us understand how the site is used (Google Analytics).