Skip to main content

Integrating with CI/CD

1. Using Vagrant in Jenkins

Property Description Example
Vagrant Box Base image used for VM creation vagrant box add ubuntu/focal64
Vagrantfile Configuration file defining VM setup Vagrant.configure("2") { |config| config.vm.box = "ubuntu/focal64" }
Jenkins Plugin Plugin to manage Vagrant VMs in CI vagrant-plugin

Example: Jenkins Pipeline with Vagrant

pipeline {
    agent any
    stages {
        stage('Setup Vagrant') {
            steps {
                sh 'vagrant up'
            }
        }
        stage('Run Tests') {
            steps {
                sh 'vagrant ssh -c "cd /app && npm test"'
            }
        }
    }
}
Note: Ensure Vagrant is installed on Jenkins agent and VMs are properly configured for SSH access.

2. Configuring GitLab CI Integration

Option Values Notes
image ubuntu:20.04 Base image for the GitLab CI runner
services postgres:13 Database service to run alongside the job
before_script apt-get update && apt-get install -y vagrant Commands to run before each job
script vagrant up && vagrant provision Commands to execute in the CI job

Example: GitLab CI Configuration

stages:
  - setup
  - test

setup_job:
  stage: setup
  image: ubuntu:20.04
  services:
    - postgres:13
  before_script:
    - apt-get update && apt-get install -y vagrant
  script:
    - vagrant up
    - vagrant provision
Note: Ensure Vagrant is installed in the CI environment and that required plugins are available.
Warning: GitLab CI runners must have sufficient resources to handle Vagrant VMs.

3. Using GitHub Actions with Vagrant

Step Description Action
1 Set up Vagrant environment in GitHub Actions workflow uses: actions/checkout@v4
2 Install Vagrant on the runner run: sudo apt-get install vagrant
3 Initialize Vagrant environment run: vagrant up
4 Run tests or deployments inside Vagrant box run: vagrant ssh -c "npm test"

Example: GitHub Actions Workflow

name: Vagrant CI
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Vagrant
        run: |
          sudo apt-get update
          sudo apt-get install -y vagrant
      - name: Start Vagrant Box
        run: vagrant up
      - name: Run Tests
        run: vagrant ssh -c "npm test"
Note: Ensure Vagrantfile is committed to the repository for GitHub Actions to access it.
Warning: Vagrant boxes may require additional setup for CI environments, such as disabling GUI or setting up SSH keys.

4. Setting Up CircleCI Jobs

Example: Basic CircleCI Configuration

version: 2.1
jobs:
  vagrant-test:
    docker:
      - image: circleci/ruby:2.6
    steps:
      - checkout
      - run:
          name: Install Vagrant
          command: |
            sudo apt-get update
            sudo apt-get install -y vagrant
      - run:
          name: Run Vagrant Commands
          command: |
            vagrant up
            vagrant ssh -c "echo 'Hello from Vagrant'"
workflows:
  version: 2
  test-and-deploy:
    jobs:
      - vagrant-test
Property Description Notes
version CircleCI configuration version Must be 2.1 for using orbs and complex workflows
jobs Defines the jobs to run in the workflow Each job can have multiple steps
docker Specifies Docker image to use for job execution Use an image with Vagrant installed or install it in a step
steps List of commands to execute in the job Each step can be a command or a predefined action
Note: Ensure that the Docker image used has Vagrant installed or include a step to install it.
Warning: CircleCI does not support running Vagrant directly on its hosted runners due to virtualization limitations. Consider using a custom Docker image or alternative CI services that support nested virtualization.

5. Configuring Travis CI

Option Values Notes
language ruby Required for Travis CI to run Vagrant commands
services docker Enables Docker support for Vagrant box provisioning
before_install sudo apt-get update Updates system packages before installing Vagrant
install sudo apt-get install vagrant Installs Vagrant from package manager
script vagrant up Starts the Vagrant environment

Example: Travis CI Configuration

language: ruby
services:
  - docker
before_install:
  - sudo apt-get update
install:
  - sudo apt-get install vagrant
script:
  - vagrant up
Note: Ensure that your Vagrantfile is committed to the repository for Travis CI to access it.
Warning: Travis CI has limited support for GUI applications; consider using headless mode for testing.

6. Implementing Azure Pipelines

Step Description Action
1 Create a Vagrantfile with provisioners for Azure pipeline
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  config.vm.provision :shell, path: "scripts/provision.sh"
end
2 Define Azure Pipeline YAML configuration
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      vagrant up
      vagrant provision
3 Set up Vagrant Cloud integration (optional) vagrant cloud publish <username>/<box-name> <version>
Note: Ensure Vagrant is installed in the Azure Pipeline agent environment.
Warning: Vagrant VMs may not persist state across pipeline runs. Use ephemeral environments for CI.

7. Using Vagrant in Docker Containers

Feature Description Notes
Vagrant + Docker Run Vagrant inside a Docker container to isolate development environments. Requires Docker daemon access from within the container.
Limitations Some Vagrant plugins may not work due to container constraints. Network and filesystem access can be limited or require special configuration.

Example: Running Vagrant in Docker

docker run -it --privileged -v /var/run/docker.sock:/var/run/docker.sock hashicorp/vagrant
Note: The --privileged flag is often required to allow Vagrant to interact with Docker.
Warning: Vagrant may not fully support all Docker container configurations. Ensure compatibility with your CI/CD pipeline.

8. Caching Vagrant Boxes

Cache Type Description Implementation Notes
box.cache.enable Enables caching of Vagrant boxes vagrant box cache enable <box-name> Available in Vagrant 2.2.19+
box.cache.disable Disables caching of Vagrant boxes vagrant box cache disable <box-name> Available in Vagrant 2.2.19+
box.cache.list Lists all cached boxes vagrant box cache list Available in Vagrant 2.2.19+

Example: Enabling Box Caching

vagrant box cache enable ubuntu/focal64

Example: Listing Cached Boxes

vagrant box cache list
Note: Caching is particularly useful in CI environments to reduce download times and improve build performance.

9. Implementing Parallel Test Execution

Example: Running Tests in Parallel with Vagrant

vagrant up
vagrant ssh -c "cd /vagrant && npm run test:parallel"
Command Description Notes
vagrant up Starts all Vagrant machines Ensures test environment is ready
vagrant ssh -c Executes command in VM Allows running tests in isolated environment
Note: Parallel test execution requires proper configuration of test runners to avoid resource contention.

Best Practices for Parallel Testing

  • Use unique test databases per worker process
  • Ensure isolated file systems for each test instance
  • Configure CI/CD to report results from all parallel runners

10. Configuring Cleanup Strategies

Strategy Description Configuration Notes
vagrant destroy Destroys the VM and all associated resources vagrant destroy -f Use with caution; irreversible operation
vagrant halt Stops the VM gracefully vagrant halt VM state preserved, can be resumed later
vagrant suspend Powers down the VM but saves its state vagrant suspend Fast resume, preserves memory state
vagrant up --provision Recreates VM and runs provisioners vagrant up --provision Useful for clean state testing in CI

Example: CI Pipeline Cleanup

#!/bin/bash
# Cleanup strategy in CI pipeline
vagrant halt || true
vagrant destroy -f || true
Note: Always ensure cleanup scripts are idempotent to avoid errors in CI environments.

Was this article helpful?