pipeline { agent any // ------------------------------------------------------------------ // Global environment variables - change as needed // ------------------------------------------------------------------ environment { DEBIAN_IP = 'debian_machine_ip' SSH_CREDENTIALS_ID = 'jenkins_root_ssh' } // ------------------------------------------------------------------ // Build parameters (visible to the user) // ------------------------------------------------------------------ parameters { string(name: 'host_ip', defaultValue: '172.20.20.51', description: 'IP of the host that runs the BLE test') string(name: 'test_count', defaultValue: '25', description: 'Number of test iterations') string(name: 'BT_USB_ID', defaultValue: '0bda:c821', description: 'USB ID of the BLE dongle') } // --------------------------------------------------------------------- // Stages // --------------------------------------------------------------------- stages { // --------------------------------------------------------------------- // 1. Build a unique screen session name // --------------------------------------------------------------------- stage('Prepare Test Hash') { steps { script { // epoch seconds as a plain long long epochSec = System.currentTimeMillis() / 1000L // keep only the last 6 digits → guarantees uniqueness env.TEST_HASH = "${String.format('%06d', epochSec % 1_000_000)}" sh "echo Test Hash - ${env.TEST_HASH}" } } } // --------------------------------------------------------------------- // 2. Ensure screen is installed (runs only once) // --------------------------------------------------------------------- stage('Ensure screen is present') { steps { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF if ! command -v screen > /dev/null; then echo "screen not found - installing" apt-get update && apt-get install -y screen fi """ } } } // --------------------------------------------------------------------- // 3. Run the BLE test inside a detached screen session // --------------------------------------------------------------------- stage('Start BLE Test') { steps { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF usbreset ${params.BT_USB_ID} sleep 5 cd /opt/ble-test/ble_api git pull cd /opt/ble-test/ble_api/api screen -dmS ble_test_${env.TEST_HASH} bash -c "cd /opt/ble-test/ble_api/api && python3 -u ./ble.py ${params.test_count} | tee /tmp/ble_test_${env.TEST_HASH}.log" """ } } } stage('Initiate Tail Watchdog'){ steps { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF echo === Initiating Tail Watchdog === screen -dmS ble_test_watchdog_${env.TEST_HASH} sh -c ' sleep 15 while pgrep -f "tail.*ble_test_${env.TEST_HASH}" > /dev/null; do if ! screen -list | grep -q ble_test_${env.TEST_HASH}; then pkill -9 -f "tail.*ble_test_${env.TEST_HASH}" fi sleep 5 done exit ' """ } } } stage('BLE Test Execution'){ steps { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF while screen -list | grep -q ble_test_${env.TEST_HASH}; do tail -f /tmp/ble_test_${env.TEST_HASH}.log || true done """ } } } } // --------------------------------------------------------------------- // Post-build cleanup - kill the remote screen session // screen -list | grep ble_test_watchdog_${env.TEST_HASH} | xargs | cut -d. -f1 // screen -list | grep ble_test_${env.TEST_HASH} | xargs | cut -d. -f1 // --------------------------------------------------------------------- post { always { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF screen -wipe if screen -list | grep -q ${env.TEST_HASH}; then screen -S ble_test_watchdog_${env.TEST_HASH} -X quit screen -S ble_test_${env.TEST_HASH} -X quit fi rm /tmp/ble_test_${env.TEST_HASH}.log """ } } } }