From 9ab30ac27f34c267ab6ab85e6c755b4b9c44da55 Mon Sep 17 00:00:00 2001 From: vriti Date: Mon, 9 Jun 2025 19:35:18 +0530 Subject: [PATCH] Jnkinsfile --- Jenkinsfile | 70 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a98a1ce..07ae9f9 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -29,48 +29,65 @@ pipeline { } } -stage('Clone Fresh Repo') { +stage('Update Repo on Remote') { steps { - echo '📁 Cloning fresh repo on remote via HTTPS...' + 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 """ ssh ${REMOTE_SERVER} ' set -e - # Backup entire uploads folder as folder inside uploads_backup + # Clean old backup folder + echo "🗑️ Removing old backups..." + rm -rf ${BACKUP_UPLOADS_DIR} + mkdir -p ${BACKUP_UPLOADS_DIR} + + echo "📦 Backing up existing data..." 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 [ -d ${REMOTE_DIR}/hospital_data ]; then + cp -a ${REMOTE_DIR}/hospital_data ${BACKUP_UPLOADS_DIR}/ + fi + if [ -f ${REMOTE_DIR}/.env ]; then - sudo cp ${REMOTE_DIR}/.env /tmp/.env + cp ${REMOTE_DIR}/.env ${BACKUP_UPLOADS_DIR}/.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}/ + if [ -d ${REMOTE_DIR}/certificates ]; then + cp -a ${REMOTE_DIR}/certificates ${BACKUP_UPLOADS_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}/ + # Pull or clone repo + if [ -d ${REMOTE_DIR}/.git ]; then + echo "🔁 Repo exists. Pulling latest changes..." + cd ${REMOTE_DIR} + git reset --hard + git clean -fd + git pull origin ${BRANCH} + 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/Tech4Biz-Services/spurrin-cleaned-node.git ${REMOTE_DIR} + + # Restore backup only on fresh clone + if [ -d ${BACKUP_UPLOADS_DIR}/uploads ]; then + cp -a ${BACKUP_UPLOADS_DIR}/uploads ${REMOTE_DIR}/ + fi + + if [ -d ${BACKUP_UPLOADS_DIR}/hospital_data ]; then + cp -a ${BACKUP_UPLOADS_DIR}/hospital_data ${REMOTE_DIR}/ + fi + + if [ -f ${BACKUP_UPLOADS_DIR}/.env ]; then + cp ${BACKUP_UPLOADS_DIR}/.env ${REMOTE_DIR}/.env + fi + + if [ -d ${BACKUP_UPLOADS_DIR}/certificates ]; then + cp -a ${BACKUP_UPLOADS_DIR}/certificates ${REMOTE_DIR}/ + fi fi ' """ @@ -80,6 +97,7 @@ stage('Clone Fresh Repo') { } + stage('Install & Start Services') { steps { echo '🚀 Installing and starting services...'