Start GitHub Actions self-hosted runner with VirtualBox and Vagrant

peaceiris

peaceiris

Posted on June 30, 2021

Start GitHub Actions self-hosted runner with VirtualBox and Vagrant

Start GitHub Actions self-hosted runner with VirtualBox and Vagrant


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


Enter fullscreen mode Exit fullscreen mode

macOS



brew cask install virtualbox


Enter fullscreen mode Exit fullscreen mode

Allow Oracle America, Inc. in the System Preferences.

Allow Oracle America, Inc.


Install Vagrant

hashicorp/vagrant: Vagrant is a tool for building and distributing development environments.

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


Enter fullscreen mode Exit fullscreen mode

macOS



brew install vagrant
vagrant autocomplete install


Enter fullscreen mode Exit fullscreen mode

Start VM

peaceiris/actions-self-hosted-runners - GitHub



git clone https://github.com/peaceiris/actions-self-hosted-runners.git
cd ./actions-self-hosted-runners/images/ubuntu-20.04
git checkout v0.4.0
vim .env
make up
vagrant ssh
cd actions-runner
./config.sh --url https://github.com/[owner]/[repo] --token [token]
nohup ./run.sh &


Enter fullscreen mode Exit fullscreen mode

Create .env file as follows.



VB_CPUS = '4'
VB_MEMORY = '8192'
VB_DISK_SIZE = '30GB'


Enter fullscreen mode Exit fullscreen mode
  • VB_CPUS: CPU cores
  • VB_MEMORY: Memory size
  • VB_DISK_SIZE: Disk size

Screen Shot 2021-06-30 at 12.45.44


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


Enter fullscreen mode Exit fullscreen mode

Slow docker-compose build job on the GitHub hosted runner.

Slow docker-compose build job on the GitHub hosted runner

Accelerated docker-compose build job on a self-hosted GitHub Actions runner.

Accelerated docker-compose build job on a self-hosted GitHub Actions runner

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

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


Repository

GitHub logo peaceiris / actions-self-hosted-runners

GitHub Actions self-hosted runner on VirtualBox with Vagrant.

actions-self-hosted-runners

CI

GitHub Actions self-hosted runner on VirtualBox with Vagrant.

Getting Started

git clone https://github.com/peaceiris/actions-self-hosted-runners.git
cd ./actions-self-hosted-runners/images/ubuntu
cat << EOF > .env
GHA_RUNNER_VERSION = 'x.xxx.x'
VB_CPUS = '4'
VB_MEMORY = '8192'
VB_DISK_SIZE = '30GB'
EOF
make up
vagrant ssh
cd actions-runner
./config.sh --url https://github.com/[owner]/[repo] --token [token]
nohup ./run.sh &
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
peaceiris
peaceiris

Posted on June 30, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related