This commit is contained in:
Marcus Almert
2025-10-29 15:42:37 +01:00
parent 0a9aab8256
commit fb66fbb59e

156
Jenkinsfile vendored
View File

@@ -1,93 +1,83 @@
pipeline { pipeline {
agent any agent any
environment { environment {
DEPLOY_HOST = "192.168.178.180" // your VM IP or hostname DEPLOY_HOST = "192.168.178.180"
DEPLOY_USER = "ubuntu" // user on the VM DEPLOY_USER = "ubuntu"
APP_DIR = "/srv/mitm-webserver" // deploy target directory APP_DIR = "/srv/mitm-webserver"
}
stages {
stage('Checkout') {
steps {
git branch: 'main',
url: 'http://192.168.178.96:3000/marcus/mitm-webserver.git'
}
} }
stages { stage('Build Frontend') {
stage('Checkout') { steps {
steps { dir('frontend') {
echo "📦 Checking out source code from Gitea..." sh 'npm ci'
git branch: 'main', url: 'http://192.168.178.96:3000/marcus/mitm-webserver.git' sh 'npm run build'
}
}
stage('Build Frontend') {
steps {
dir('frontend') {
echo "⚙️ Building frontend..."
sh 'npm ci'
sh 'npm run build'
}
}
}
stage('Prepare Backend') {
steps {
dir('backend') {
echo "🐍 Preparing backend..."
sh '''
python3 -m venv venv
./venv/bin/pip install --upgrade pip
./venv/bin/pip install -r requirements.txt
'''
}
}
}
stage('Deploy to VM') {
steps {
echo "🚀 Deploying mitm-webserver to ${DEPLOY_HOST}..."
withCredentials([usernamePassword(
credentialsId: 'deploy-pass-id', // Jenkins credential ID
usernameVariable: 'USER',
passwordVariable: 'PASS'
)]) {
sh '''
# Ensure sshpass is installed
if ! command -v sshpass >/dev/null 2>&1; then
echo "Installing sshpass..."
sudo apt-get update -y && sudo apt-get install -y sshpass
fi
echo "🔌 Testing SSH connection..."
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 $USER@$DEPLOY_HOST "echo '✅ Connected to $HOSTNAME'"
echo "📤 Uploading frontend build..."
sshpass -p "$PASS" rsync -avz --delete \
-e "ssh -o StrictHostKeyChecking=no" \
frontend/dist/ $USER@$DEPLOY_HOST:$APP_DIR/frontend/dist/
echo "📤 Uploading backend..."
sshpass -p "$PASS" rsync -avz --delete \
-e "ssh -o StrictHostKeyChecking=no" \
backend/ $USER@$DEPLOY_HOST:$APP_DIR/backend/
echo "🔁 Restarting backend service..."
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no $USER@$DEPLOY_HOST "
cd $APP_DIR/backend &&
source venv/bin/activate &&
sudo systemctl restart mitm-webserver-backend.service
"
echo "✅ Deployment finished successfully."
'''
}
}
} }
}
} }
post { stage('Prepare Backend') {
success { steps {
echo '✅ mitm-webserver deployed successfully!' dir('backend') {
} sh '''
failure { python3 -m venv venv
echo '❌ Deployment failed. Check Jenkins logs for details.' ./venv/bin/pip install --upgrade pip
./venv/bin/pip install -r requirements.txt
'''
} }
}
} }
stage('Deploy to VM') {
steps {
withCredentials([usernamePassword(
credentialsId: 'deploy-pass-id',
usernameVariable: 'USER',
passwordVariable: 'PASS'
)]) {
sh '''
# make sure sshpass exists
if ! command -v sshpass >/dev/null 2>&1; then
sudo apt-get update -y && sudo apt-get install -y sshpass
fi
echo "Testing SSH connection..."
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
$USER@$DEPLOY_HOST "echo Connected OK"
echo "Uploading frontend..."
sshpass -p "$PASS" rsync -avz --delete \
-e "ssh -o StrictHostKeyChecking=no" \
frontend/dist/ $USER@$DEPLOY_HOST:$APP_DIR/frontend/dist/
echo "Uploading backend..."
sshpass -p "$PASS" rsync -avz --delete \
-e "ssh -o StrictHostKeyChecking=no" \
backend/ $USER@$DEPLOY_HOST:$APP_DIR/backend/
echo "Restarting backend..."
sshpass -p "$PASS" ssh -o StrictHostKeyChecking=no \
$USER@$DEPLOY_HOST "
cd $APP_DIR/backend &&
source venv/bin/activate &&
sudo systemctl restart mitm-webserver-backend.service
"
'''
}
}
}
}
post {
success { echo '✅ mitm-webserver deployed successfully!' }
failure { echo '❌ Deployment failed. Check logs for details.' }
}
} }