Jenkins Pipeline for Scheduled Test Runs

Jenkins automates the executions of my test scripts: frontend, backend, data checks and even installation tests on virtual machines. I use it when the work needs a self-hosted runner, access to an internal network or a desktop environment that a cloud runner cannot provide. For cloud-native pipelines I use GitHub Actions.

The declarative pipeline below provisions a virtual machine with Vagrant, installs and runs the application, verifies the result in Firestore and removes the machine whatever the outcome. It runs on a schedule, retries a flaky provisioning step and exports its logs for every build.

pipeline {
    agent any
    options { 
        timestamps()
        preserveStashes(buildCount: 5)
        timeout(time: 1, unit: 'HOURS')
    }
    triggers {
        cron('* H/12 * * *')
    }
    environment {
        DATE = "${new Date().format('yyyyMMdd')}"
    }
    stages {
        stage('Install - RHEL 7') {
            options { 
                retry(2) 
            }
            steps {
                script {
                    bat """
                        cd C:/Users/antonio/Vagrant/VirtualMachine
                        vagrant up redhat7
                        vagrant ssh redhat7
                        vagrant ssh redhat7 -c "cat /etc/os-release"
                        vagrant ssh redhat7 -c "uname -a"
                        vagrant ssh redhat7 -c "cp /home/vagrant/shared/executable.run ."
                        vagrant ssh redhat7 -c "chmod 700 executable.run"
                        vagrant ssh redhat7 -c "./executable.run"
                        vagrant ssh redhat7 -c "./executable.run"
                        vagrant ssh redhat7 -c "cat /home/vagrant/field/user.inf > code.txt"
                        vagrant ssh redhat7 -c "sed -n '3p' code.txt | cut -c 6,7,8,9,10,11 >> codeUP.txt"
                        vagrant ssh redhat7 -c "cp /home/vagrant/codeUP.txt /home/vagrant/shared"
                        vagrant ssh redhat7 -c "sudo /opt/CrowdStrike/falconctl -g --aid | cut -c 6-37 > csid.txt"
                        vagrant ssh redhat7 -c "cp /home/vagrant/csid.txt /home/vagrant/shared"
                        vagrant destroy redhat7 -f
                    """
                }
            }
        }
        stage ('Verify on Firestore') {
            steps {
                script {
                    bat """ 
                        cd C:/Users/antonio/Vagrant/VirtualMachine
                        python -m verify_csid.py
                    """
                }
            }
        }
    }
    post {
        always {
            echo 'Deleting VM in VBox...'
            script {
                bat """
                    cd C:/Program Files/Oracle/VirtualBox
                    VBoxManage unregistervm box-redhat7 --delete
                    VBoxManage list vms
                """
            }
            echo 'Export LOGs'
            bat "mkdir C:\\Users\\antonio\\Documents\\${env.JOB_NAME}_${env.BUILD_NUMBER}_${DATE}"
            bat "COPY C:\\ProgramData\\Jenkins\\.jenkins\\jobs\\variables\\builds\\${env.BUILD_ID}\\log C:\\Users\\antonio\\Documents\\${env.JOB_NAME}_${env.BUILD_NUMBER}_${DATE}.txt"
        }
        regression {
            echo 'Showing VMs running on VBox & Vagrant...'
            script {
                bat """
                    cd C:/Program Files/Oracle/VirtualBox
                    VBoxManage list vms
                    cd C:/Users/antonio/Vagrant/VirtualMachine
                    vagrant status
                    vagrant global-status
                """
            }
        }
        success {
            echo 'RUN RedHat 7 => OK'
        }
        cleanup {
            echo 'Deleting code & csid .TXT'
            script {
                bat """
                    cd C:/Users/antonio/Vagrant/VirtualMachine
                    Del /F /A code.txt
                    Del /F /A csid.txt
                """
            }
        }
    }
}
  • Safety nets in options: a one-hour timeout, build timestamps and a limit on preserved stashes, so a hung build never blocks the agent.
  • A cron trigger runs the job unattended on a schedule.
  • retry(2) on the provisioning stage absorbs the occasional virtual machine start-up failure without hiding real defects, because the checks themselves are not retried.
  • A second stage verifies the outcome in the database (see data quality testing), because an installer that exits cleanly can still leave bad data behind.
  • The post section always runs: it deletes the virtual machine, exports the build log to a dated folder and cleans temporary files. Test environments must be disposable and leave nothing behind.