pipeline { agent any environment { SSH_CREDENTIALS = 'exceed-sleep' REMOTE_SERVER = 'ubuntu@160.187.167.106' REMOTE_WORKSPACE = '/var/www/html' GIT_CREDENTIALS = 'git-cred' BACKEND_TEST_DIR = 'Spurrin_Backend_Test' BACKEND_TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Backend_Test.git' BACKEND_TEST_ENV = '/home/ubuntu/test' } stages { stage('Add Host Key') { steps { script { sshagent(credentials: [SSH_CREDENTIALS]) { sh """ mkdir -p ~/.ssh ssh-keyscan -H ${REMOTE_SERVER.split('@')[1]} >> ~/.ssh/known_hosts """ } } } } stage('Clone & Build Frontend') { steps { script { withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) { sshagent(credentials: [SSH_CREDENTIALS]) { sh """ ssh ${REMOTE_SERVER} ' set -e cd ${REMOTE_WORKSPACE} if [ -d spurrin-dev-ui ]; then mv spurrin-dev-ui spurrin-dev-ui-prev; fi git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki/Tech4Biz-Services/spurrin-dev-ui.git cd spurrin-dev-ui export PATH=/home/ubuntu/.nvm/versions/node/v22.14.0/bin:\$PATH npm install --legacy-peer-deps --force npm run build ' """ } } } } } stage('Run Backend Test Validation') { steps { script { withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) { sshagent(credentials: [SSH_CREDENTIALS]) { sh """ ssh ${REMOTE_SERVER} ' set -e rm -rf ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR} git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@${BACKEND_TEST_REPO} ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR} cd ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR} chmod +x run_tests.sh ./run_tests.sh > backend_pytest_output.log 2>&1 || ( echo "Backend tests failed. Rolling back..." && \ cd ${REMOTE_WORKSPACE}/spurrin-dev-ui && \ git reset --hard HEAD~1 && \ exit 1 ) ' """ } } } } } stage('Copy Test Log from Remote') { when { expression { currentBuild.result == null || currentBuild.result == 'FAILURE' } } steps { script { sshagent(credentials: [SSH_CREDENTIALS]) { sh """ scp ${REMOTE_SERVER}:${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}/backend_pytest_output.log backend_pytest_output.log || echo "No backend log found." """ } } } } stage('Reload Nginx') { steps { script { sshagent(credentials: [SSH_CREDENTIALS]) { sh """ ssh ${REMOTE_SERVER} ' sudo nginx -t && sudo systemctl reload nginx ' """ } } } } } post { success { emailext( to: 'jassim.mohammed@tech4biz.io', subject: "โœ… Jenkins Deployment Successful: Spurrin UI", body: "The deployment and backend tests completed successfully on ${REMOTE_SERVER}.\n\n${env.BUILD_URL}console" ) } failure { script { def backendErrors = sh( script: "grep -E 'FAIL|ERROR|Exception|Traceback' backend_pytest_output.log || echo 'No errors found.'", returnStdout: true ).trim() emailext( to: 'jassim.mohammed@tech4biz.io', subject: "โŒ Jenkins Deployment Failed: Spurrin UI (Backend Tests)", body: """Deployment or backend testing failed on ${REMOTE_SERVER}. โš ๏ธ **Backend Test Errors**: ${backendErrors} ๐Ÿ” View full console: ${env.BUILD_URL}console """, attachmentsPattern: 'backend_pytest_output.log', attachLog: false ) } } always { echo "๐Ÿงน Cleaning Jenkins workspace..." cleanWs() } } }