feat: deploy both backend (3001) and frontend (3002) on shared Docker network for Dev; backend listens on 3001; robust for gitea server
Some checks failed
DevSecOps-Multibranch/pipeline/head There was a failure building this commit
Some checks failed
DevSecOps-Multibranch/pipeline/head There was a failure building this commit
This commit is contained in:
67
Jenkinsfile
vendored
67
Jenkinsfile
vendored
@@ -1,42 +1,35 @@
|
||||
pipeline {
|
||||
// 1. Run heavy lifting (Build/Push) on the Agent
|
||||
agent { label 'jenkins-agent' }
|
||||
|
||||
environment {
|
||||
// CRITICAL FIX: Use the Registry Name we verified via 'doctl'
|
||||
REGISTRY_URL = 'registry.digitalocean.com/devsecops-lab'
|
||||
|
||||
// CRITICAL FIX: Use 'core' to stay within 1-repo limit
|
||||
REPO_NAME = 'core'
|
||||
|
||||
// Dynamic Tags
|
||||
BACKEND_TAG = "backend-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
||||
FRONTEND_TAG = "frontend-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
|
||||
|
||||
// Deployment Target (The Gitea Server)
|
||||
DEPLOY_HOST = 'gitea.kongseng.in'
|
||||
DEPLOY_USER = 'root'
|
||||
}
|
||||
|
||||
stages {
|
||||
stage('Checkout') {
|
||||
steps { checkout scm }
|
||||
}
|
||||
|
||||
stage('Install Dependencies') {
|
||||
steps {
|
||||
echo "Installing dependencies for ${env.BRANCH_NAME}..."
|
||||
// Ensure folders exist to avoid errors
|
||||
sh 'ls -la'
|
||||
dir('backend') { sh 'npm install' }
|
||||
dir('frontend') { sh 'npm install' }
|
||||
}
|
||||
}
|
||||
|
||||
stage('Build Docker Images') {
|
||||
stage('Deploy') {
|
||||
steps {
|
||||
script {
|
||||
echo "Building Images..."
|
||||
// Define Ports for Dev environment
|
||||
def backendPort = "3001"
|
||||
def frontendPort = "3002"
|
||||
def backendContainer = "backend"
|
||||
def frontendContainer = "frontend"
|
||||
def network = "devsecops-net"
|
||||
def remote = "ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa_deploy ${DEPLOY_USER}@${DEPLOY_HOST}"
|
||||
withCredentials([string(credentialsId: 'do-registry-token', variable: 'DO_TOKEN')]) {
|
||||
// 1. Remote Login
|
||||
sh "${remote} 'echo ${DO_TOKEN} | docker login registry.digitalocean.com -u token --password-stdin'"
|
||||
// 2. Create network if not exists
|
||||
sh "${remote} 'docker network inspect ${network} >/dev/null 2>&1 || docker network create ${network}'"
|
||||
// 3. Pull images
|
||||
sh "${remote} 'docker pull ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG}'"
|
||||
sh "${remote} 'docker pull ${REGISTRY_URL}/${REPO_NAME}:${FRONTEND_TAG}'"
|
||||
// 4. Remove old containers
|
||||
sh "${remote} 'docker stop ${backendContainer} || true'"
|
||||
sh "${remote} 'docker rm ${backendContainer} || true'"
|
||||
sh "${remote} 'docker stop ${frontendContainer} || true'"
|
||||
sh "${remote} 'docker rm ${frontendContainer} || true'"
|
||||
// 5. Run backend
|
||||
sh "${remote} 'docker run -d --name ${backendContainer} --network ${network} --restart always -p ${backendPort}:3001 ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG}'"
|
||||
// 6. Run frontend
|
||||
sh "${remote} 'docker run -d --name ${frontendContainer} --network ${network} --restart always -p ${frontendPort}:80 ${REGISTRY_URL}/${REPO_NAME}:${FRONTEND_TAG}'"
|
||||
echo "SUCCESS: Backend at http://${DEPLOY_HOST}:${backendPort}, Frontend at http://${DEPLOY_HOST}:${frontendPort}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sh "docker build -t ${REGISTRY_URL}/${REPO_NAME}:${BACKEND_TAG} ./backend"
|
||||
sh "docker build -t ${REGISTRY_URL}/${REPO_NAME}:${FRONTEND_TAG} ./frontend"
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ USER nodeapp
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
|
||||
CMD wget --no-verbose --tries=1 --spider http://localhost:3001/health || exit 1
|
||||
|
||||
# 2. Fix Expose to match the app
|
||||
EXPOSE 3000
|
||||
# Expose port 3001 for backend
|
||||
EXPOSE 3001
|
||||
|
||||
# Start the application
|
||||
CMD ["node", "server.js"]
|
||||
|
||||
@@ -19,7 +19,7 @@ const MONGODB_CONNECTION = 'mongodb://admin:supersecret123@localhost:27017/devdb
|
||||
const TWITTER_API_KEY = 'twitter_api_key_1234567890abcdef1234567890abcdef1234567890';
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
|
||||
Reference in New Issue
Block a user