Update Jenkinsfile

This commit is contained in:
jassim 2026-01-28 12:34:08 +00:00
parent 91fe238547
commit 56b3959e46

391
Jenkinsfile vendored
View File

@ -1,86 +1,68 @@
pipeline { pipeline {
agent any agent any
environment {
SSH_CREDENTIALS = 'ltts'
REMOTE_SERVER = 'ubuntu@160.187.166.95'
REMOTE_WORKSPACE = '/home/ubuntu'
PROJECT_NAME = 'qassure-frontend'
BUILD_PATH = '/home/ubuntu/qassure-frontend'
DEPLOY_PATH = '/var/www/qassure-ui'
GIT_CREDENTIALS = 'git-cred'
REPO_URL = 'https://git.tech4biz.wiki/yashwin/Qassure-frontend.git'
NPM_PATH = '/home/ubuntu/.nvm/versions/node/v22.17.0/bin/npm'
NODE_PATH = '/home/ubuntu/.nvm/versions/node/v22.17.0/bin/node'
EMAIL_RECIPIENT = 'sibarchan.nayak@tech4biz.org'
}
options { options {
disableConcurrentBuilds()
timestamps()
timeout(time: 30, unit: 'MINUTES') timeout(time: 30, unit: 'MINUTES')
retry(2)
timestamps()
buildDiscarder(logRotator(numToKeepStr: '10')) buildDiscarder(logRotator(numToKeepStr: '10'))
} }
environment {
// --- Deployment Target ---
SERVER_IP = '160.187.166.95'
SERVER_USER = 'ubuntu'
PROJECT_DIR = '/home/ubuntu/ltts/Qassure-frontend'
// --- Git Config ---
// Using the verified repository URL
GIT_REPO = 'https://git.tech4biz.wiki/yashwin/Qassure-frontend.git'
GIT_BRANCH = 'main'
GIT_CREDENTIALS_ID = 'git-cred'
// --- SSH Config ---
SSH_KEY_CREDENTIALS_ID = 'ltts'
// --- Application Config ---
NODE_HOME = '/home/ubuntu/.nvm/versions/node/v22.17.0'
// --- Build Config ---
VITE_API_BASE_URL = 'https://backendqaasure.tech4bizsolutions.com/api/v1'
// --- System Config ---
BACKUP_DIR = '/home/ubuntu/backups/qassure-frontend'
EMAIL_RECIPIENTS = 'mohammed.yaseen@tech4biz.org'
DEPLOY_START_TIME = "${System.currentTimeMillis()}"
}
stages { stages {
stage('Initialize') { stage('Preparation') {
steps { steps {
script { script {
echo "🚀 Starting QAssur Frontend Deployment Pipeline" echo "Starting ${PROJECT_NAME} deployment pipeline"
echo "📍 Target: ${SERVER_IP}" echo "Server: ${REMOTE_SERVER}"
echo "🌿 Branch: ${GIT_BRANCH}" echo "Build Path: ${BUILD_PATH}"
echo "📂 Directory: ${PROJECT_DIR}" echo "Deploy Path: ${DEPLOY_PATH}"
} }
} }
} }
stage('Setup Project Directory') { stage('Git Operations on Remote Server') {
steps { steps {
sshagent(credentials: [SSH_KEY_CREDENTIALS_ID]) { script {
script { sshagent(credentials: [SSH_CREDENTIALS]) {
withCredentials([usernamePassword( withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
credentialsId: GIT_CREDENTIALS_ID,
usernameVariable: 'GIT_USERNAME',
passwordVariable: 'GIT_PASSWORD'
)]) {
sh """ sh """
ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${SERVER_USER}@${SERVER_IP} ' ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
set -e set -e
echo "📁 Setting up project directory..."
mkdir -p "\$(dirname "${PROJECT_DIR}")" echo "Checking Git repo in home directory..."
mkdir -p ${BACKUP_DIR} if [ -d "${BUILD_PATH}/.git" ]; then
echo "Pulling latest code..."
cd ${BUILD_PATH}
if [ ! -d "${PROJECT_DIR}/.git" ]; then git config --global --add safe.directory ${BUILD_PATH}
echo "📥 Repository not found. Cloning from scratch..." git reset --hard
cd "\$(dirname "${PROJECT_DIR}")" git clean -fd
git config pull.rebase false
git pull https://${GIT_USER}:${GIT_PASS}@git.tech4biz.wiki/yashwin/Qassure-frontend.git main
else
echo "Cloning fresh repo..."
rm -rf ${BUILD_PATH}
git clone https://${GIT_USER}:${GIT_PASS}@git.tech4biz.wiki/yashwin/Qassure-frontend.git ${BUILD_PATH}
git config --global --add safe.directory ${BUILD_PATH}
fi
git config --global credential.helper store cd ${BUILD_PATH}
echo "https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki" > ~/.git-credentials echo "Commit: \$(git rev-parse HEAD)"
'
git clone -b ${GIT_BRANCH} ${GIT_REPO}
rm -f ~/.git-credentials
git config --global --unset credential.helper
echo "✅ Repository cloned successfully"
else
echo "✅ Repository already exists"
fi
'
""" """
} }
} }
@ -88,176 +70,167 @@ pipeline {
} }
} }
stage('Fetch Latest Changes') { stage('Verify Node.js Environment') {
steps { steps {
sshagent(credentials: [SSH_KEY_CREDENTIALS_ID]) { script {
script { sshagent(credentials: [SSH_CREDENTIALS]) {
withCredentials([usernamePassword(
credentialsId: GIT_CREDENTIALS_ID,
usernameVariable: 'GIT_USERNAME',
passwordVariable: 'GIT_PASSWORD'
)]) {
def gitInfo = sh(
script: """
ssh -o StrictHostKeyChecking=no ${SERVER_USER}@${SERVER_IP} '
set -e
cd ${PROJECT_DIR}
echo "📥 Fetching latest changes..."
git config credential.helper store
echo "https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki" > ~/.git-credentials
# Force fetch and reset to ensure we match remote exactly
git fetch origin ${GIT_BRANCH}
git reset --hard origin/${GIT_BRANCH}
rm -f ~/.git-credentials
git config --unset credential.helper
echo "COMMIT_SHORT=\$(git rev-parse --short HEAD)"
echo "COMMIT_MSG=\$(git log -1 --pretty=%B)"
echo "COMMIT_AUTHOR=\$(git log -1 --pretty=%an)"
'
""",
returnStdout: true
).trim()
gitInfo.split('\n').each { line ->
if (line.startsWith('COMMIT_SHORT=')) env.GIT_COMMIT_SHORT = line.split('=', 2)[1]
else if (line.startsWith('COMMIT_MSG=')) env.GIT_COMMIT_MSG = line.split('=', 2)[1]
else if (line.startsWith('COMMIT_AUTHOR=')) env.GIT_AUTHOR = line.split('=', 2)[1]
}
echo "✅ Updated to: ${env.GIT_COMMIT_SHORT}"
}
}
}
}
}
stage('Pre-Flight Checks') {
steps {
sshagent(credentials: [SSH_KEY_CREDENTIALS_ID]) {
script {
sh """ sh """
ssh -o StrictHostKeyChecking=no ${SERVER_USER}@${SERVER_IP} ' ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
set -e set -e
echo "🔍 Running pre-flight checks..." export PATH="/home/ubuntu/.nvm/versions/node/v22.17.0/bin:\$PATH"
cd ${BUILD_PATH}
# Check Node.js echo "Node: \$(${NODE_PATH} -v)"
export PATH="${NODE_HOME}/bin:\$PATH" echo "NPM: \$(${NPM_PATH} -v)"
if ! node --version >/dev/null 2>&1; then ${NPM_PATH} cache clean --force
echo "❌ Node.js not found at ${NODE_HOME}" '
exit 1
fi
# Disk Space (minimum 1GB)
available=\$(df ${PROJECT_DIR} | tail -1 | awk "{print \\\$4}")
if [ \$available -lt 1048576 ]; then
echo "❌ Insufficient disk space"
exit 1
fi
echo "✅ Pre-flight checks passed"
'
""" """
} }
} }
} }
} }
stage('Install & Build') { stage('Install Dependencies') {
steps { steps {
sshagent(credentials: [SSH_KEY_CREDENTIALS_ID]) { sshagent(credentials: [SSH_CREDENTIALS]) {
script { sh """
sh """ ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
ssh -o StrictHostKeyChecking=no ${SERVER_USER}@${SERVER_IP} ' set -e
set -e export PATH="/home/ubuntu/.nvm/versions/node/v22.17.0/bin:\$PATH"
cd ${PROJECT_DIR} cd ${BUILD_PATH}
export PATH="${NODE_HOME}/bin:\$PATH" echo "Installing dependencies..."
export VITE_API_BASE_URL="${VITE_API_BASE_URL}" rm -rf node_modules package-lock.json
${NPM_PATH} install --force
'
"""
}
}
}
echo "📦 Installing dependencies..." stage('Build Application') {
# Use clean install for reliability steps {
npm ci --prefer-offline --no-audit sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
set -e
export PATH="/home/ubuntu/.nvm/versions/node/v22.17.0/bin:\$PATH"
cd ${BUILD_PATH}
echo "Building application..."
${NPM_PATH} run build
echo "Build directory contents:"
ls -la dist || ls -la build || echo "No build/dist directory found"
'
"""
}
}
}
echo "🏗️ Building application..." stage('Deploy Static Files') {
npm run build steps {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
set -e
cd ${BUILD_PATH}
# Verify the build output echo "Preparing deployment directory..."
if [ -f "dist/index.html" ]; then sudo mkdir -p ${DEPLOY_PATH}
echo "✅ Build successful - dist/index.html optimized and ready"
else echo "Moving dist files to deployment directory..."
echo "❌ Build failed - dist/index.html missing" if [ -d "dist" ]; then
exit 1 sudo rm -rf ${DEPLOY_PATH}/*
fi sudo cp -r dist/* ${DEPLOY_PATH}/
' echo "Dist files copied successfully"
""" elif [ -d "build" ]; then
} sudo rm -rf ${DEPLOY_PATH}/*
sudo cp -r build/* ${DEPLOY_PATH}/
echo "Build files copied successfully"
else
echo "Error: No dist or build directory found!"
exit 1
fi
# Set proper ownership and permissions
sudo chown -R www-data:www-data ${DEPLOY_PATH}
sudo find ${DEPLOY_PATH} -type d -exec chmod 755 {} \\;
sudo find ${DEPLOY_PATH} -type f -exec chmod 644 {} \\;
echo "Deployment directory contents:"
ls -la ${DEPLOY_PATH}
'
"""
}
}
}
stage('Restart Nginx') {
steps {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
set -e
echo "Testing nginx configuration..."
sudo nginx -t
echo "Restarting nginx..."
sudo systemctl restart nginx
echo "Checking nginx status..."
sudo systemctl status nginx --no-pager -l
'
"""
}
}
}
stage('Health Check') {
steps {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} '
set -e
echo "Verifying deployment..."
echo "Build directory structure:"
ls -la ${BUILD_PATH}
echo "Deployment directory contents:"
ls -la ${DEPLOY_PATH}
echo "Nginx status:"
sudo systemctl is-active nginx
'
"""
} }
} }
} }
} }
post { post {
failure { always {
script { cleanWs()
echo "❌ Deployment failed"
def durationMillis = System.currentTimeMillis() - env.DEPLOY_START_TIME.toLong()
def minutes = (durationMillis / 60000) as Integer
def seconds = ((durationMillis % 60000) / 1000) as Integer
def failureMessage = """❌ QAssur Frontend DEPLOYMENT FAILED
═══════════════════════════════════════
DETAILS
═══════════════════════════════════════
Branch: ${GIT_BRANCH}
Commit: ${env.GIT_COMMIT_SHORT ?: 'Unknown'}
Author: ${env.GIT_AUTHOR ?: 'Unknown'}
Duration: ${minutes}m ${seconds}s
Stage: ${env.STAGE_NAME ?: 'Unknown'}
Server: ${SERVER_IP}
Action Required:
1. Check Jenkins console logs for detailed error
2. Verify local build works: npm run build
"""
try {
mail to: "${EMAIL_RECIPIENTS}",
subject: "❌ QAssur Frontend FAILED - Build #${BUILD_NUMBER}",
body: failureMessage
} catch (e) {
echo "⚠️ Email failed: ${e.message}"
}
}
} }
success { success {
script { mail to: "${EMAIL_RECIPIENT}",
def durationMillis = System.currentTimeMillis() - env.DEPLOY_START_TIME.toLong() subject: "✅ Jenkins - ${PROJECT_NAME} Deployment Successful",
def minutes = (durationMillis / 60000) as Integer body: """The deployment of '${PROJECT_NAME}' to ${REMOTE_SERVER} was successful.
def seconds = ((durationMillis % 60000) / 1000) as Integer
def successMessage = """✅ QAssur Frontend DEPLOYMENT SUCCESS Build Number: ${BUILD_NUMBER}
URL: ${BUILD_URL}
Time: ${new Date()}
═══════════════════════════════════════ The static files have been deployed and nginx has been restarted.
DETAILS """
═══════════════════════════════════════ }
Branch: ${GIT_BRANCH} failure {
Commit: ${env.GIT_COMMIT_SHORT} mail to: "${EMAIL_RECIPIENT}",
Author: ${env.GIT_AUTHOR} subject: "❌ Jenkins - ${PROJECT_NAME} Deployment Failed",
Duration: ${minutes}m ${seconds}s body: """Deployment failed. Please review logs at:
Live URL: https://qasure.tech4bizsolutions.com ${BUILD_URL}console
Time: ${new Date()}
""" """
try {
mail to: "${EMAIL_RECIPIENTS}",
subject: "✅ QAssur Frontend SUCCESS - Build #${BUILD_NUMBER}",
body: successMessage
} catch (e) {
echo "⚠️ Email failed: ${e.message}"
}
}
} }
} }
} }