Deploy to Kubernetes from GitHub Actions
To deploy from GitHub Actions, give the workflow its own service account that can only change one namespace, and a kubeconfig that signs in with that service account’s token. Don’t use the kubeconfig you download from the portal: it has full administrator access to the whole cluster, and anyone who can read your CI secrets would get that access too.
You set this up once with the admin kubeconfig, then every workflow run uses the limited one.
How it works
- With the admin kubeconfig, you create a namespace for the app, a service account in it, a RoleBinding that grants the service account permissions in that namespace only, and a token for the service account.
- You build a small kubeconfig from the cluster’s API endpoint, its CA certificate and that token.
- You store that kubeconfig as a GitHub Actions secret. The workflow writes it to a file and runs
kubectlorhelm.
Before you begin
- A running cluster. On the cluster’s Overview tab, under Connection Details, download the admin kubeconfig with Kubeconfig → Download.
kubectlon your machine.- Admin access to the GitHub repository, to add secrets. The GitHub CLI (
gh) is optional. - The GitHub Actions runner must be able to reach the cluster’s API endpoint, shown under Connection Details. If the endpoint isn’t reachable from GitHub-hosted runners, use a self-hosted runner in a network that can reach it.
The examples use the namespace example-app and the service account github-deployer. Replace them with your own names.
Steps
Choose the permissions CI needs
Grant one of the built-in Kubernetes roles, through a RoleBinding so it applies to one namespace only:
Use edit unless your manifests or Helm charts create Roles or RoleBindings. Neither role can create namespaces, CustomResourceDefinitions (CRDs) or ClusterRoles. Install those once with the admin kubeconfig.
Create the namespace, service account, role binding and token
Kubernetes fills the Secret with a token for the service account. This token doesn’t expire. You replace it when you rotate it (see Rotate or revoke the token).
To let the same service account deploy to more namespaces, add a RoleBinding like the one above in each of them. Keep subjects[0].namespace set to example-app, where the service account lives.
Build the CI kubeconfig
These commands read the API endpoint and CA certificate from the admin kubeconfig, read the token, and write a new file, github-deployer.kubeconfig:
github-deployer.kubeconfig contains the token. Treat it like a password: don’t commit it, and delete it once it’s stored in GitHub.
Store the kubeconfig as a GitHub secret
With the GitHub CLI, from inside your repository:
Or in GitHub, open the repository’s Settings → Secrets and variables → Actions, click New repository secret, name it KUBE_CONFIG, and paste the whole file.
To require approval before deploying, or to keep separate kubeconfigs for separate clusters, store the secret on a GitHub environment instead (gh secret set KUBE_CONFIG --env production < github-deployer.kubeconfig) and add environment: production to the job.
Then delete the local copy:
Build, push and deploy an image
To build your image, push it to Container Registry and roll it out in one workflow, add a Pull and push registry access token to the repository as IBEE_REGISTRY_USERNAME and IBEE_REGISTRY_TOKEN, then replace the Deploy step:
The cluster pulls the image with its own Pull only token, not the CI token. See Pull private images in Kubernetes.
Deploy with Helm
Helm uses the same KUBECONFIG. Add azure/setup-helm@v4 and run:
If the chart creates Roles or RoleBindings, bind admin instead of edit. If it installs CRDs, install them once with the admin kubeconfig.
Verify
- The workflow run finishes and the Deploy step prints
deployment "web-api" successfully rolled out. - With the admin kubeconfig,
kubectl get deployments -n example-appshows the new version.
Rotate or revoke the token
To rotate the token, delete the token Secret, apply github-deployer.yaml again, rebuild the kubeconfig and update the KUBE_CONFIG secret in GitHub. The old token stops working when its Secret is deleted:
To revoke CI’s access entirely, delete the service account. Its token Secret and access go with it:
Troubleshooting
Related pages
- Pull private images in Kubernetes
- Container Registry: store the images your workflow builds
- Kubernetes with External Secrets: sync app secrets into the cluster instead of applying them from CI
