diff --git a/Dockerfile b/Dockerfile index 511d3651..6a309c40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:24-alpine +FROM node:22-alpine # Create app user and group RUN addgroup app && adduser -S -G app app @@ -15,5 +15,5 @@ COPY . . # Switch to non-root user for runtime USER app -EXPOSE 3000 +EXPOSE 8000 CMD ["node", "app.js"] diff --git a/Jenkins b/Jenkins new file mode 100644 index 00000000..7d028253 --- /dev/null +++ b/Jenkins @@ -0,0 +1,58 @@ +pipeline { + agent any + + environment { + IMAGE_NAME = "sample-node-app" + CONTAINER_NAME = "node-app-container" + PORT = "8000" + } + + stages { + + stage('Clone Repository') { + steps { + git branch: 'dev', + url: 'https://github.com/Jaygohel164/sample-node-project.git' + } + } + + stage('Build Docker Image') { + steps { + sh "docker build -t ${IMAGE_NAME}:latest ." + } + } + + stage('Stop Old Container') { + steps { + sh "docker stop ${CONTAINER_NAME} || true" + sh "docker rm ${CONTAINER_NAME} || true" + } + } + + stage('Run Docker Container') { + steps { + sh """ + docker run -d \ + --name ${CONTAINER_NAME} \ + -p ${PORT}:${PORT} \ + ${IMAGE_NAME}:latest + """ + } + } + + stage('Verify') { + steps { + sh "docker ps | grep ${CONTAINER_NAME}" + } + } + } + + post { + success { + echo "App running at http://3.109.202.16:8000" + } + failure { + sh "docker logs ${CONTAINER_NAME} || true" + } + } +} \ No newline at end of file diff --git a/app.js b/app.js index 007d9367..226d3422 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,7 @@ const express = require('express'); const app = express(); -app.set('port', process.env.PORT || 3000); // Application port is set +app.set('port', process.env.PORT || 8000); // Application port is set app.set('views', __dirname + '/app/server/views'); // Views folder is set app.set('view engine', 'ejs'); // View engine is set app.use(express.static(__dirname + '/app/public')); // Public folder containing static files is set diff --git a/app/server/views/index.ejs b/app/server/views/index.ejs index fe2d4951..ad95a94f 100644 --- a/app/server/views/index.ejs +++ b/app/server/views/index.ejs @@ -1,9 +1,9 @@ - Sample Node.js Application + Sample Node.js Application by Jay Gohel - Sample Node.js Application + Sample Node.js Application by Jay Gohel \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 40c0bbf0..beb012dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,4 +5,4 @@ services: context: . dockerfile: Dockerfile ports: - - "3000:3000" \ No newline at end of file + - "8000:8000" \ No newline at end of file