spurrin-cleaned-backend-dev/Jenkinsfile
2025-06-12 14:25:29 +00:00

131 lines
5.3 KiB
Groovy

pipeline {
agent any
environment {
SSH_CREDENTIALS = 'exceed-sleep'
REMOTE_SERVER = 'ubuntu@160.187.167.106'
REMOTE_WORKSPACE = '/var/www/html'
GIT_CREDENTIALS = 'git-cred'
TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Frontend_Test.git'
TEST_DIR = 'Spurrin_Frontend_Test'
BACKEND_TEST_DIR = 'Spurrin_Backend_Test'
BACKEND_TEST_REPO = 'https://git.tech4biz.wiki/Tech4biz/Spurrin_Backend_Test.git'
BACKEND_TEST_ENV = '/home/ubuntu/test'
}
stages {
// ... [Previous stages remain unchanged] ...
stage('Run Pytest Validation') {
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}
rm -rf ${TEST_DIR}
git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@git.tech4biz.wiki/Tech4biz/${TEST_DIR}.git
cd ${TEST_DIR}
chmod +x run_pipeline.sh
/home/ubuntu/venv/bin/bash run_pipeline.sh > pytest_output.log 2>&1 || (
echo "Tests failed. Rolling back..." && \
cd ${REMOTE_WORKSPACE} && \
rm -rf spurrin-dev-ui && \
mv spurrin-dev-ui-prev spurrin-dev-ui && \
exit 1
)
'
"""
}
}
}
}
}
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_ENV}/${BACKEND_TEST_DIR}
git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@${BACKEND_TEST_REPO} ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}
cd ${BACKEND_TEST_ENV}/${BACKEND_TEST_DIR}
chmod +x run_tests.sh
./run_tests.sh > backend_pytest_output.log 2>&1 || (
echo "Backend tests failed. Rolling back..." && \
cd ${REMOTE_WORKSPACE} && \
rm -rf spurrin-dev-ui && \
mv spurrin-dev-ui-prev spurrin-dev-ui && \
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}:${REMOTE_WORKSPACE}/${TEST_DIR}/pytest_output.log pytest_output.log || echo "No frontend log found."
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 tests completed successfully on ${REMOTE_SERVER}.\n\n${env.BUILD_URL}console"
)
}
failure {
emailext(
to: 'jassim.mohammed@tech4biz.io',
subject: "❌ Jenkins Deployment Failed: Spurrin UI",
body: """Deployment or testing failed on ${REMOTE_SERVER}.
See full console output:
${env.BUILD_URL}console
""",
attachmentsPattern: 'pytest_output.log,backend_pytest_output.log',
attachLog: false
)
}
always {
echo "🧹 Cleaning Jenkins workspace..."
cleanWs()
}
}
}