From f7dcecba8bdf0dc8a6bb2205b4ac74e14f71c3e8 Mon Sep 17 00:00:00 2001 From: jassim Date: Thu, 12 Jun 2025 14:37:46 +0000 Subject: [PATCH] Update Jenkinsfile --- Jenkinsfile | 73 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 448536b..ebdc50b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,21 +1,31 @@ pipeline { agent any + environment { SSH_CREDENTIALS = 'exceed-sleep' REMOTE_SERVER = 'ubuntu@160.187.167.106' REMOTE_WORKSPACE = '/var/www/html' GIT_CREDENTIALS = 'git-cred' - TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Frontend_Test.git' - TEST_DIR = 'Spurrin_Frontend_Test' 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 { - // ... [Previous stages remain unchanged] ... + 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('Run Pytest Validation') { + stage('Clone & Build Frontend') { steps { script { withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) { @@ -24,17 +34,12 @@ pipeline { ssh ${REMOTE_SERVER} ' set -e cd ${REMOTE_WORKSPACE} - rm -rf ${TEST_DIR} - git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki/Tech4biz/${TEST_DIR}.git - cd ${TEST_DIR} - chmod +x run_pipeline.sh - /home/ubuntu/venv/bin/bash run_pipeline.sh > pytest_output.log 2>&1 || ( - echo "Tests failed. Rolling back..." && \ - cd ${REMOTE_WORKSPACE} && \ - rm -rf spurrin-dev-ui && \ - mv spurrin-dev-ui-prev spurrin-dev-ui && \ - exit 1 - ) + 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 ' """ } @@ -57,9 +62,8 @@ pipeline { chmod +x run_tests.sh ./run_tests.sh > backend_pytest_output.log 2>&1 || ( echo "Backend tests failed. Rolling back..." && \ - cd ${REMOTE_WORKSPACE} && \ - rm -rf spurrin-dev-ui && \ - mv spurrin-dev-ui-prev spurrin-dev-ui && \ + cd ${REMOTE_WORKSPACE}/spurrin-dev-ui && \ + git reset --hard HEAD~1 && \ exit 1 ) ' @@ -78,7 +82,6 @@ pipeline { script { sshagent(credentials: [SSH_CREDENTIALS]) { sh """ - scp ${REMOTE_SERVER}:${REMOTE_WORKSPACE}/${TEST_DIR}/pytest_output.log pytest_output.log || echo "No frontend log found." scp ${REMOTE_SERVER}:${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}/backend_pytest_output.log backend_pytest_output.log || echo "No backend log found." """ } @@ -106,22 +109,34 @@ pipeline { emailext( to: 'jassim.mohammed@tech4biz.io', subject: "✅ Jenkins Deployment Successful: Spurrin UI", - body: "The deployment and tests completed successfully on ${REMOTE_SERVER}.\n\n${env.BUILD_URL}console" + body: "The deployment and backend tests completed successfully on ${REMOTE_SERVER}.\n\n${env.BUILD_URL}console" ) } - failure { - emailext( - to: 'jassim.mohammed@tech4biz.io', - subject: "❌ Jenkins Deployment Failed: Spurrin UI", - body: """Deployment or testing failed on ${REMOTE_SERVER}. -See full console output: + 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: 'pytest_output.log,backend_pytest_output.log', - attachLog: false - ) + attachmentsPattern: 'backend_pytest_output.log', + attachLog: false + ) + } } + always { echo "🧹 Cleaning Jenkins workspace..." cleanWs()