pipeline { agent { label 'bruno-docker' } options { timeout(time: 30, unit: 'MINUTES') } environment { TESTS_DIR = 'nonexistent_folder_xyz12345678_9_0' LAST_EXIT_CODE = '0' } stages { stage('Checkout') { steps { git branch: 'main', url: 'https://github.com/AlexB1960/burlaka_jenkins.git', credentialsId: 'GitTea' } } stage('Validate Directory') { steps { script { echo "DEBUG: Начальное значение LAST_EXIT_CODE: ${env.LAST_EXIT_CODE}" def status = sh( script: ''' echo "=== Проверка папки '${TESTS_DIR}' ===" ls -la if [ ! -d "${TESTS_DIR}" ]; then echo "ERROR: Directory ${TESTS_DIR} not found!" exit 4 else echo "OK: Directory exists." exit 0 fi ''', returnStatus: true ) // ЭТА СТРОКА КРИТИЧНА: она должна быть именно такой env.LAST_EXIT_CODE = status.toString() echo "DEBUG: Значение status из sh: ${status}" echo "DEBUG: LAST_EXIT_CODE установлен в: ${env.LAST_EXIT_CODE}" if (status != 0) { echo "DEBUG: Обнаружена ошибка. Вызываем error..." error "Stage failed with exit code ${status}. Папка не найдена." } } } } stage('Run Bruno Tests') { when { expression { env.LAST_EXIT_CODE == '0' } } steps { script { dir(env.TESTS_DIR) { sh 'mkdir -p reports' sh 'chmod -R 777 reports' sh 'docker run --rm -v $(pwd):/bruno -w /bruno usebruno/cli:latest run --reporter-html reports/report.html --reporter-junit reports/test-results.xml' } } } } } post { failure { script { echo "DEBUG: LAST_EXIT_CODE в post failure: ${env.LAST_EXIT_CODE}" echo "Тесты не пройдены!" if (env.LAST_EXIT_CODE != "4") { archiveArtifacts artifacts: "${env.TESTS_DIR}/reports/**", onlyIfSuccessful: false } else { echo "Код ошибки 4 (папка не найдена). Отчёты не архивируем." } } } } }