diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..749607b --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,221 @@ +pipeline { + agent any + + environment { + SSH_CREDENTIALS = 'cloudtopiaa' + REMOTE_SERVER = 'ubuntu@160.187.166.39' + REMOTE_WORKSPACE = '/home/ubuntu' + PROJECT_NAME = 'codenuk-frontend-live' + DEPLOY_PATH = '/var/www/html/codenuk-frontend-live' + GIT_CREDENTIALS = 'git-cred' + REPO_URL = 'https://git.tech4biz.wiki/Tech4Biz-Services/codenuk-frontend-live.git' + NPM_PATH = '/home/ubuntu/.nvm/versions/node/v22.18.0/bin/npm' + NODE_PATH = '/home/ubuntu/.nvm/versions/node/v22.18.0/bin/node' + EMAIL_RECIPIENT = 'jassim.mohammed@tech4biz.io, chandini.pachigunta@tech4biz.org' + } + + options { + timeout(time: 30, unit: 'MINUTES') + retry(2) + timestamps() + buildDiscarder(logRotator(numToKeepStr: '10')) + } + + stages { + stage('Preparation') { + steps { + script { + echo "Starting ${PROJECT_NAME} deployment pipeline" + echo "Server: ${REMOTE_SERVER}" + echo "Deploy Path: ${DEPLOY_PATH}" + } + } + } + + stage('Git Operations on Remote Server') { + steps { + script { + sshagent(credentials: [SSH_CREDENTIALS]) { + withCredentials([usernamePassword(credentialsId: GIT_CREDENTIALS, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { + sh """ + ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} ' + set -e + + echo "Checking Git repo..." + if [ -d "${DEPLOY_PATH}/.git" ]; then + echo "Pulling latest code..." + cd ${DEPLOY_PATH} + + # Fix ownership issues + sudo chown -R ubuntu:ubuntu ${DEPLOY_PATH} + git config --global --add safe.directory ${DEPLOY_PATH} + + git reset --hard + git clean -fd + git config pull.rebase false + git pull https://${GIT_USER}:${GIT_PASS}@git.tech4biz.wiki/Tech4Biz-Services/codenuk-frontend-live.git main + else + echo "Cloning fresh repo..." + sudo rm -rf ${DEPLOY_PATH} + sudo mkdir -p /var/www/html + sudo git clone https://${GIT_USER}:${GIT_PASS}@git.tech4biz.wiki/Tech4Biz-Services/codenuk-frontend-live.git ${DEPLOY_PATH} + sudo chown -R ubuntu:ubuntu ${DEPLOY_PATH} + git config --global --add safe.directory ${DEPLOY_PATH} + fi + + cd ${DEPLOY_PATH} + echo "Commit: \$(git rev-parse HEAD)" + ' + """ + } + } + } + } + } + + stage('Verify Node.js Environment') { + steps { + script { + sshagent(credentials: [SSH_CREDENTIALS]) { + sh """ + ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} ' + set -e + export PATH="/home/ubuntu/.nvm/versions/node/v22.18.0/bin:\$PATH" + cd ${DEPLOY_PATH} + + echo "Node: \$(${NODE_PATH} -v)" + echo "NPM: \$(${NPM_PATH} -v)" + ${NPM_PATH} cache clean --force + ' + """ + } + } + } + } + + stage('Install Dependencies') { + steps { + sshagent(credentials: [SSH_CREDENTIALS]) { + sh """ + ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} ' + set -e + export PATH="/home/ubuntu/.nvm/versions/node/v22.18.0/bin:\$PATH" + cd ${DEPLOY_PATH} + echo "Installing dependencies..." + rm -rf node_modules package-lock.json + ${NPM_PATH} install --force + ' + """ + } + } + } + + stage('Build Application') { + steps { + sshagent(credentials: [SSH_CREDENTIALS]) { + sh """ + ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} ' + set -e + export PATH="/home/ubuntu/.nvm/versions/node/v22.18.0/bin:\$PATH" + cd ${DEPLOY_PATH} + echo "Building..." + ${NPM_PATH} run build + echo "Build directory contents:" + ls -la dist || ls -la build || echo "No build/dist directory found" + ' + """ + } + } + } + + stage('Deploy Static Files') { + steps { + sshagent(credentials: [SSH_CREDENTIALS]) { + sh """ + ssh -o StrictHostKeyChecking=no ${REMOTE_SERVER} ' + set -e + cd ${DEPLOY_PATH} + echo "Setting up static files for nginx..." + + # Ensure 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 "Static files prepared for nginx serving" + ' + """ + } + } + } + + 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 "Directory structure:" + ls -la ${DEPLOY_PATH} + + echo "Build files:" + ls -la ${DEPLOY_PATH}/dist || ls -la ${DEPLOY_PATH}/build || echo "No build directory found" + + echo "Nginx status:" + sudo systemctl is-active nginx + ' + """ + } + } + } + } + + post { + always { + cleanWs() + } + success { + mail to: "${EMAIL_RECIPIENT}", + subject: "✅ Jenkins - ${PROJECT_NAME} Deployment Successful", + body: """The deployment of '${PROJECT_NAME}' to ${REMOTE_SERVER} was successful. + +Build Number: ${BUILD_NUMBER} +URL: ${BUILD_URL} +Time: ${new Date()} + +The static files have been deployed and nginx has been restarted. +""" + } + failure { + mail to: "${EMAIL_RECIPIENT}", + subject: "❌ Jenkins - ${PROJECT_NAME} Deployment Failed", + body: """Deployment failed. Please review logs at: + +${BUILD_URL}console + +Time: ${new Date()} +""" + } + } +} \ No newline at end of file