forked from rohit/spurrin-backend
126 lines
4.7 KiB
Groovy
126 lines
4.7 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
SSH_CREDENTIALS = 'spurrin-backend-dev'
|
|
GIT_CREDENTIALS = 'gitea-cred'
|
|
REMOTE_SERVER = 'ubuntu@160.187.166.67'
|
|
REPO_HTTPS_URL = 'https://git.tech4biz.wiki/Tech4Biz-Services/spurrin-cleaned-node.git'
|
|
BRANCH = 'dev'
|
|
|
|
REMOTE_DIR = '/home/ubuntu/spurrin-cleaned-node'
|
|
BACKUP_UPLOADS_DIR = '/home/ubuntu/uploads_backup'
|
|
VENV_PYTHON = '../venv/bin/python'
|
|
|
|
NODE_BIN_PATH = '/home/ubuntu/.nvm/versions/node/v22.12.0/bin'
|
|
NOTIFY_EMAIL = 'jassim.mohammed@tech4biz.io'
|
|
}
|
|
|
|
stages {
|
|
stage('Add Remote Host Key') {
|
|
steps {
|
|
echo '🔐 Adding remote host to known_hosts...'
|
|
sshagent(credentials: [SSH_CREDENTIALS]) {
|
|
sh '''
|
|
mkdir -p ~/.ssh
|
|
ssh-keyscan -H ${REMOTE_SERVER#*@} >> ~/.ssh/known_hosts
|
|
'''
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Clone Fresh Repo') {
|
|
steps {
|
|
echo '📁 Cloning fresh repo on remote via HTTPS...'
|
|
withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS}", usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
|
sshagent(credentials: [SSH_CREDENTIALS]) {
|
|
sh """
|
|
ssh ${REMOTE_SERVER} '
|
|
set -e
|
|
|
|
# Backup entire uploads folder as folder inside uploads_backup
|
|
if [ -d ${REMOTE_DIR}/uploads ]; then
|
|
rm -rf ${BACKUP_UPLOADS_DIR}
|
|
mkdir -p ${BACKUP_UPLOADS_DIR}
|
|
cp -a ${REMOTE_DIR}/uploads ${BACKUP_UPLOADS_DIR}/
|
|
fi
|
|
|
|
# Backup .env if it exists
|
|
if [ -f ${REMOTE_DIR}/.env ]; then
|
|
sudo cp ${REMOTE_DIR}/.env /tmp/.env
|
|
fi
|
|
|
|
# Remove old repo and recreate directory
|
|
rm -rf ${REMOTE_DIR}
|
|
mkdir -p ${REMOTE_DIR}
|
|
|
|
# Clone fresh repo using HTTPS with creds
|
|
git clone -b ${BRANCH} https://${GIT_USER}:${GIT_PASS}@git.tech4biz.wiki/Tech4Biz-Services/spurrin-cleaned-node.git ${REMOTE_DIR}
|
|
|
|
# Restore uploads folder as whole folder
|
|
if [ -d ${BACKUP_UPLOADS_DIR}/uploads ]; then
|
|
rm -rf ${REMOTE_DIR}/uploads
|
|
cp -a ${BACKUP_UPLOADS_DIR}/uploads ${REMOTE_DIR}/
|
|
fi
|
|
|
|
# Restore .env file
|
|
if [ -f /tmp/.env ]; then
|
|
sudo cp /tmp/.env ${REMOTE_DIR}/.env
|
|
fi
|
|
|
|
# Copy certificates folder from home to repo
|
|
if [ -d /home/ubuntu/certificates ]; then
|
|
cp -a /home/ubuntu/certificates ${REMOTE_DIR}/
|
|
fi
|
|
'
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
stage('Install & Start Services') {
|
|
steps {
|
|
echo '🚀 Installing and starting services...'
|
|
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
|
|
'
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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"
|
|
}
|
|
|
|
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"
|
|
}
|
|
}
|
|
} |