Update Jenkinsfile

This commit is contained in:
jassim 2025-06-12 14:37:46 +00:00
parent 82a7d77e39
commit f7dcecba8b

73
Jenkinsfile vendored
View File

@ -1,21 +1,31 @@
pipeline { pipeline {
agent any agent any
environment { environment {
SSH_CREDENTIALS = 'exceed-sleep' SSH_CREDENTIALS = 'exceed-sleep'
REMOTE_SERVER = 'ubuntu@160.187.167.106' REMOTE_SERVER = 'ubuntu@160.187.167.106'
REMOTE_WORKSPACE = '/var/www/html' REMOTE_WORKSPACE = '/var/www/html'
GIT_CREDENTIALS = 'git-cred' 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_DIR = 'Spurrin_Backend_Test'
BACKEND_TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Backend_Test.git' BACKEND_TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Backend_Test.git'
BACKEND_TEST_ENV = '/home/ubuntu/test' BACKEND_TEST_ENV = '/home/ubuntu/test'
} }
stages { 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 { steps {
script { script {
withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) { withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
@ -24,17 +34,12 @@ pipeline {
ssh ${REMOTE_SERVER} ' ssh ${REMOTE_SERVER} '
set -e set -e
cd ${REMOTE_WORKSPACE} cd ${REMOTE_WORKSPACE}
rm -rf ${TEST_DIR} 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/${TEST_DIR}.git git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki/Tech4Biz-Services/spurrin-dev-ui.git
cd ${TEST_DIR} cd spurrin-dev-ui
chmod +x run_pipeline.sh export PATH=/home/ubuntu/.nvm/versions/node/v22.14.0/bin:\$PATH
/home/ubuntu/venv/bin/bash run_pipeline.sh > pytest_output.log 2>&1 || ( npm install --legacy-peer-deps --force
echo "Tests failed. Rolling back..." && \ npm run build
cd ${REMOTE_WORKSPACE} && \
rm -rf spurrin-dev-ui && \
mv spurrin-dev-ui-prev spurrin-dev-ui && \
exit 1
)
' '
""" """
} }
@ -57,9 +62,8 @@ pipeline {
chmod +x run_tests.sh chmod +x run_tests.sh
./run_tests.sh > backend_pytest_output.log 2>&1 || ( ./run_tests.sh > backend_pytest_output.log 2>&1 || (
echo "Backend tests failed. Rolling back..." && \ echo "Backend tests failed. Rolling back..." && \
cd ${REMOTE_WORKSPACE} && \ cd ${REMOTE_WORKSPACE}/spurrin-dev-ui && \
rm -rf spurrin-dev-ui && \ git reset --hard HEAD~1 && \
mv spurrin-dev-ui-prev spurrin-dev-ui && \
exit 1 exit 1
) )
' '
@ -78,7 +82,6 @@ pipeline {
script { script {
sshagent(credentials: [SSH_CREDENTIALS]) { sshagent(credentials: [SSH_CREDENTIALS]) {
sh """ 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." 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( emailext(
to: 'jassim.mohammed@tech4biz.io', to: 'jassim.mohammed@tech4biz.io',
subject: "✅ Jenkins Deployment Successful: Spurrin UI", 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 ${env.BUILD_URL}console
""", """,
attachmentsPattern: 'pytest_output.log,backend_pytest_output.log', attachmentsPattern: 'backend_pytest_output.log',
attachLog: false attachLog: false
) )
}
} }
always { always {
echo "🧹 Cleaning Jenkins workspace..." echo "🧹 Cleaning Jenkins workspace..."
cleanWs() cleanWs()