Skip to content

Commit

Permalink
Add support for customizing config.vm.provider (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
hummeltech authored Oct 21, 2024
1 parent 40eb54c commit 6217cb2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 45 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ on:

jobs:
Linux:
name: ${{ matrix.box }}
name: Linux (${{ matrix.box }}) (${{ matrix.provider }})
runs-on: ubuntu-latest
strategy:
matrix:
box:
- generic/arch
provider:
- libvirt
- virtualbox
fail-fast: false
steps:
- name: Checkout code
Expand All @@ -26,6 +29,7 @@ jobs:
with:
box: ${{ matrix.box }}
cpus: ${{ env.CPUS }}
provider: ${{ matrix.provider }}

- name: Run Test (/etc/os-release) (VM)
run: |
Expand Down Expand Up @@ -66,12 +70,15 @@ jobs:
working-directory: new_working_directory

macOS:
name: ${{ matrix.box }}
name: macOS (${{ matrix.box }}) (${{ matrix.provider }})
runs-on: macos-13
strategy:
matrix:
box:
- generic/arch
provider:
- libvirt
- virtualbox
fail-fast: false
steps:
- name: Checkout code
Expand All @@ -86,6 +93,7 @@ jobs:
with:
box: ${{ matrix.box }}
cpus: ${{ env.CPUS }}
provider: ${{ matrix.provider }}

- name: Run Test (/etc/os-release) (VM)
run: |
Expand Down
110 changes: 67 additions & 43 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ inputs:
find ${{ runner.temp }} -mindepth 1 -delete;
description: Command(s) to run before packaging Vagrant Box (double quotation marks are not allowed)
type: string
provider:
description: Vagrant provider to use, defaults to libvirt on Linux and virtualbox on macOS
options:
- libvirt
- virtualbox
type: string
provision_commands:
default: >-
echo 'AcceptEnv *' >> /etc/ssh/sshd_config;
Expand Down Expand Up @@ -66,25 +72,75 @@ inputs:
runs:
using: composite
steps:
- name: Set `PROVIDER` (Linux)
run: |
echo "PROVIDER=${{ inputs.provider || 'libvirt' }}" >> ${GITHUB_ENV}
shell: bash
if: runner.os == 'Linux'

- name: Set `PROVIDER` (macOS)
run: |
echo "PROVIDER=${{ inputs.provider || 'virtualbox' }}" >> ${GITHUB_ENV}
shell: bash
if: runner.os == 'macOS'

- name: Install/Upgrade Vagrant (Linux)
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get --yes install \
vagrant
shell: bash
if: runner.os == 'Linux'

- name: Install/Configure Provider Dependencies (Linux) (libvirt)
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get --yes install \
libvirt-daemon-system \
qemu \
vagrant \
qemu
sudo setfacl -m user:$USER:rw /var/run/libvirt/libvirt-sock
shell: bash
if: runner.os == 'Linux' && env.PROVIDER == 'libvirt'

- name: Install/Configure Provider Dependencies (Linux) (virtualbox)
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get --yes install \
virtualbox
shell: bash
if: runner.os == 'Linux'
if: runner.os == 'Linux' && env.PROVIDER == 'virtualbox'

- name: Install/Upgrade Vagrant (macOS)
run: |
brew install --cask --force --quiet vagrant virtualbox
brew install --cask --force --quiet \
vagrant
shell: bash
if: runner.os == 'macOS'

- name: Install/Configure Provider Dependencies (macOS) (libvirt)
run: |
brew install --force --quiet \
libvirt \
iproute2mac \
qemu
brew services start libvirt
echo "LIBVIRT_DEFAULT_URI=qemu:///session?socket=${HOME}/.cache/libvirt/libvirt-sock" >> ${GITHUB_ENV}
shell: bash
if: runner.os == 'macOS' && env.PROVIDER == 'libvirt'

- name: Install/Configure Provider Dependencies (macOS) (virtualbox)
run: |
brew install --cask --force --quiet \
virtualbox
# Fix for Vagrant 2.4.1 with VirtualBox 7.1
sudo sed -i.bak 's/"7.0" => Version_7_0,/"7.0" => Version_7_0,"7.1" => Version_7_0,/g' $(find /opt/vagrant -name meta.rb | grep virtualbox)
shell: bash
if: runner.os == 'macOS' && env.PROVIDER == 'virtualbox'

- name: Cache Vagrant Box
if: inputs.save_box_to_cache
uses: actions/cache@v4
Expand All @@ -100,38 +156,13 @@ runs:
fi
shell: bash

- name: Install Vagrant libvirt Plugin (Linux)
- name: Install Vagrant libvirt Plugin (libvirt)
run: |
vagrant plugin install vagrant-libvirt
sudo setfacl -m user:$USER:rw /var/run/libvirt/libvirt-sock
shell: bash
if: runner.os == 'Linux'

- name: Generate Vagrantfile (Linux)
run: |
cat > Vagrantfile <<EOF
Vagrant.configure("2") do |config|
config.ssh.compression = false
config.ssh.connect_timeout = ${{ inputs.connect_timeout }}
config.ssh.forward_env = ["*"]
config.ssh.shell = "bash"
config.ssh.username = "${{ inputs.vagrant_ssh_username }}"
config.vm.boot_timeout = ${{ inputs.boot_timeout }}
config.vm.box = "${{ inputs.box }}"
config.vm.box_check_update = false
config.vm.provider :libvirt do |v|
v.cpus = ${{ inputs.cpus }}
v.driver = "kvm"
v.memory = ${{ inputs.memory }}
end
config.vm.provision "shell", inline: "${{ inputs.provision_commands }}"
config.vm.synced_folder ".", "/vagrant", disabled: true
end
EOF
shell: bash
if: runner.os == 'Linux'
if: env.PROVIDER == 'libvirt'

- name: Generate Vagrantfile (macOS)
- name: Generate Vagrantfile
run: |
cat > Vagrantfile <<EOF
Vagrant.configure("2") do |config|
Expand All @@ -143,33 +174,26 @@ runs:
config.vm.boot_timeout = ${{ inputs.boot_timeout }}
config.vm.box = "${{ inputs.box }}"
config.vm.box_check_update = false
config.vm.provider :virtualbox do |v|
config.vm.provider :${{ env.PROVIDER }} do |v|
v.cpus = ${{ inputs.cpus }}
${{ (runner.os == 'Linux' && env.PROVIDER == 'libvirt') && 'v.driver = "kvm"' || '' }}
v.memory = ${{ inputs.memory }}
end
config.vm.provision "shell", inline: "${{ inputs.provision_commands }}"
config.vm.synced_folder ".", "/vagrant", disabled: true
end
EOF
shell: bash
if: runner.os == 'macOS'

- name: Show Vagrant Status
run: |
vagrant status
shell: bash

- name: Start Vagrant Environment (Linux)
- name: Start Vagrant Environment
run: |
vagrant up --provider libvirt
vagrant up --provider ${{ env.PROVIDER }}
shell: bash
if: runner.os == 'Linux'

- name: Start Vagrant Environment (macOS)
run: |
vagrant up
shell: bash
if: runner.os == 'macOS'

- name: Save OpenSSH configuration to ~/.ssh/config
run: |
Expand Down

0 comments on commit 6217cb2

Please sign in to comment.