Update Jenkinsfile
This commit is contained in:
parent
4ea308aa18
commit
82a7d77e39
175
Jenkinsfile
vendored
175
Jenkinsfile
vendored
@ -1,117 +1,130 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
environment {
|
||||
SSH_CREDENTIALS = 'hookpilot'
|
||||
GIT_CREDENTIALS = 'gitea-cred'
|
||||
REMOTE_SERVER = 'ubuntu@160.187.167.213'
|
||||
REPO_HTTPS_URL = 'https://git.tech4biz.wiki/rohit/spurrin-cleaned-backend-dev.git'
|
||||
BRANCH = 'main'
|
||||
|
||||
REMOTE_DIR = '/home/ubuntu/spurrin-cleaned-backend-dev'
|
||||
BACKUP_UPLOADS_DIR = '/home/ubuntu/uploads_backup'
|
||||
VENV_PYTHON = '../venv/bin/python'
|
||||
|
||||
NODE_BIN_PATH = '/home/ubuntu/.nvm/versions/node/v22.15.0/bin'
|
||||
NOTIFY_EMAIL = 'jassim.mohammed@tech4biz.io'
|
||||
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 {
|
||||
stage('Add Remote Host Key') {
|
||||
// ... [Previous stages remain unchanged] ...
|
||||
|
||||
stage('Run Pytest Validation') {
|
||||
steps {
|
||||
echo '🔐 Adding remote host to known_hosts...'
|
||||
sshagent(credentials: [SSH_CREDENTIALS]) {
|
||||
sh '''
|
||||
mkdir -p ~/.ssh
|
||||
ssh-keyscan -H ${REMOTE_SERVER#*@} >> ~/.ssh/known_hosts
|
||||
'''
|
||||
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}
|
||||
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 {
|
||||
echo '🔄 Pulling latest code on remote server with conditional restore and fresh backup...'
|
||||
withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS}", usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||
sshagent(credentials: [SSH_CREDENTIALS]) {
|
||||
sh """
|
||||
script {
|
||||
withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
|
||||
sshagent(credentials: [SSH_CREDENTIALS]) {
|
||||
sh """
|
||||
ssh ${REMOTE_SERVER} '
|
||||
set -e
|
||||
|
||||
echo "🗑️ Removing old backups..."
|
||||
rm -rf ${BACKUP_UPLOADS_DIR}
|
||||
mkdir -p ${BACKUP_UPLOADS_DIR}
|
||||
|
||||
echo "📦 Backing up existing data..."
|
||||
[ -d ${REMOTE_DIR}/uploads ] && cp -a ${REMOTE_DIR}/uploads ${BACKUP_UPLOADS_DIR}/
|
||||
[ -d ${REMOTE_DIR}/hospital_data ] && cp -a ${REMOTE_DIR}/hospital_data ${BACKUP_UPLOADS_DIR}/
|
||||
[ -f ${REMOTE_DIR}/.env ] && cp ${REMOTE_DIR}/.env ${BACKUP_UPLOADS_DIR}/.env
|
||||
[ -d ${REMOTE_DIR}/certificates ] && cp -a ${REMOTE_DIR}/certificates ${BACKUP_UPLOADS_DIR}/
|
||||
|
||||
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
|
||||
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} && \
|
||||
rm -rf spurrin-dev-ui && \
|
||||
mv spurrin-dev-ui-prev spurrin-dev-ui && \
|
||||
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}:${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 {
|
||||
echo '🚀 Installing and starting services...'
|
||||
sshagent(credentials: [SSH_CREDENTIALS]) {
|
||||
sh """
|
||||
script {
|
||||
sshagent(credentials: [SSH_CREDENTIALS]) {
|
||||
sh """
|
||||
ssh ${REMOTE_SERVER} '
|
||||
set -e
|
||||
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
|
||||
sudo nginx -t && sudo systemctl reload nginx
|
||||
'
|
||||
"""
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
post {
|
||||
always {
|
||||
echo '🧹 Cleaning workspace...'
|
||||
cleanWs()
|
||||
}
|
||||
|
||||
success {
|
||||
echo '✅ Deployment successful!'
|
||||
mail to: "${NOTIFY_EMAIL}",
|
||||
subject: '✅ Jenkins - spurrin-cleaned-node Deployment Successful',
|
||||
body: "The deployment of spurrin-cleaned-node to ${REMOTE_SERVER} was successful.\n\nRegards,\nJenkins"
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
failure {
|
||||
echo '❌ Deployment failed!'
|
||||
mail to: "${NOTIFY_EMAIL}",
|
||||
subject: '❌ Jenkins - spurrin-cleaned-node Deployment Failed',
|
||||
body: "The deployment of spurrin-cleaned-node to ${REMOTE_SERVER} failed. Please check Jenkins logs.\n\nRegards,\nJenkins"
|
||||
emailext(
|
||||
to: 'jassim.mohammed@tech4biz.io',
|
||||
subject: "❌ Jenkins Deployment Failed: Spurrin UI",
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user