Setting Up a Self-Hosted Runner on GKE: A Step-by-Step Guide

yogesh Raiyogesh Rai
3 min read

If you're managing CI/CD pipelines with GitHub Actions and prefer more control over your environment, setting up a self-hosted runner on Google Kubernetes Engine (GKE) can be a great option. This guide will walk you through the process, from installing Cert Manager to deploying the runner, ensuring you have a reliable and secure setup.

1. Install Cert Manager

Cert Manager is a powerful Kubernetes add-on to manage and issue TLS certificates. Here’s how you can install it:

  1. Add the Cert Manager Helm Repository:

    Start by adding the Cert Manager Helm repository to your Helm setup:

     helm repo add jetstack https://charts.jetstack.io
    
  2. Update the Helm Chart Index:

    Ensure your Helm chart index is up to date:

     helm repo update
    
  3. Search for the Cert Manager Chart:

    Find the available versions of the Cert Manager Helm chart:

     helm search repo cert-manager
    
  4. Install Cert Manager:

    Install Cert Manager with the latest version, ensuring Prometheus is disabled and Custom Resource Definitions (CRDs) are installed:

     helm install \
       cert-manager jetstack/cert-manager \
       --namespace cert-manager \
       --create-namespace \
       --version v1.6.0 \
       --set prometheus.enabled=false \
       --set installCRDs=true
    
  5. Verify the Installation:

    Check that Cert Manager has been installed successfully by verifying the pods:

     kubectl get pods -n cert-manager
    

2. Set Up Authentication with GitHub API Using a GitHub App

To securely interact with the GitHub API, you'll need to authenticate using a GitHub App. Here's how to set it up:

  1. Create a GitHub App:

    • Navigate to your GitHub settings.

    • Go to "Developer settings" and click "New GitHub App."

    • Name your app with a globally unique name.

    • In the Homepage URL section, you can use the GitHub URL itself.

    • Disable the webhook option.

    • For repository access, set "Read" access to Actions and "Read & Write" access to Administration.

    • Create the GitHub App.

  2. Generate a Private Key for the App:

    In the GitHub App's general settings, generate a private key. This key will be downloaded and used later.

  3. Install the GitHub App:

    • In the "Install App" section, select the option to install the app.

    • Choose the repository you want to install the app in and complete the installation.

  4. Create a Kubernetes Secret for the GitHub App:

    Store the GitHub App credentials securely in a Kubernetes secret:

     kubectl create secret generic controller-manager \
     -n actions \
     --from-literal=github_app_id=<GITHUB_APP_ID> \
     --from-literal=github_app_installation_id=<INSTALLATION_ID> \
     --from-literal=github_app_private_key=<PRIVATE_KEY>
    

3. Deploy the Actions Controller Using Helm

Now that authentication is set up, deploy the Actions Runner Controller:

  1. Update the Helm Repo Index:

    Refresh your Helm repository index:

     helm repo update
    
  2. Add the Helm Repo:

    Search for the Actions Runner Controller Helm chart:

     helm search repo actions
    
  3. Install the Runner:

    Install the Actions Runner Controller:

     helm install runner \
     actions-runner-controller/actions-runner-controller \
     --namespace actions \
     --version 0.14.0 \
     --set syncPeriod=1m
    
  4. Verify the Runner Installation:

    Ensure the runner pods are up and running:

     kubectl get pods -n actions
    

4. Create a Self-Hosted Runner

Finally, create and deploy a self-hosted runner on GKE:

  1. Create a runner.yaml File:

    Define the runner configuration:

     apiVersion: actions.summerwind.dev/v1alpha1
     kind: Runner
     metadata:
       name: k8-single-runner
       namespace: actions
     spec:
       repository: owner/repo-name
       env: []
    
  2. Apply the Deployment:

    Deploy the runner by applying the YAML file:

     kubectl apply -f k8s/runner.yaml
    
  3. Verify the Runner Creation:

    Go to your GitHub repository's settings and check under "Actions" to see if the runner appears. The runner should have the name specified in the runner.yaml file.

Conclusion

By following these steps, you've successfully set up a self-hosted GitHub Actions runner on GKE. This setup provides you with greater flexibility and control over your CI/CD environment, ensuring that your builds and deployments are optimized for your specific needs.

0
Subscribe to my newsletter

Read articles from yogesh Rai directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

yogesh Rai
yogesh Rai