Search code examples
continuous-integrationvagrantvirtual-machinevirtualboxgithub-actions

Using Vagrant on GitHub Actions (ideally incl. VirtualBox)


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.


Solution

  • 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.