forked from rohit/spurrin-backend
Jnkinsfile
This commit is contained in:
parent
ba3c0b2bde
commit
9ab30ac27f
70
Jenkinsfile
vendored
70
Jenkinsfile
vendored
@ -29,48 +29,65 @@ pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Clone Fresh Repo') {
|
stage('Update Repo on Remote') {
|
||||||
steps {
|
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')]) {
|
withCredentials([usernamePassword(credentialsId: "${GIT_CREDENTIALS}", usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||||
sshagent(credentials: [SSH_CREDENTIALS]) {
|
sshagent(credentials: [SSH_CREDENTIALS]) {
|
||||||
sh """
|
sh """
|
||||||
ssh ${REMOTE_SERVER} '
|
ssh ${REMOTE_SERVER} '
|
||||||
set -e
|
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
|
if [ -d ${REMOTE_DIR}/uploads ]; then
|
||||||
rm -rf ${BACKUP_UPLOADS_DIR}
|
|
||||||
mkdir -p ${BACKUP_UPLOADS_DIR}
|
|
||||||
cp -a ${REMOTE_DIR}/uploads ${BACKUP_UPLOADS_DIR}/
|
cp -a ${REMOTE_DIR}/uploads ${BACKUP_UPLOADS_DIR}/
|
||||||
fi
|
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
|
if [ -f ${REMOTE_DIR}/.env ]; then
|
||||||
sudo cp ${REMOTE_DIR}/.env /tmp/.env
|
cp ${REMOTE_DIR}/.env ${BACKUP_UPLOADS_DIR}/.env
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove old repo and recreate directory
|
if [ -d ${REMOTE_DIR}/certificates ]; then
|
||||||
rm -rf ${REMOTE_DIR}
|
cp -a ${REMOTE_DIR}/certificates ${BACKUP_UPLOADS_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
|
fi
|
||||||
|
|
||||||
# Restore .env file
|
# Pull or clone repo
|
||||||
if [ -f /tmp/.env ]; then
|
if [ -d ${REMOTE_DIR}/.git ]; then
|
||||||
sudo cp /tmp/.env ${REMOTE_DIR}/.env
|
echo "🔁 Repo exists. Pulling latest changes..."
|
||||||
fi
|
cd ${REMOTE_DIR}
|
||||||
|
git reset --hard
|
||||||
# Copy certificates folder from home to repo
|
git clean -fd
|
||||||
if [ -d /home/ubuntu/certificates ]; then
|
git pull origin ${BRANCH}
|
||||||
cp -a /home/ubuntu/certificates ${REMOTE_DIR}/
|
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
|
fi
|
||||||
'
|
'
|
||||||
"""
|
"""
|
||||||
@ -80,6 +97,7 @@ stage('Clone Fresh Repo') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stage('Install & Start Services') {
|
stage('Install & Start Services') {
|
||||||
steps {
|
steps {
|
||||||
echo '🚀 Installing and starting services...'
|
echo '🚀 Installing and starting services...'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user