Skip to main content

Deploy to Kubernetes from GitHub Actions

Report an Issue

This guide shows how to let a GitHub Actions workflow deploy to a Kubernetes cluster through Teleport without storing any secret in GitHub.

You will create a Machine & Workload Identity bot that the workflow joins as, scope the bot's Kubernetes access to a single namespace, and run a workflow that deploys nginx to that namespace. You will then see the join in the audit log and see a run from a branch that isn't allowed being rejected.

Workshop environment

The workshop environment simulates a GitHub Enterprise Server at https://github.localhost:11443, with a sample repository, a runner, and a k3s Kubernetes cluster enrolled in Teleport.

How it works​

The workflow never holds a Teleport credential. Each job gets an ID token signed by GitHub, and tbot, the Machine & Workload Identity agent, exchanges it for short-lived certificates:

NamePurpose
BotA Machine ID identity for the workflow. Its roles define what the workflow can access.
Join tokenA github join token that names the bot and the repositories, branches, and other claims allowed to join. It is not a secret.
ID tokenA JWT that GitHub issues to each job that has id-token: write permission. Its claims include the repository, ref, and workflow.
teleport-actions/auth-k8sA GitHub Action that runs tbot with the job's ID token and writes a kubeconfig for a Kubernetes cluster in Teleport.

Teleport checks the ID token's signature and its claims against the join token's allow rules. The certificates expire shortly after the job ends, so there is nothing to rotate or revoke.

Prerequisites​

  • A Teleport cluster with a Kubernetes cluster enrolled.
  • A Teleport user with the editor role, and tctl signed in as that user.
  • A GitHub repository where you can add workflows. GitHub-hosted runners must be able to reach the Teleport Proxy Service.
  • kubectl access to the Kubernetes cluster as an administrator, to create a namespace and RBAC.
Workshop environment
  • Sign in to https://teleport.localhost:3080 and choose Andrew Admin. This user is in the admins group, which grants editor and Kubernetes access.
  • The k3s cluster is enrolled in Teleport as k3s.
  • The simulated GitHub Enterprise Server hosts the repository example-org/teleport-k8s-demo, with the workflow .github/workflows/deploy.yaml.
  • The environment creates the bot, role, join token, and Kubernetes RBAC from this guide on startup. Read steps 1 to 3 to see how they're configured, then run the workflow in step 4.

Step 1/4. Review Teleport resources​

First, we create a Teleport role allowing access to the demo namespace in Kubernetes.

kind: role
version: v7
metadata:
  name: github-actions-k8s-demo
spec:
  allow:
    kubernetes_labels:
      "*": "*"
    # The Kubernetes group Teleport impersonates.
    # Kubernetes RBAC decides what the group can do.
    kubernetes_groups:
      - github-ci
    kubernetes_resources:
      - kind: "*"
        namespace: demo
        name: "*"
        verbs: ["*"]

Next, we define a bot that can use this role:

kind: bot
version: v1
metadata:
  name: github-actions-k8s-demo
spec:
  roles:
    - github-actions-k8s-demo

Lastly, we create a join token (of type github) that defines the rules under which workloads are allowed to run.

kind: token
version: v2
metadata:
  name: github-actions-k8s-demo
spec:
  roles: [Bot]
  bot_name: github-actions-k8s-demo
  join_method: github
  github:
    allow:
      # Only workflows in this repository...
      - repository: example-org/teleport-k8s-demo
        # ...running on the main branch.
        ref_type: branch
        ref: refs/heads/main

In the workshop environment, these resources are all defined in a single YAML file and created with a single command:

tctl create -f gha-demo-resources.yaml

Step 2/4. Review Kubernetes RBAC​

Teleport forwards the bot's requests to Kubernetes as the group github-ci. Kubernetes RBAC needs to be configured in order to allow this group to deploy to the demo namespace.

The Kubernetes manifest looks like this:

apiVersion: v1
kind: Namespace
metadata:
  name: demo
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: github-ci-deploy
  namespace: demo
rules:
  - apiGroups: ["apps"]
    resources: ["deployments", "replicasets"]
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: github-ci-deploy
  namespace: demo
subjects:
  - kind: Group
    name: github-ci
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: github-ci-deploy
  apiGroup: rbac.authorization.k8s.io

Apply it to the cluster:

kubectl apply -f github-ci-rbac.yaml

Step 3/4. Review the GitHub Actions workflow​

A GitHub Actions workflow (.github/workflows/deploy.yaml) uses a few marketplace actions to run kubectl against a Teleport-protected Kubernetes cluster.

name: Deploy
on:
  push:
    branches: [main]
  workflow_dispatch:
jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      # Lets the job request a GitHub OIDC ID token.
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v7
      - uses: azure/setup-kubectl@v5
      - uses: teleport-actions/setup@v2
        with:
          version: auto
          proxy: teleport.localhost:3080
      - uses: teleport-actions/auth-k8s@v2
        with:
          proxy: teleport.localhost:3080
          token: github-actions-k8s-demo
          certificate-ttl: 10m
          kubernetes-cluster: k3s
      - run: kubectl apply -f ./manifests/deployment.yaml
      - run: kubectl -n demo rollout status deployment/nginx --timeout=120s
      - run: kubectl -n demo get pods

The id-token: write permission is required. Without it, GitHub doesn't issue an ID token and tbot can't join. The auth-k8s step sets KUBECONFIG for the steps that follow, so kubectl connects to the cluster through Teleport.

In this example, we're applying a simple nginx deployment (manifests/deployment.yaml).

Step 4/4. Run the workflow​

Open the repository's Actions tab, select Deploy, and click Run workflow on the main branch. In the workshop environment, open https://github.localhost:11443/example-org/teleport-k8s-demo/actions.

When the run finishes, the last step lists two nginx pods. In the workshop environment, the auth-k8s step also logs the claims of the ID token that Teleport checked, such as repository and ref.

To see the join, open the audit log in the Teleport Web UI and find the Bot Joined event (bot.join) with the method github. Its attributes show the repository, ref, workflow, and actor from the ID token.

To check the deployment yourself, connect to the cluster through Teleport:

tsh kube login k3s
kubectl -n demo get pods

Try a branch that isn't allowed​

Run the workflow again, this time on another branch, such as dev. The join token only allows refs/heads/main, so Teleport rejects the join and the auth-k8s step fails with id token claims did not match any allow rules. The deploy steps are skipped, and the audit log shows a failed bot.join event.

Further reading​