Files
ble_api/api/Jenkinsfile.ble_test
T

180 lines
8.2 KiB
Plaintext

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.6.0', description: 'IP of the host that runs the BLE test')
string(name: 'test_count', defaultValue: '25', description: 'Number of test iterations')
}
// ---------------------------------------------------------------------
// Stages
// ---------------------------------------------------------------------
stages {
// ---------------------------------------------------------------------
// 1. Build a unique screen session name, disable MOTD
// ---------------------------------------------------------------------
stage('Prepare Test Environment') {
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)}"
echo "==MC== Test Hash - ${env.TEST_HASH}"
echo "==MC== Disabling MOTD..."
sh """
cd /tmp
touch .hushlogin
scp -o StrictHostKeyChecking=no -i /var/jenkins_home/jenkins_key .hushlogin root@${params.host_ip}:/root/.hushlogin
rm .hushlogin
"""
}
}
}
// ---------------------------------------------------------------------
// 2. Ensure apps are installed
// ---------------------------------------------------------------------
stage('Ensure apps are present') {
steps {
sshagent([env.SSH_CREDENTIALS_ID]) {
sh """
ssh -q -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF
echo ==MC== Checking for screen application...
if ! command -v screen > /dev/null; then
echo ==MC== "screen not found - installing"
apt-get update && apt-get install -y screen
fi
echo ==MC== Checking for rfkill application...
if ! command -v rfkill > /dev/null; then
echo ==MC== "rfkill not found - installing"
apt-get update && apt-get install -y rfkill
fi
"""
}
}
}
// ---------------------------------------------------------------------
// 3. Run the BLE test inside a detached screen session
// ---------------------------------------------------------------------
stage('Start BLE Test') {
steps {
sshagent([env.SSH_CREDENTIALS_ID]) {
sh """
ssh -q -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF
echo ==MC== Initiating BLE test in Screen session ble_test_${env.TEST_HASH}
if [ ! -d /opt/ble-test/ble_api/.git ]; then
echo ==MC== Directory /opt/ble-test/ble_api missing - creating & cloning repo...
# Create parent folder if necessary
mkdir -p /opt/ble-test
# Pull the latest copy of the repo
git clone https://gitea.matt-cloud.com/matt/ble_api.git /opt/ble-test/ble_api
else
echo ==MC== Pulling code changes from Gitea...
cd /opt/ble-test/ble_api
git pull
fi
sleep 2
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"
echo ==MC== BLE Test running...
"""
}
}
}
stage('Initiate Tail Watchdog'){
steps {
sshagent([env.SSH_CREDENTIALS_ID]) {
sh """
ssh -q -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF
echo ==MC== Initiating Tail Watchdog
screen -dmS ble_test_watchdog_${env.TEST_HASH} sh -c '
while ! pgrep -f "tail.*ble_test_${env.TEST_HASH}" > /dev/null; do
sleep 1
done
while true; do
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
done
exit
'
echo ==MC== Tail watchdog running in screen session ble_test_watchdog_${env.TEST_HASH}
"""
}
}
}
stage('BLE Test Execution'){
steps {
sshagent([env.SSH_CREDENTIALS_ID]) {
sh """
ssh -q -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF
echo ==MC== Test is running, monitoring output.
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 -q -o StrictHostKeyChecking=no root@${params.host_ip} -o ServerAliveInterval=60 -o ServerAliveCountMax=120 << EOF
echo ==MC== Test complete, cleaning up Screen sessions
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
if [ -f /tmp/ble_test_${env.TEST_HASH}.log ]; then
echo ==MC== Removing test log
rm /tmp/ble_test_${env.TEST_HASH}.log
fi
if [ -f /root/.hushlogin ]; then
echo ==MC== Re-enabling MOTD
rm /root/.hushlogin
fi
echo ==MC== Clearing ssh key for this host
ssh-keygen -f '/root/.ssh/known_hosts' -R '${params.host_ip}'
"""
}
}
}
}