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 = "ble_test_${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('Run 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" """ } } } //while screen -list | grep -q ble_test_${env.TEST_HASH}; do // tail -f /tmp/ble_test_${env.TEST_HASH}.log //done stage('View Test Logs'){ steps { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ #!/usr/bin/env bash ssh -o StrictHostKeyChecking=no root@${params.host_ip} << EOF screen -list | grep -q "ble_test_${TEST_HASH}" EOF """ } } } } //sh """ //ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF // set -euo pipefail // # Outer loop - runs as long as the screen session is alive // while screen -list | grep -q "ble_test_${TEST_HASH}"; do // # `tail -f` streams the file; the read-loop ensures line-by-line flush // tail -n +1 -f /tmp/ble_test_${env.TEST_HASH}.log | while IFS= read -r line; do // echo "\$line" // done // done //""" // --------------------------------------------------------------------- // Post-build cleanup - kill the remote screen session // --------------------------------------------------------------------- post { always { sshagent([env.SSH_CREDENTIALS_ID]) { sh """ ssh -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF if screen -list | grep -q ${env.TEST_HASH}; then screen -S ble_test_${env.TEST_HASH} -X quit fi rm /tmp/ble_test_${env.TEST_HASH}.log """ } } } }