Files
ble_api/api/Jenkinsfile.ble_test
T

112 lines
4.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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"
"""
}
}
}
stage('View Test Logs'){
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
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
"""
}
}
}
}