Update Jenkinsfile

This commit is contained in:
jassim 2025-06-12 14:25:29 +00:00
parent 4ea308aa18
commit 82a7d77e39

175
Jenkinsfile vendored
View File

@ -1,117 +1,130 @@
pipeline { pipeline {
agent any agent any
environment { environment {
SSH_CREDENTIALS = 'hookpilot' SSH_CREDENTIALS = 'exceed-sleep'
GIT_CREDENTIALS = 'gitea-cred' REMOTE_SERVER = 'ubuntu@160.187.167.106'
REMOTE_SERVER = 'ubuntu@160.187.167.213' REMOTE_WORKSPACE = '/var/www/html'
REPO_HTTPS_URL = 'https://git.tech4biz.wiki/rohit/spurrin-cleaned-backend-dev.git' GIT_CREDENTIALS = 'git-cred'
BRANCH = 'main' TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Frontend_Test.git'
TEST_DIR = 'Spurrin_Frontend_Test'
REMOTE_DIR = '/home/ubuntu/spurrin-cleaned-backend-dev' BACKEND_TEST_DIR = 'Spurrin_Backend_Test'
BACKUP_UPLOADS_DIR = '/home/ubuntu/uploads_backup' BACKEND_TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Backend_Test.git'
VENV_PYTHON = '../venv/bin/python' BACKEND_TEST_ENV = '/home/ubuntu/test'
NODE_BIN_PATH = '/home/ubuntu/.nvm/versions/node/v22.15.0/bin'
NOTIFY_EMAIL = 'jassim.mohammed@tech4biz.io'
} }
stages { stages {
stage('Add Remote Host Key') { // ... [Previous stages remain unchanged] ...
stage('Run Pytest Validation') {
steps { steps {
echo '🔐 Adding remote host to known_hosts...' script {
sshagent(credentials: [SSH_CREDENTIALS]) { withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh ''' sshagent(credentials: [SSH_CREDENTIALS]) {
mkdir -p ~/.ssh sh """
ssh-keyscan -H ${REMOTE_SERVER#*@} >> ~/.ssh/known_hosts 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
)
'
"""
}
}
} }
} }
} }
stage('Update Repo on Remote') { stage('Run Backend Test Validation') {
steps { steps {
echo '🔄 Pulling latest code on remote server with conditional restore and fresh backup...' script {
withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS}", usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sshagent(credentials: [SSH_CREDENTIALS]) { sshagent(credentials: [SSH_CREDENTIALS]) {
sh """ sh """
ssh ${REMOTE_SERVER} ' ssh ${REMOTE_SERVER} '
set -e set -e
rm -rf ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}
echo "🗑️ Removing old backups..." git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@${BACKEND_TEST_REPO} ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}
rm -rf ${BACKUP_UPLOADS_DIR} cd ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}
mkdir -p ${BACKUP_UPLOADS_DIR} chmod +x run_tests.sh
./run_tests.sh > backend_pytest_output.log 2>&1 || (
echo "📦 Backing up existing data..." echo "Backend tests failed. Rolling back..." && \
[ -d ${REMOTE_DIR}/uploads ] && cp -a ${REMOTE_DIR}/uploads ${BACKUP_UPLOADS_DIR}/ cd ${REMOTE_WORKSPACE} && \
[ -d ${REMOTE_DIR}/hospital_data ] && cp -a ${REMOTE_DIR}/hospital_data ${BACKUP_UPLOADS_DIR}/ rm -rf spurrin-dev-ui && \
[ -f ${REMOTE_DIR}/.env ] && cp ${REMOTE_DIR}/.env ${BACKUP_UPLOADS_DIR}/.env mv spurrin-dev-ui-prev spurrin-dev-ui && \
[ -d ${REMOTE_DIR}/certificates ] && cp -a ${REMOTE_DIR}/certificates ${BACKUP_UPLOADS_DIR}/ exit 1
)
if [ -d ${REMOTE_DIR}/.git ]; then
echo "🔁 Repo exists. Pulling latest changes..."
cd ${REMOTE_DIR}
git stash push --include-untracked --message "temp-backup-before-pull" || true
git pull origin ${BRANCH}
git stash pop || true
else
echo "📥 Repo not found. Cloning fresh and restoring backup..."
rm -rf ${REMOTE_DIR}
git clone -b ${BRANCH} https://${GIT_USER}:${GIT_PASS}@git.tech4biz.wiki/rohit/spurrin-cleaned-backend-dev.git ${REMOTE_DIR}
[ -d ${BACKUP_UPLOADS_DIR}/uploads ] && cp -a ${BACKUP_UPLOADS_DIR}/uploads ${REMOTE_DIR}/
[ -d ${BACKUP_UPLOADS_DIR}/hospital_data ] && cp -a ${BACKUP_UPLOADS_DIR}/hospital_data ${REMOTE_DIR}/
[ -f ${BACKUP_UPLOADS_DIR}/.env ] && cp ${BACKUP_UPLOADS_DIR}/.env ${REMOTE_DIR}/.env
[ -d ${BACKUP_UPLOADS_DIR}/certificates ] && cp -a ${BACKUP_UPLOADS_DIR}/certificates ${REMOTE_DIR}/
fi
' '
"""
}
}
}
}
}
stage('Copy Test Log from Remote') {
when {
expression { currentBuild.result == null || currentBuild.result == 'FAILURE' }
}
steps {
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."
""" """
} }
} }
} }
} }
stage('Install & Start Services') { stage('Reload Nginx') {
steps { steps {
echo '🚀 Installing and starting services...' script {
sshagent(credentials: [SSH_CREDENTIALS]) { sshagent(credentials: [SSH_CREDENTIALS]) {
sh """ sh """
ssh ${REMOTE_SERVER} ' ssh ${REMOTE_SERVER} '
set -e sudo nginx -t && sudo systemctl reload nginx
export PATH=${NODE_BIN_PATH}:\$PATH
cd ${REMOTE_DIR}
npm install --legacy-peer-deps --force
pm2 delete web-server || true
pm2 delete convo || true
pm2 start npm --name web-server -- start
pm2 start chat.py --interpreter ${VENV_PYTHON} --name=convo
' '
""" """
}
} }
} }
} }
} }
post { post {
always {
echo '🧹 Cleaning workspace...'
cleanWs()
}
success { success {
echo '✅ Deployment successful!' emailext(
mail to: "${NOTIFY_EMAIL}", to: 'jassim.mohammed@tech4biz.io',
subject: '✅ Jenkins - spurrin-cleaned-node Deployment Successful', subject: "✅ Jenkins Deployment Successful: Spurrin UI",
body: "The deployment of spurrin-cleaned-node to ${REMOTE_SERVER} was successful.\n\nRegards,\nJenkins" body: "The deployment and tests completed successfully on ${REMOTE_SERVER}.\n\n${env.BUILD_URL}console"
)
} }
failure { failure {
echo '❌ Deployment failed!' emailext(
mail to: "${NOTIFY_EMAIL}", to: 'jassim.mohammed@tech4biz.io',
subject: '❌ Jenkins - spurrin-cleaned-node Deployment Failed', subject: "❌ Jenkins Deployment Failed: Spurrin UI",
body: "The deployment of spurrin-cleaned-node to ${REMOTE_SERVER} failed. Please check Jenkins logs.\n\nRegards,\nJenkins" body: """Deployment or testing failed on ${REMOTE_SERVER}.
See full console output:
${env.BUILD_URL}console
""",
attachmentsPattern: 'pytest_output.log,backend_pytest_output.log',
attachLog: false
)
}
always {
echo "🧹 Cleaning Jenkins workspace..."
cleanWs()
} }
} }
} }