Skip to content

Commit

Permalink
Split out generic configuration from freebsd-vagrant-action
Browse files Browse the repository at this point in the history
  • Loading branch information
hummeltech committed Aug 2, 2024
1 parent 28c5535 commit 40eb54c
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 0 deletions.
125 changes: 125 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
name: Tests

on:
push:

jobs:
Linux:
name: ${{ matrix.box }}
runs-on: ubuntu-latest
strategy:
matrix:
box:
- generic/arch
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set `CPUS`
run: |
echo "CPUS=$(nproc)" >> ${GITHUB_ENV}
- name: Provision VM
uses: ./
with:
box: ${{ matrix.box }}
cpus: ${{ env.CPUS }}

- name: Run Test (/etc/os-release) (VM)
run: |
source /etc/os-release
if [ "${NAME}" = "Arch Linux" ]; then
echo "Arch Linux detected."
else
echo "Arch Linux not detected."
exit 1
fi
shell: bash --noprofile --norc -euo pipefail {0}

- name: Run Test (/etc/os-release) (Host)
run: |
source /etc/os-release
if [ "${NAME}" = "Arch Linux" ]; then
echo "Arch Linux detected."
exit 1
else
echo "Arch Linux not detected."
fi
shell: /bin/bash --noprofile --norc -euo pipefail {0}

- name: Prepare Test (working-directory) (VM)
run: |
mkdir new_working_directory
shell: bash --noprofile --norc -euo pipefail {0}

- name: Run Test (working-directory) (VM)
run: |
if [ "$(basename ${PWD})" = "new_working_directory" ]; then
echo "Expected working directory detected."
else
echo "Expected working directory not detected."
exit 1
fi
shell: bash --noprofile --norc -euo pipefail {0}
working-directory: new_working_directory

macOS:
name: ${{ matrix.box }}
runs-on: macos-13
strategy:
matrix:
box:
- generic/arch
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set `CPUS`
run: |
echo "CPUS=$(getconf _NPROCESSORS_ONLN)" >> ${GITHUB_ENV}
- name: Provision VM
uses: ./
with:
box: ${{ matrix.box }}
cpus: ${{ env.CPUS }}

- name: Run Test (/etc/os-release) (VM)
run: |
source /etc/os-release
if [ "${NAME}" = "Arch Linux" ]; then
echo "Arch Linux detected."
else
echo "Arch Linux not detected."
exit 1
fi
shell: bash --noprofile --norc -euo pipefail {0}

- name: Run Test (uname) (Host)
run: |
if [ "$(uname -s)" = "Darwin" ]; then
echo "Darwin detected."
else
echo "Darwin not detected."
exit 1
fi
shell: /bin/bash --noprofile --norc -euo pipefail {0}

- name: Prepare Test (working-directory) (VM)
run: |
mkdir new_working_directory
shell: bash --noprofile --norc -euo pipefail {0}

- name: Run Test (working-directory) (VM)
run: |
if [ "$(basename ${PWD})" = "new_working_directory" ]; then
echo "Expected working directory detected."
else
echo "Expected working directory not detected."
exit 1
fi
shell: bash --noprofile --norc -euo pipefail {0}
working-directory: new_working_directory
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Vagrant Action

**The value for [runs-on](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on) must either be set to `macos-13` or `ubuntu-latest` in order to use this action.**

This action allows the running of command-line programs via the `bash` shell of VMs provisioned with Vagrant using the [run](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun) keyword. This also works with composite actions which exclusively use the `run` keyword (and/or call other composite actions which also do so.)

#### *This action is currently only tested with these boxes (but will probably also work with others):*
* `generic/arch`

# Usage
<!-- start usage -->
1. Provision a `VM` using the specified `box` (with 2 CPUs & 2GB of RAM)
```yaml
- name: Provision VM
uses: hummeltech/vagrant-action@v1
with:
box: generic/arch
cpus: 2
memory: 2048
```
2. Execute a command using the `run` keyword
```yaml
- name: Display the contents of /etc/os-release
run: cat /etc/os-release
```
<!-- end usage -->
209 changes: 209 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
---
name: Start Vagrant Box
description: Allow running command-line programs via bash shell of VMs provisioned with Vagrant using the run keyword.

inputs:
box:
default: generic/arch
description: Vagrant Box
type: string
boot_timeout:
default: 1800
description: VM Boot Timeout
type: string
connect_timeout:
default: 30
description: VM SSH Connect Timeout
type: string
cpus:
default: 2
description: VM CPUs
options:
- 2
- 4
- 8
type: choice
memory:
default: 2048
description: VM Memory
options:
- 1024
- 2048
- 4096
- 8192
type: choice
pre_package_commands:
default: >-
find ${{ github.workspace }} -mindepth 1 -delete;
find ${{ runner.temp }} -mindepth 1 -delete;
description: Command(s) to run before packaging Vagrant Box (double quotation marks are not allowed)
type: string
provision_commands:
default: >-
echo 'AcceptEnv *' >> /etc/ssh/sshd_config;
mkdir -p ${{ github.workspace }};
chown vagrant:vagrant ${{ github.workspace }};
mkdir -p ${{ runner.temp }};
chown vagrant:vagrant ${{ runner.temp }};
description: Command(s) to run for provisioning (double quotation marks are not allowed)
type: string
save_box_to_cache:
description: Save Vagrant Box to GitHub Actions Cache
type: boolean
use_cached_box:
description: Use Vagrant Box from GitHub Actions Cache
type: boolean
vagrant_box_descriptor:
default: /tmp/packaged.box
description: Vagrant Box Descriptor <name, url, or path>
type: string
vagrant_ssh_username:
default: vagrant
description: SSH Username for `vagrant ssh`
type: string


runs:
using: composite
steps:
- name: Install/Upgrade Vagrant (Linux)
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get --yes install \
libvirt-daemon-system \
qemu \
vagrant \
virtualbox
shell: bash
if: runner.os == 'Linux'

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

- name: Cache Vagrant Box
if: inputs.save_box_to_cache
uses: actions/cache@v4
with:
path: ${{ inputs.vagrant_box_descriptor }}
key: ${{ inputs.box }}-${{ runner.os }}.box

- name: Add Vagrant Box
if: inputs.use_cached_box
run: |
if [ -f ${{ inputs.vagrant_box_descriptor }} ]; then
vagrant box add --name ${{ inputs.box }} ${{ inputs.vagrant_box_descriptor }}
fi
shell: bash

- name: Install Vagrant libvirt Plugin (Linux)
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'

- name: Generate Vagrantfile (macOS)
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 :virtualbox do |v|
v.cpus = ${{ inputs.cpus }}
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)
run: |
vagrant up --provider libvirt
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: |
mkdir -p ~/.ssh
vagrant ssh-config --host vagrantbox >> ~/.ssh/config
shell: bash

- name: Generate /bin/bash override script
run: |
echo "#!/bin/bash" | sudo tee /usr/local/bin/bash
echo "SCRIPT=\$(mktemp)" | sudo tee -a /usr/local/bin/bash
echo "echo \"#!/usr/bin/env bash\" >> \${SCRIPT}" | sudo tee -a /usr/local/bin/bash
echo "echo \"cd \${PWD}\" >> \${SCRIPT}" | sudo tee -a /usr/local/bin/bash
echo "echo \"set -euxo pipefail\" >> \${SCRIPT}" | sudo tee -a /usr/local/bin/bash
echo "echo \"bash \$@\" >> \${SCRIPT}" | sudo tee -a /usr/local/bin/bash
echo "mv \${SCRIPT} ${{ runner.temp }}/command.sh" | sudo tee -a /usr/local/bin/bash
echo "chmod a+x ${{ runner.temp }}/command.sh" | sudo tee -a /usr/local/bin/bash
echo "rsync --archive --delete ${{ github.workspace }}/ vagrantbox:${{ github.workspace }}/" | sudo tee -a /usr/local/bin/bash
echo "rsync --archive --delete ${{ runner.temp }}/ vagrantbox:${{ runner.temp }}/" | sudo tee -a /usr/local/bin/bash
echo "vagrant ssh --command \"${{ runner.temp }}/command.sh\"" | sudo tee -a /usr/local/bin/bash
echo "EXIT_STATUS=\$?" | sudo tee -a /usr/local/bin/bash
echo "rsync --archive --delete vagrantbox:${{ github.workspace }}/ ${{ github.workspace }}/" | sudo tee -a /usr/local/bin/bash
echo "rsync --archive --delete vagrantbox:${{ runner.temp }}/ ${{ runner.temp }}/" | sudo tee -a /usr/local/bin/bash
echo "exit \${EXIT_STATUS}" | sudo tee -a /usr/local/bin/bash
sudo chmod +x /usr/local/bin/bash
shell: bash

- name: Package Vagrant Box
if: inputs.save_box_to_cache
uses: gacts/run-and-post-run@v1
with:
post: >-
if [ ! -f ${{ inputs.vagrant_box_descriptor }} ]; then
vagrant ssh --command "${{ inputs.pre_package_commands }}";
vagrant package --output ${{ inputs.vagrant_box_descriptor }};
fi
post-shell: /bin/bash

0 comments on commit 40eb54c

Please sign in to comment.