5

I know from this answer that it's possible to spin up and run Vagrant Boxes on TravisCI using libvirt instead of VirtualBox.

Is that also possible with GitHub Actions? Since we're moving everything away from TravisCI because of the new pricing model, we also need to switch our Vagrant-based test cases.

jonashackt
  • 12,022
  • 5
  • 67
  • 124

1 Answers1

7

Yes, this is possible using the macos-10.15 (which is currently the macos-latest) environment. You could also try to go with macos-11.0, but there currently seems to be no stable VirtualBox release for Big Sur. I created a small example project at https://github.com/jonashackt/vagrant-github-actions

Assume you have a Vagrantfile inside your repo like this:

Vagrant.configure("2") do |config|
    config.vm.box = "generic/ubuntu1804"

    config.vm.define 'ubuntu'

    # Prevent SharedFoldersEnableSymlinksCreate errors
    config.vm.synced_folder ".", "/vagrant", disabled: true
end

Then you add a GitHub Action workflow like vagrant-up.yml inside your .github/workflows folder like this:

name: vagrant-up

on: [push]

jobs:
  vagrant-up:
    runs-on: macos-10.15

    steps:
    - uses: actions/checkout@v2

    - name: Cache Vagrant boxes
      uses: actions/cache@v2
      with:
        path: ~/.vagrant.d/boxes
        key: ${{ runner.os }}-vagrant-${{ hashFiles('Vagrantfile') }}
        restore-keys: |
          ${{ runner.os }}-vagrant-

    - name: Run vagrant up
      run: vagrant up

    - name: ssh into box after boot
      run: vagrant ssh -c "echo 'hello world!'"

You could even leave out the caching action - I simply added it here to show what's possible. It'll save you some seconds, which depends on the VagrantBox you're using.

The GitHub Actions implementation for running Vagrant is much easier then the TravisCI counterpart, because you don't need to install Vagrant or VirtualBox - and you also don't need to switch to libvirt. Just use the box you want from https://app.vagrantup.com/boxes/search which is pretty cool.

jonashackt
  • 12,022
  • 5
  • 67
  • 124
  • 1
    I get `vagrant: command not found` in November of 2021 on `macos-latest`. – a2f0 Nov 01 '21 at 06:31
  • According to [the GitHub Actions Virtual Environment docs for `macos-latest`](https://github.com/actions/virtual-environments/blob/main/images/macos/macos-10.15-Readme.md) (which is macOS 10.15 today, not BigSur) `vagrant` should be still available. – jonashackt Nov 08 '21 at 12:22
  • this is a good point but here's an example workflow run where it's not available on macos latest (this used to work, might be a pathing issue, not sure). Can see the workflow action syntax since it's public. https://github.com/a2f0/dotfiles/runs/4184671229?check_suite_focus=true – a2f0 Nov 12 '21 at 05:52
  • 1
    In my example project there's also an ongoing discussion about macOS 11 Vagrant support https://github.com/jonashackt/vagrant-github-actions/issues/2 – jonashackt Nov 12 '21 at 09:18
  • Hi @jonashackt is there any better way today ? or is your repo still the reference for such usage of Vagrant ? In order to use Vagrant for Linux, we still have to use `macos` systems on GitHub Actions ? – AkechiShiro May 19 '23 at 20:01
  • I've found this issue describing that now it was possible to use nested virtualization under Linux/Windows GitHub Actions images, but I haven't given it a try myself : https://github.com/actions/runner-images/issues/183 EDIT: Actually, it is only available for large runner and not normal runner so it's not free I believe. EDIT 2: A workaround would be to use this : https://actuated.dev/blog/kvm-in-github-actions – AkechiShiro May 19 '23 at 21:03