spurrin-cleaned-backend-dev/Jenkinsfile
2025-06-13 12:15:51 +00:00

148 lines
5.4 KiB
Groovy

pipeline {
agent any
environment {
SSH_CREDENTIALS = 'spurrin-backend-dev'
REMOTE_SERVER = 'ubuntu@160.187.166.67'
REMOTE_WORKSPACE = '/var/www/html'
GIT_CREDENTIALS = 'git-cred'
BACKEND_TEST_DIR = 'Spurrin_Backend_Test'
BACKEND_TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Backend_Test.git'
BACKEND_TEST_ENV = '/home/ubuntu/testenv'
}
stages {
stage('Add Host Key') {
steps {
script {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
mkdir -p ~/.ssh
ssh-keyscan -H ${REMOTE_SERVER.split('@')[1]} >> ~/.ssh/known_hosts
"""
}
}
}
}
stage('Clone & Build Frontend') {
steps {
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}
if [ -d spurrin-dev-ui ]; then mv spurrin-dev-ui spurrin-dev-ui-prev; fi
git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki/Tech4Biz-Services/spurrin-dev-ui.git
cd spurrin-dev-ui
export PATH=/home/ubuntu/.nvm/versions/node/v22.14.0/bin:\$PATH
npm install --legacy-peer-deps --force
npm run build
'
"""
}
}
}
}
}
stage('Run Backend Test Validation') {
steps {
script {
withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
ssh ${REMOTE_SERVER} '
set -e
rm -rf ${BACKEND_TEST_DIR}
git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@${BACKEND_TEST_REPO}
cd ${BACKEND_TEST_DIR}
chmod +x run_tests.sh
source ${BACKEND_TEST_ENV}/venv/bin/activate
./run_tests.sh > backend_pytest_output.log 2>&1 || (
echo "Backend tests failed. Rolling back..." && \
cd ${REMOTE_WORKSPACE}/spurrin-dev-ui && \
git reset --hard HEAD~1 && \
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}:${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}/backend_pytest_output.log backend_pytest_output.log || echo "No backend log found."
"""
}
}
}
}
stage('Reload Nginx') {
steps {
script {
sshagent(credentials: [SSH_CREDENTIALS]) {
sh """
ssh ${REMOTE_SERVER} '
sudo nginx -t && sudo systemctl reload nginx
'
"""
}
}
}
}
}
post {
success {
emailext(
to: 'jassim.mohammed@tech4biz.io',
subject: "✅ Jenkins Deployment Successful: Spurrin UI",
body: "The deployment and backend tests completed successfully on ${REMOTE_SERVER}.\n\n${env.BUILD_URL}console"
)
}
failure {
script {
def backendErrors = sh(
script: "grep -E 'FAIL|ERROR|Exception|Traceback' backend_pytest_output.log || echo 'No errors found.'",
returnStdout: true
).trim()
emailext(
to: 'jassim.mohammed@tech4biz.io',
subject: "❌ Jenkins Deployment Failed: Spurrin UI (Backend Tests)",
body: """Deployment or backend testing failed on ${REMOTE_SERVER}.
⚠️ **Backend Test Errors**:
${backendErrors}
🔍 View full console:
${env.BUILD_URL}console
""",
attachmentsPattern: 'backend_pytest_output.log',
attachLog: false
)
}
}
always {
echo "🧹 Cleaning Jenkins workspace..."
cleanWs()
}
}
}