Skip to navigation

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

  1. 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.
  2. You build a small kubeconfig from the cluster’s API endpoint, its CA certificate and that token.
  3. You store that kubeconfig as a GitHub Actions secret. The workflow writes it to a file and runs kubectl or helm.

Before you begin

  • A running cluster. On the cluster’s Overview tab, under Connection Details, download the admin kubeconfig with Kubeconfig → Download.
  • kubectl on 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

1

Point kubectl at the admin kubeconfig

export KUBECONFIG=~/Downloads/PASTE-KUBECONFIG-FILE-NAME
kubectl get nodes

The command lists your cluster’s nodes.

2

Choose the permissions CI needs

Grant one of the built-in Kubernetes roles, through a RoleBinding so it applies to one namespace only:

RoleCI canCI can’t
editCreate, change and delete most objects in the namespace: Deployments, StatefulSets, Services, ConfigMaps, Secrets, Ingresses, Jobs, CronJobs, PersistentVolumeClaimsCreate Roles or RoleBindings; change anything in other namespaces or anything cluster-wide
adminEverything edit can, plus Roles and RoleBindings in the namespaceChange anything in other namespaces or anything cluster-wide

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.

3

Create the namespace, service account, role binding and token

github-deployer.yaml
apiVersion: v1
kind: Namespace
metadata:
name: example-app
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: github-deployer
namespace: example-app
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: github-deployer
namespace: example-app
subjects:
- kind: ServiceAccount
name: github-deployer
namespace: example-app
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: edit # or admin, see the table above
---
apiVersion: v1
kind: Secret
metadata:
name: github-deployer-token
namespace: example-app
annotations:
kubernetes.io/service-account.name: github-deployer
type: kubernetes.io/service-account-token
kubectl apply -f github-deployer.yaml

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.

4

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:

NAMESPACE=example-app
SA=github-deployer
OUT=github-deployer.kubeconfig
SERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
kubectl config view --raw --minify \
-o jsonpath='{.clusters[0].cluster.certificate-authority-data}' | base64 --decode > ca.crt
TOKEN=$(kubectl -n "$NAMESPACE" get secret "$SA-token" -o jsonpath='{.data.token}' | base64 --decode)
kubectl --kubeconfig="$OUT" config set-cluster cluster --server="$SERVER" \
--certificate-authority=ca.crt --embed-certs=true
kubectl --kubeconfig="$OUT" config set-credentials "$SA" --token="$TOKEN"
kubectl --kubeconfig="$OUT" config set-context ci --cluster=cluster --user="$SA" --namespace="$NAMESPACE"
kubectl --kubeconfig="$OUT" config use-context ci
rm ca.crt

github-deployer.kubeconfig contains the token. Treat it like a password: don’t commit it, and delete it once it’s stored in GitHub.

5

Check what the new kubeconfig can do

kubectl --kubeconfig=github-deployer.kubeconfig auth can-i create deployments -n example-app
kubectl --kubeconfig=github-deployer.kubeconfig auth can-i create deployments -n kube-system

The first command prints yes and the second prints no.

6

Store the kubeconfig as a GitHub secret

With the GitHub CLI, from inside your repository:

gh secret set KUBE_CONFIG < github-deployer.kubeconfig

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:

rm github-deployer.kubeconfig
7

Use it in a workflow

This workflow applies the manifests in k8s/ on every push to main and waits for the rollout to finish:

.github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: azure/setup-kubectl@v4
- name: Configure kubeconfig
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
run: |
install -m 600 /dev/null "$RUNNER_TEMP/kubeconfig"
printf '%s\n' "$KUBE_CONFIG" > "$RUNNER_TEMP/kubeconfig"
echo "KUBECONFIG=$RUNNER_TEMP/kubeconfig" >> "$GITHUB_ENV"
- name: Deploy
run: |
kubectl apply -n example-app -f k8s/
kubectl rollout status deployment/web-api -n example-app --timeout=180s

Passing the secret through env: rather than writing ${{ secrets.KUBE_CONFIG }} inside run: keeps it out of the generated script.

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:

- uses: docker/login-action@v3
with:
registry: registry.ibee.ai
username: ${{ secrets.IBEE_REGISTRY_USERNAME }}
password: ${{ secrets.IBEE_REGISTRY_TOKEN }}
- name: Build and push
run: |
docker build -t registry.ibee.ai/acme-prod/web-api:${{ github.sha }} .
docker push registry.ibee.ai/acme-prod/web-api:${{ github.sha }}
- name: Deploy
run: |
kubectl set image deployment/web-api \
web-api=registry.ibee.ai/acme-prod/web-api:${{ github.sha }} -n example-app
kubectl rollout status deployment/web-api -n example-app --timeout=180s

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:

helm upgrade --install web-api ./chart --namespace example-app --wait

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-app shows 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:

kubectl delete secret github-deployer-token -n example-app
kubectl apply -f github-deployer.yaml

To revoke CI’s access entirely, delete the service account. Its token Secret and access go with it:

kubectl delete serviceaccount github-deployer -n example-app

Troubleshooting

You seeWhat it meansWhat to do
Unable to connect to the server or i/o timeoutThe runner can’t reach the cluster’s API endpointCheck the endpoint under Connection Details. If GitHub-hosted runners can’t reach it, use a self-hosted runner that can.
x509: certificate signed by unknown authorityThe kubeconfig is missing the cluster’s CA certificateRebuild it with --embed-certs=true as shown above.
error: You must be logged in to the server (Unauthorized)The token was rotated or the service account was deletedRebuild the kubeconfig and update the GitHub secret.
Forbidden: User "system:serviceaccount:example-app:github-deployer" cannot create resource "roles"The edit role can’t create Roles or RoleBindingsBind admin instead of edit.
Forbidden for namespaces, customresourcedefinitions or clusterrolesCluster-wide objects are outside the service account’s namespaceCreate them once with the admin kubeconfig.
Forbidden in another namespaceThe service account only has a RoleBinding in example-appAdd a RoleBinding in that namespace.
The token in github-deployer-token is emptyThe kubernetes.io/service-account.name annotation doesn’t match the service account, or the Secret was created moments agoCheck the annotation, wait a few seconds and read the Secret again.
ImagePullBackOff after deployingThe cluster can’t pull your private imageSee Pull private images in Kubernetes.