Start GitHub Actions self-hosted runner with VirtualBox and Vagrant

How to launch GitHub Actions self-hosted runner on Ubuntu VM with VirtualBox and Vagrant.

Lastmod: 2021-07-11

View Source Edit
How to launch GitHub Actions self-hosted runner on Ubuntu VM with VirtualBox and Vagrant.

Table of Contents

Overview

  • Start GitHub Actions self-hosted runner with VirtualBox and Vagrant on your local machine or a remote machine like Google Compute Engine and EC2.
  • Avoid dind (Docker in Docker)
  • Accelerate docker build (or docker-compose build) on the virtual machine with the Docker Layer Caching as the same as your local environment.
  • Host OS: Ubuntu 18.04/20.04, macOS, Windows
  • Guest OS: Ubuntu 20.04

Install VirtualBox

Oracle VM VirtualBox

Ubuntu 18.04/20.04

sudo apt install -y virtualbox

macOS

brew cask install virtualbox

Allow Oracle America, Inc. in the System Preferences.

Allow Oracle America, Inc. in the macOS System Preferences.
Allow Oracle America, Inc. in the macOS System Preferences.

Install Vagrant

Ubuntu 18.04/20.04

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt update && sudo apt install -y vagrant
vagrant autocomplete install

macOS

brew install vagrant
vagrant autocomplete install

Start VM

git clone https://github.com/peaceiris/actions-self-hosted-runners.git
cd ./actions-self-hosted-runners/images/ubuntu-20.04
git checkout v0.3.2
vim .env
make up

Create .env file as follows.

VB_CPUS = '4'
VB_MEMORY = '8192'
VB_DISK_SIZE = '30GB'
GHA_RUNNER_URL = 'https://github.com/peaceiris/repo'
GHA_RUNNER_TOKEN = 'token_here'
  • VB_CPUS: CPU cores
  • VB_MEMORY: Memory size
  • VB_DISK_SIZE: Disk size
  • GHA_RUNNER_URL: A URL of an organization or repository to register a self-hosted runner.
  • GHA_RUNNER_TOKEN: A token generated in your settings page.

Launch GitHub Actions self-hosted runner on Ubuntu 20.04 virtual machine
Launch GitHub Actions self-hosted runner on Ubuntu 20.04 virtual machine

Use the VM and Accelerate docker build

Unlike the GitHub hosted runner, a self-hosted runner is not destroyed for each job, so Docker Layer Caching will work as well as local machine. You don’t have to worry about tricky usage of the actions/cache and capacity limits. The docker build job became accelerated and we achieved the purpose.

name: CI frontend

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  docker:
    runs-on: self-hosted
    timeout-minutes: 3
    env:
      COMPOSE_DOCKER_CLI_BUILD: 1
      DOCKER_BUILDKIT: 1
    steps:
      - uses: actions/checkout@v2.3.4
      - run: hadolint ./Dockerfile
      - run: sudo docker-compose build frontend

A docker-compose build job that took 50s on GitHub hosted runner
A docker-compose build job that took 50s on GitHub hosted runner

A job of docker-compose build that became explosive due to Docker Layer Caching in self-hosted runner
A job of docker-compose build that became explosive due to Docker Layer Caching in self-hosted runner

Enabled Docker Layer Caching on a GitHub Actions self-hosted runner
Enabled Docker Layer Caching on a GitHub Actions self-hosted runner

Repository

Conclusion

  • We can start a self-hosted GitHub Actions runner on a Ubuntu 20.04 virtual machine with VirtualBox and Vagrant very easily.
  • We can accelerate docker build (or docker-compose build) on the virtual machine with the Docker Layer Caching as the same as your local environment.

Enjoy the comfortable CI/CD! Thank you.


Random Recommended Posts