Skip to content

Self-Hosted Runners in GitHub Actions, Customizing Your CI/CD Environment

Updated: at 11:12 AM (4 min read)

Table of Contents

Open Table of Contents

Introduction

GitHub Actions provides powerful automation capabilities for your software development workflows. While GitHub-hosted runners are sufficient for most cases, there are scenarios where you might need more control over the environment in which your jobs run. This is where self-hosted runners come into play.

In this article, we’ll explore:

1. Understanding GitHub Runners

What is a Runner?

A runner is an application that listens for available jobs, runs them, and reports the results back to GitHub. It can be hosted by GitHub or self-hosted by you.

2. Why Use Self-Hosted Runners?

While GitHub-hosted runners are suitable for most workflows, there are scenarios where self-hosted runners provide advantages.

2.1 Hardware Customization

2.2 Operating System Choices

2.3 Custom Software and Tools

2.4 Persistent Environment

3. Setting Up a Self-Hosted Runner

3.1 Prerequisites

3.2 Configuring the Runner

Step 1: Access Repository Settings

Step 2: Add a New Self-Hosted Runner

Step 3: Install the Runner Application

SSH into your machine and execute the commands provided by GitHub:

# Download the latest runner package
$ curl -o actions-runner-linux-x64-2.296.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.296.1/actions-runner-linux-x64-2.296.1.tar.gz

# Extract the installer
$ tar xzf ./actions-runner-linux-x64-2.296.1.tar.gz

# Configure the runner
$ ./config.sh --url https://github.com/your-username/your-repo --token YOUR_RUNNER_TOKEN

Replace https://github.com/your-username/your-repo with your repository URL. Replace YOUR_RUNNER_TOKEN with the token provided.

Step 4: Start the Runner

$ ./run.sh
# Install the service
$ sudo ./svc.sh install

# Start the service
$ sudo ./svc.sh start

4. Using Self-Hosted Runners in Workflows

To assign jobs to your self-hosted runner, update the runs-on field in your workflow YAML file.

Basic Example

jobs:
  build:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v3
      - name: Build Project
        run: make build

Using Labels

If you have multiple self-hosted runners with different capabilities, you can assign labels during configuration and target them in your workflows.

Assigning Labels During Configuration

When configuring your runner, you’ll be prompted to enter labels:

Enter any additional labels (ex: label-1,label-2):

# For example, enter:
custom-runner,high-memory

Using Labels in Workflow

jobs:
  build:
    runs-on: [self-hosted, custom-runner, high-memory]
    steps:
      - uses: actions/checkout@v3
      - name: Build Project
        run: make build

5. Security Considerations

Using self-hosted runners introduces security risks that you need to manage.

Key Points

Read more: Refer to GitHub’s Security hardening for GitHub Actions for detailed guidelines.

6. Conclusion

Self-hosted runners offer flexibility and control over your CI/CD environment in GitHub Actions. By customizing hardware, operating systems, and software, you can tailor the execution environment to meet your specific needs.

Benefits

Considerations

By understanding how to set up and use self-hosted runners securely, you can enhance your workflows and better meet the demands of your projects.