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

44
Jenkinsfile vendored
View File

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