Sub topic 7
Authenticating with a kubeconfig secret
This chapter assumes you know basic kubectl usage, can read YAML, and have seen a GitHub Actions workflow with jobs and steps. You will store a kubeconfig as a GitHub secret and use it to let a deploy job talk to your cluster.
What a kubeconfig contains
A kubeconfig is a YAML file that tells kubectl three things: which cluster to contact (the API server address and its CA certificate), which identity to use (a token or client certificate), and which context ties the two together. A GitHub runner starts empty, so it has none of this. The simplest way to give it access is to store the file as a secret and write it to disk during the job.
Storing the secret
Create a secret named KUBE_CONFIG and paste the full file contents into it. Scope it to the production environment rather than the whole repository. Then only jobs that declare environment: production can read it, and you can add required reviewers to that environment.
Use a kubeconfig for a dedicated deploy identity, such as a ServiceAccount. Do not use your own admin kubeconfig. The next chapter covers how to restrict that identity.
Writing the kubeconfig in the job
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- name: Write kubeconfig
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
run: |
mkdir -p "$HOME/.kube"
printf '%s' "$KUBE_CONFIG" > "$HOME/.kube/config"
chmod 600 "$HOME/.kube/config"
- name: Check access
run: kubectl get deployments -n shopThree details matter here.
- The secret goes into an
envvariable first, and the script reads$KUBE_CONFIG. This avoids pasting${{ secrets.KUBE_CONFIG }}directly into the shell script, where special characters in the value could be interpreted by the shell. printf '%s'writes the value exactly as it is.echocan alter backslashes or add a newline on some shells.chmod 600makes the file readable only by its owner. kubectl warns about loose permissions, and other processes on the runner should not read it.
kubectl looks for $HOME/.kube/config by default, so later steps need no extra flags.
How the pieces fit together
Common errors
| Symptom | Likely cause |
|---|---|
| `The connection to the server localhost:8080 was refused` | The file was not written, is empty, or the secret is missing because the job does not declare the right environment. |
| `error: You must be logged in to the server (Unauthorized)` | The token in the kubeconfig expired or was revoked. |
| `Forbidden` on a specific resource | The identity is valid but its RBAC role lacks that verb or namespace. |
| `Unable to connect ... certificate` | The API server address is not reachable from GitHub-hosted runners, or the CA data is wrong. |
Why this is not the end state
This approach works with any cluster, which makes it a good starting point. The cost is that someone must rotate the token by hand, and a leaked copy stays valid until you do. A later chapter shows how OIDC federation removes the stored credential.
Exercise
A deploy job fails with "The connection to the server localhost:8080 was refused" even though you created the KUBE_CONFIG secret in the production environment. Name two things to check.
Show answer
First, check that the job declares environment: production. Without it, the environment-scoped secret is empty. Second, check that the step writes the file to $HOME/.kube/config (or that KUBECONFIG points to it) before any kubectl command runs. kubectl falls back to localhost:8080 when it finds no kubeconfig.