> ## Documentation Index
> Fetch the complete documentation index at: https://docs.costgraph.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Run CI jobs on your own cluster

> Let AutoPilot for CI run GitHub Actions jobs on one-job runners in spare capacity on your own Kubernetes cluster.

> [!NOTE]
>
> Running CI jobs on your own cluster is in preview.

[AutoPilot for CI](https://docs.costgraph.ai/costgraph/autopilot/ci.md) can send a job to spare capacity
on your own Kubernetes cluster. A dispatcher from the
[agent runtime chart](https://docs.costgraph.ai/costgraph/workflows/your-environment.md) starts a GitHub
Actions runner pod for each job through GitHub's
[actions-runner-controller](https://docs.github.com/en/actions/concepts/runners/actions-runner-controller).
The pod takes only that job and is removed when the job ends.

CostGraph registers each runner through the CostGraph GitHub App, so you don't
need your own GitHub App, a personal access token, or a runner scale set. To
route to runner scale sets you already run, see
[Use runner sets you manage](#use-runner-sets-you-manage).

## Requirements

* AutoPilot for CI is set up as described in [Requirements](https://docs.costgraph.ai/costgraph/autopilot/ci.md#requirements).
* An owner of the GitHub organization has approved the CostGraph GitHub App's
  **Self-hosted runners** organization permission, set to **Read and write**,
  and its **Workflow job** event. Until then, the cluster is skipped with
  reason `github_app_reapproval`.
* The [CostGraph operator](https://docs.costgraph.ai/costgraph/operator/overview.md) runs in the cluster.
* Every node has a price. Nodes on a supported cloud are priced from their
  instance type. Other nodes need a [pricing ID](#price-nodes-without-an-instance-type).
* Jobs run on Linux. Jobs that use Docker need Kubernetes 1.29 or later.

## Install

Install actions-runner-controller and the dispatcher with one release of the
`costgraph-agent-runtime` chart.

### Create the secrets

The dispatcher can use the operator's CostGraph API key. In CostGraph, add the
`sandbox:worker` and `deployment:register` scopes to that key. The first lets
the dispatcher take jobs. The second lets the cluster pull the dispatcher image
from `registry.costgraph.ai`. To use a separate key, create one with the same
two scopes.

The chart reads the key from a secret under `api-key` and needs a pull secret
for the registry. Create both in the runtime's namespace.

> [tab] kubectl
>
> The following commands copy the key from the operator's secret. For an
> operator release named `costgraph-operator`, the secret is
> `costgraph-operator-credentials` in the `costgraph` namespace. If you set
> `global.existingSecret`, use that secret instead.
>
> ```shell theme={null}
> kubectl create namespace costgraph-agents
> COSTGRAPH_API_KEY="$(kubectl -n costgraph get secret costgraph-operator-credentials \
>   -o jsonpath='{.data.apiKey}' | base64 -d)"
> kubectl -n costgraph-agents create secret generic costgraph-sandbox-key \
>   --from-literal=api-key="$COSTGRAPH_API_KEY"
> kubectl -n costgraph-agents create secret docker-registry costgraph-registry \
>   --docker-server=registry.costgraph.ai \
>   --docker-username=x \
>   --docker-password="$COSTGRAPH_API_KEY"
> ```

> [tab] External Secrets Operator
>
> Point two ExternalSecrets at the remote key the operator's ExternalSecret
> uses. Replace the store and the remote key with your own.
>
> ```yaml theme={null}
> apiVersion: external-secrets.io/v1
> kind: ExternalSecret
> metadata:
>   name: costgraph-sandbox-key
>   namespace: costgraph-agents
> spec:
>   secretStoreRef:
>     kind: ClusterSecretStore
>     name: my-store
>   target:
>     name: costgraph-sandbox-key
>   data:
>     - secretKey: api-key
>       remoteRef:
>         key: costgraph/api-key
> ---
> apiVersion: external-secrets.io/v1
> kind: ExternalSecret
> metadata:
>   name: costgraph-registry
>   namespace: costgraph-agents
> spec:
>   secretStoreRef:
>     kind: ClusterSecretStore
>     name: my-store
>   target:
>     name: costgraph-registry
>     template:
>       type: kubernetes.io/dockerconfigjson
>       data:
>         .dockerconfigjson: '{"auths":{"registry.costgraph.ai":{"username":"x","password":"{{ .apiKey }}"}}}'
>   data:
>     - secretKey: apiKey
>       remoteRef:
>         key: costgraph/api-key
> ```

### Install the chart

Set `ciRunner.cluster` to the `global.clusterName` of the CostGraph operator in
this cluster. `arcController.enabled: true` installs actions-runner-controller
0.15.1 with the release.

> [tab] Helm
>
> Write the values to `values.yaml`.
>
> ```yaml theme={null}
> dispatcher:
>   workerName: ci-prod
>   existingSecret: costgraph-sandbox-key
>   imagePullSecrets:
>     - name: costgraph-registry
>   ciRunner:
>     enabled: true
>     cluster: prod-eu
>     arch: x86_64
>     maxRunners: 20
> arcController:
>   enabled: true
> ```
>
> Install the chart.
>
> ```shell theme={null}
> helm repo add costgraph https://charts.costgraph.ai
> helm install agent-runtime costgraph/costgraph-agent-runtime \
>   --version 0.7.9 --namespace costgraph-agents -f values.yaml
> ```

> [tab] Argo CD
>
> Create an Application for the chart. `ServerSideApply=true` lets Argo CD
> apply the actions-runner-controller resource definitions, which are too
> large for a client-side apply. Create the secrets before the first sync.
>
> ```yaml theme={null}
> apiVersion: argoproj.io/v1alpha1
> kind: Application
> metadata:
>   name: costgraph-agent-runtime
>   namespace: argocd
> spec:
>   project: default
>   source:
>     repoURL: https://charts.costgraph.ai
>     chart: costgraph-agent-runtime
>     targetRevision: 0.7.9
>     helm:
>       releaseName: agent-runtime
>       valuesObject:
>         dispatcher:
>           workerName: ci-prod
>           existingSecret: costgraph-sandbox-key
>           imagePullSecrets:
>             - name: costgraph-registry
>           ciRunner:
>             enabled: true
>             cluster: prod-eu
>             arch: x86_64
>             maxRunners: 20
>         arcController:
>           enabled: true
>   destination:
>     server: https://kubernetes.default.svc
>     namespace: costgraph-agents
>   syncPolicy:
>     automated: {}
>     syncOptions:
>       - CreateNamespace=true
>       - ServerSideApply=true
> ```

After the dispatcher connects, AutoPilot considers the cluster for every Linux
job that fits it. The routing job's log lists it under the `kubernetes`
provider. To disconnect the cluster, uninstall the release or remove the
`sandbox:worker` scope from the key.

### Chart values

| Value | Meaning |
| - | - |
| `dispatcher.ciRunner.enabled` | Runs CI jobs in this cluster. |
| `dispatcher.ciRunner.cluster` | The operator's `global.clusterName` for this cluster. Required. |
| `dispatcher.ciRunner.arch` | Node architecture for runners, `x86_64` (default) or `arm64`. |
| `dispatcher.ciRunner.maxRunners` | Most runner pods at once. `0`, the default, is unlimited. |
| `dispatcher.ciRunner.image` | Runner image. Empty uses the [CostGraph CI runner image](https://docs.costgraph.ai/costgraph/autopilot/ci-sandboxes.md#runner-image). |
| `arcController.enabled` | Installs actions-runner-controller with this release. |
| `dispatcher.ciRunner.controllerServiceAccount.name` | Service account of an actions-runner-controller you installed. Required when `arcController.enabled` is `false`. |
| `dispatcher.ciRunner.controllerServiceAccount.namespace` | Namespace of that service account. Empty uses the release namespace. |

One release runs runners on one architecture. To run `x86_64` and `arm64` jobs
on the same cluster, install a second release with another `workerName` and
the other `arch`.

### Use an actions-runner-controller you already run

Set `arcController.enabled: false` and name the controller's service account.
The chart lets that controller create runner pods in the release namespace.
With the CostGraph operator chart's `arc.enabled`, the service account is
`costgraph-arc-controller` in the operator's namespace.

```yaml theme={null}
dispatcher:
  ciRunner:
    controllerServiceAccount:
      name: costgraph-arc-controller
      namespace: costgraph
arcController:
  enabled: false
```

## How each job runs

AutoPilot sends a job to the cluster only when a ready node of the release's
architecture has enough unrequested CPU and memory for the runner pod. The pod
requests the CPU and memory AutoPilot measured for the job. Before the job is
measured, it requests half the default runner's vCPUs, at least 1, and up to
2 GB of memory. When `maxRunners` pods are running, AutoPilot sends no more
jobs to the cluster until one finishes.

A job that uses Docker gets a privileged Docker-in-Docker sidecar in its runner
pod. If the cluster refuses privileged pods in the release namespace, AutoPilot
stops sending Docker jobs to the cluster. Jobs without Docker still go there.

Routing to the cluster otherwise works like any other runner. Your
[policy](https://docs.costgraph.ai/costgraph/autopilot/ci.md#policies),
[trials](https://docs.costgraph.ai/costgraph/autopilot/ci.md#try-new-runners), and
[provider limits](https://docs.costgraph.ai/costgraph/autopilot/ci-settings.md#provider-limits) apply,
with the cluster under the `kubernetes` provider. A cluster is skipped with
reason `target_offline` when its dispatcher has not connected in the last 10
minutes or its operator has stopped reporting.

To limit which workflows use the cluster, set `providers` on the routing job.
To send every job that fits to the cluster, set `pin: kubernetes`. Refer to
[Pin a workflow to a provider](https://docs.costgraph.ai/costgraph/autopilot/ci.md#pin-a-workflow-to-a-provider).

## What a job costs

The capacity a cluster job uses is already paid for, so the job counts as \$0
for routing and [savings](https://docs.costgraph.ai/costgraph/autopilot/ci.md#savings). CostGraph still
records the node's share next to every run, so you can see what the cluster
spent. The share is the node's hourly price times the larger of the pod's share
of the node's CPU and of its memory.

### Price nodes without an instance type

A cluster with no priced node that fits the job is skipped with reason
`not_priced`. Nodes without a cloud instance type, such as bare metal,
on-premises, or kind nodes, need a compute rate and a
[pricing ID](https://docs.costgraph.ai/costgraph/operator/on-prem-pricing.md) label.

```bash theme={null}
kubectl label node <node> costgraph.ai/pricing-id.compute=<pricing-id>
```

## Use runner sets you manage

AutoPilot also routes to runner scale sets you run with actions-runner-controller.
The CostGraph operator reports each runner set, and the runner set's name is the
label AutoPilot returns. Runner sets follow the same capacity and cost rules as
the dispatcher's runners.

* AutoPilot matches jobs on the CPU and memory requests of the `runner`
  container. Without requests, it uses the job's measured usage or the default
  runner's size.
* Jobs that use Docker go only to runner sets in Docker-in-Docker mode
  (`containerMode.type: dind`).
* A runner set that is not running is skipped with reason
  `cluster_unavailable`.
* A runner set registered for a GitHub enterprise can take jobs from every
  organization of that enterprise you connect to CostGraph.

A runner set registers its runners with a credential you own, because the
CostGraph GitHub App's private key cannot live in your cluster. We recommend a
GitHub App of your own. A runner set registered for a GitHub enterprise needs a
personal access token instead. Refer to GitHub's
[authentication guide](https://docs.github.com/en/actions/how-tos/manage-runners/use-actions-runner-controller/authenticate-to-the-api).

### Create your own GitHub App

Create a GitHub App in your organization's **Settings > Developer settings >
GitHub Apps**, with **Webhook** turned off, and install it on the organization.
For an organization runner set, give it **Self-hosted runners** set to **Read
and write** under **Organization permissions**. For a repository runner set,
give it **Administration** set to **Read and write** under **Repository
permissions**. Both need **Metadata** set to **Read-only**.

Create the secret in the runner set's namespace from the app's **App ID**, its
installation ID, and a private key you generate on its **General** page. The
installation ID is the number at the end of the installation's URL. With the
CostGraph operator chart, the namespace is the operator's, `costgraph`.

```shell theme={null}
kubectl create secret generic arc-github-app \
  --namespace costgraph \
  --from-literal=github_app_id=123456 \
  --from-literal=github_app_installation_id=654321 \
  --from-file=github_app_private_key=path/to/private-key.pem
```

To use a personal access token instead, create a classic token. Give it the
`admin:org` scope for an organization runner set or the `repo` scope for a
repository runner set. Store it in the same secret as `github_token`.

```shell theme={null}
kubectl create secret generic arc-github-app \
  --namespace costgraph \
  --from-literal=github_token=YOUR_TOKEN
```

### Install a runner set

You can install a runner set with GitHub's charts or with the CostGraph
operator chart. The operator finds runner sets either way.

> [tab] GitHub's charts
>
> Follow GitHub's
> [quickstart](https://docs.github.com/en/actions/tutorials/use-actions-runner-controller/get-started)
> and [deploy a runner scale set](https://docs.github.com/en/actions/how-tos/manage-runners/use-actions-runner-controller/deploy-runner-scale-sets).
> Set `githubConfigSecret` to the secret from
> [Create your own GitHub App](#create-your-own-github-app).

> [tab] CostGraph operator chart
>
> Set `arc.enabled: true` in the operator chart's values to install
> actions-runner-controller 0.15.1 and one runner set with the release. The
> `arcController` and `arcScaleSet` values pass unchanged to GitHub's charts.
> The runner set uses Docker-in-Docker mode by default. If the operator is
> not in the `costgraph` namespace, set
> `arcScaleSet.controllerServiceAccount.namespace` to its namespace.
>
> ```yaml theme={null}
> arc:
>   enabled: true
> arcScaleSet:
>   githubConfigUrl: https://github.com/acme
>   githubConfigSecret: arc-github-app
>   runnerScaleSetName: acme-k8s-4cpu
>   minRunners: 0
>   maxRunners: 10
>   template:
>     spec:
>       containers:
>         - name: runner
>           image: ghcr.io/actions/actions-runner:latest
>           command: ["/home/runner/run.sh"]
>           resources:
>             requests:
>               cpu: "4"
>               memory: 16Gi
> ```
>
> For more runner sets, install GitHub's runner scale set chart once per
> extra set.
>
> Helm installs the controller's resource definitions only on a fresh
> install. To turn on `arc.enabled` in an existing release, apply them before
> `helm upgrade`.
>
> ```shell theme={null}
> helm pull oci://ghcr.io/actions/actions-runner-controller-charts/gha-runner-scale-set-controller \
>   --version 0.15.1 --untar
> kubectl apply --server-side -f gha-runner-scale-set-controller/crds
> ```
>
> Before you uninstall the release or turn off `arc.enabled`, delete the
> runner set so the controller can remove its runners from GitHub.
>
> ```shell theme={null}
> kubectl delete autoscalingrunnerset --all --namespace costgraph
> ```
>
> Helm leaves the resource definitions in the cluster. Remove them after you
> uninstall.
>
> ```shell theme={null}
> kubectl delete crd autoscalingrunnersets.actions.github.com \
>   autoscalinglisteners.actions.github.com \
>   ephemeralrunnersets.actions.github.com \
>   ephemeralrunners.actions.github.com
> ```

## Public repositories

Runners on your cluster join your organization's **Default** runner group
unless a runner set names another. By default, GitHub doesn't let that group
run jobs from public repositories, so those jobs never reach your cluster. To
allow them, an organization owner opens **Settings > Actions > Runner groups**
in GitHub and turns on **Allow public repositories** for the group. The group
must also allow every repository you want routed there. Read
GitHub's [guidance on self-hosted runners and public repositories](https://docs.github.com/en/actions/reference/security/secure-use#hardening-for-self-hosted-runners)
before you turn it on.

## Related

* [AutoPilot for CI](https://docs.costgraph.ai/costgraph/autopilot/ci.md)
* [AutoPilot for CI settings](https://docs.costgraph.ai/costgraph/autopilot/ci-settings.md)
* [Run CI jobs in sandboxes](https://docs.costgraph.ai/costgraph/autopilot/ci-sandboxes.md)
* [CostGraph operator](https://docs.costgraph.ai/costgraph/operator/overview.md)
* [Pricing IDs](https://docs.costgraph.ai/costgraph/operator/on-prem-pricing.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.