From 41624c1deca97a6d125517e7950e943948514922 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Tue, 15 Jul 2025 16:04:43 +0530 Subject: [PATCH 01/29] Create jenkins --- jenkins | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 jenkins diff --git a/jenkins b/jenkins new file mode 100644 index 0000000..5f54049 --- /dev/null +++ b/jenkins @@ -0,0 +1,33 @@ +pipeline { + agent any + stages{ + stage ("clone") { + steps { + git branch: 'main', url: 'https://github.com/ArunMagi/sample-node-app.git' + } + } + stage ("build") { + steps { + sh "npm install" + } + } + stage ("docker image"){ + steps { + sh "docker build -t techdocker24/node ." + sh "docker images" + } + } + stage("docker hub") { + steps { + sh "docker login -u techdocker24 -p Manikandan@2422" + sh "docker push techdocker24/node" + } + } + stage ("docker container"){ + steps { + sh "docker rm -f node-app || true" + sh "docker run -d --name node-app -p 8087:8080 techdocker24/node" + } + } + } +} From a727361d6b2a6895deb5296f8b814a8bcfca8488 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Tue, 15 Jul 2025 16:07:11 +0530 Subject: [PATCH 02/29] Update jenkins --- jenkins | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins b/jenkins index 5f54049..817969a 100644 --- a/jenkins +++ b/jenkins @@ -3,7 +3,7 @@ pipeline { stages{ stage ("clone") { steps { - git branch: 'main', url: 'https://github.com/ArunMagi/sample-node-app.git' + git branch: 'main', url: 'https://github.com/MANIKANDAN242221/sample-node-app.git' } } stage ("build") { From e5cbcfff0ba3bdbab59f90c29db9c96a2b0e06d6 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 12:17:08 +0530 Subject: [PATCH 03/29] Update Dockerfile --- Dockerfile | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 53651de..612d08e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,18 @@ -FROM node:6.10.3 +# Use modern, lightweight Node version +FROM node:18-alpine -# Create app directory -RUN mkdir -p /usr/src/app +# Set working directory WORKDIR /usr/src/app -# Install app dependencies -COPY package.json /usr/src/app/ -RUN npm install +# Copy only package files first (optimizes Docker cache) +COPY package*.json ./ +RUN npm ci --only=production -# Bundle app source -COPY . /usr/src/app +# Copy the rest of the source code +COPY . . +# Expose the correct port EXPOSE 9000 -CMD [ "npm", "start" ] -#helo + +# Start the app +CMD ["npm", "start"] From 0ca8c2dc3397bb49cf9319f0b3d066e8139f87c7 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 12:17:47 +0530 Subject: [PATCH 04/29] Update jenkins --- jenkins | 68 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/jenkins b/jenkins index 817969a..d875fe0 100644 --- a/jenkins +++ b/jenkins @@ -1,33 +1,71 @@ pipeline { - agent any - stages{ - stage ("clone") { + agent { label 'agent' } + + environment { + AWS_ACCOUNT_ID = '951042686423' + AWS_REGION = 'ap-south-1' + REPO_NAME = 'sample-node-app' + IMAGE_TAG = 'latest' + ECR_REGISTRY = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com" + IMAGE_URI = "${ECR_REGISTRY}/${REPO_NAME}:${IMAGE_TAG}" + GIT_REPO = 'https://github.com/MANIKANDAN242221/sample-node-app.git' + AWS_CREDENTIALS_ID = 'aws-jenkins-creds' // AWS Access Key + Secret + } + + stages { + stage('Clone Repository') { steps { - git branch: 'main', url: 'https://github.com/MANIKANDAN242221/sample-node-app.git' + git url: "${GIT_REPO}", branch: 'main' } } - stage ("build") { + + stage('Build Docker Image') { steps { - sh "npm install" + sh "docker build -t ${IMAGE_URI} ." } } - stage ("docker image"){ + + stage('ECR Login') { steps { - sh "docker build -t techdocker24/node ." - sh "docker images" + withCredentials([ + usernamePassword( + credentialsId: "${AWS_CREDENTIALS_ID}", + usernameVariable: 'AWS_ACCESS_KEY_ID', + passwordVariable: 'AWS_SECRET_ACCESS_KEY' + ) + ]) { + sh """ + aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID + aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY + aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY} + """ + } } } - stage("docker hub") { + + stage('Push to ECR') { steps { - sh "docker login -u techdocker24 -p Manikandan@2422" - sh "docker push techdocker24/node" + sh "docker push ${IMAGE_URI}" } } - stage ("docker container"){ + + stage('Deploy Container') { steps { - sh "docker rm -f node-app || true" - sh "docker run -d --name node-app -p 8087:8080 techdocker24/node" + sh """ + docker stop ${REPO_NAME} || true + docker rm ${REPO_NAME} || true + docker run -d --name ${REPO_NAME} -p 9000:9000 ${IMAGE_URI} + """ } } } + + post { + success { + echo "✅ Pipeline completed successfully!" + } + failure { + echo "❌ Pipeline failed!" + } + } } From 43185ccdc1138072822c9d2e24c996814ada901f Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 12:25:56 +0530 Subject: [PATCH 05/29] Update Dockerfile --- Dockerfile | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 612d08e..af1c488 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,13 @@ -# Use modern, lightweight Node version FROM node:18-alpine -# Set working directory WORKDIR /usr/src/app -# Copy only package files first (optimizes Docker cache) COPY package*.json ./ -RUN npm ci --only=production -# Copy the rest of the source code +RUN npm install --omit=dev + COPY . . -# Expose the correct port EXPOSE 9000 - -# Start the app CMD ["npm", "start"] + From fea017a7addbd418127d2cfa04edffb84cdf5957 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 13:14:45 +0530 Subject: [PATCH 06/29] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7a7c29c..9bbf55f 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,6 @@ const port = 9000; app.get('/', (req, res) => { res.send('

Hello World from Docker and Node.js!

'); }); -app.listen(port, () => { +app.listen(port, '0.0.0.0', () => { console.log(`Listening on port ${port}`); }); From c21635ec81dcfb986d1294e69e2c4f44f32be057 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 14:38:32 +0530 Subject: [PATCH 07/29] Create docker-compose.yml --- docker-compose.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..45cc540 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: '3.8' +services: + app: + build: + context: . + dockerfile: Dockerfile + container_name: sample-node-app + ports: + - "9000:9000" + restart: unless-stopped From 86e0961a6051073a90105109e147220816368106 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:12:11 +0530 Subject: [PATCH 08/29] Update jenkins --- jenkins | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/jenkins b/jenkins index d875fe0..b3059e4 100644 --- a/jenkins +++ b/jenkins @@ -9,7 +9,7 @@ pipeline { ECR_REGISTRY = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com" IMAGE_URI = "${ECR_REGISTRY}/${REPO_NAME}:${IMAGE_TAG}" GIT_REPO = 'https://github.com/MANIKANDAN242221/sample-node-app.git' - AWS_CREDENTIALS_ID = 'aws-jenkins-creds' // AWS Access Key + Secret + AWS_CREDENTIALS_ID = 'aws-jenkins-creds' } stages { @@ -19,13 +19,19 @@ pipeline { } } - stage('Build Docker Image') { + stage('Build Docker Image with Compose') { steps { - sh "docker build -t ${IMAGE_URI} ." + sh 'docker-compose build' } } - stage('ECR Login') { + stage('Tag Image for ECR') { + steps { + sh "docker tag sample-node-app:latest ${IMAGE_URI}" // ✅ Updated tag + } + } + + stage('Login to ECR') { steps { withCredentials([ usernamePassword( @@ -34,11 +40,12 @@ pipeline { passwordVariable: 'AWS_SECRET_ACCESS_KEY' ) ]) { - sh """ + sh ''' aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY + aws configure set default.region ${AWS_REGION} aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY} - """ + ''' } } } @@ -51,18 +58,17 @@ pipeline { stage('Deploy Container') { steps { - sh """ - docker stop ${REPO_NAME} || true - docker rm ${REPO_NAME} || true - docker run -d --name ${REPO_NAME} -p 9000:9000 ${IMAGE_URI} - """ + sh ''' + docker-compose down || true + docker-compose up -d + ''' } } } post { success { - echo "✅ Pipeline completed successfully!" + echo "✅ Pipeline completed successfully using Docker Compose!" } failure { echo "❌ Pipeline failed!" From d0046e53dd5f681e55f55cb18e9a4482e57ff94b Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:13:41 +0530 Subject: [PATCH 09/29] Update docker-compose.yml --- docker-compose.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 45cc540..7ac7364 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,13 @@ version: '3.8' + services: app: build: context: . dockerfile: Dockerfile + image: sample-node-app:latest # ✅ Add this line container_name: sample-node-app ports: - "9000:9000" restart: unless-stopped + From a2a14a9ffb80a10a7a2b72eb3af3588623abd5ca Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:36:40 +0530 Subject: [PATCH 10/29] Update docker-compose.yml --- docker-compose.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 7ac7364..4f430d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,9 +5,12 @@ services: build: context: . dockerfile: Dockerfile - image: sample-node-app:latest # ✅ Add this line + image: sample-node-app:latest container_name: sample-node-app ports: - "9000:9000" + env_file: + - .env # 👈 This line loads your environment variables restart: unless-stopped + From 84f993d7cfaf9f62a107de1e48801b4d20a3f801 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:37:04 +0530 Subject: [PATCH 11/29] Update index.js --- index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 9bbf55f..afa90a2 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,15 @@ const express = require('express'); +require('dotenv').config(); // 👈 Load env variables + const app = express(); -const port = 9000; +const port = process.env.PORT || 9000; +const message = process.env.CUSTOM_MESSAGE || 'Hello World!'; + app.get('/', (req, res) => { - res.send('

Hello World from Docker and Node.js!

'); + res.send(`

${message}

`); }); + app.listen(port, '0.0.0.0', () => { - console.log(`Listening on port ${port}`); + console.log(`🚀 Server running on port ${port}`); }); + From 35acf22e89b3b75e1280683087564c095fd86b65 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:55:24 +0530 Subject: [PATCH 12/29] Update jenkins --- jenkins | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/jenkins b/jenkins index b3059e4..df747f0 100644 --- a/jenkins +++ b/jenkins @@ -19,6 +19,18 @@ pipeline { } } + stage('Ensure .env File Exists') { + steps { + sh ''' + if [ ! -f .env ]; then + echo "PORT=9000" > .env + echo "NODE_ENV=production" >> .env + echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline" >> .env + fi + ''' + } + } + stage('Build Docker Image with Compose') { steps { sh 'docker-compose build' @@ -27,7 +39,7 @@ pipeline { stage('Tag Image for ECR') { steps { - sh "docker tag sample-node-app:latest ${IMAGE_URI}" // ✅ Updated tag + sh "docker tag ${REPO_NAME}:latest ${IMAGE_URI}" } } @@ -73,5 +85,8 @@ pipeline { failure { echo "❌ Pipeline failed!" } + always { + sh 'docker image prune -f' + } } } From 382ffa3dc2551a0e1fd87fa852a33d84abf03b9c Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 15:59:52 +0530 Subject: [PATCH 13/29] Update package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a114d3f..b522c61 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ }, "dependencies": { "express": "^4.15.3", - "nodemon": "^1.11.0" + "nodemon": "^1.11.0", + "dotenv": "^16.0.0" } } From 71b2d9f7c2d0122797f89615baf0b217a4f9be51 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 16:29:52 +0530 Subject: [PATCH 14/29] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4f430d1..610a074 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: image: sample-node-app:latest container_name: sample-node-app ports: - - "9000:9000" + - "9001:9000" env_file: - .env # 👈 This line loads your environment variables restart: unless-stopped From 55cdaaf4a90a0c1d2b77ef69bb8aa3cb063c10d7 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 16:30:52 +0530 Subject: [PATCH 15/29] Update jenkins --- jenkins | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins b/jenkins index df747f0..0a336ae 100644 --- a/jenkins +++ b/jenkins @@ -23,7 +23,7 @@ pipeline { steps { sh ''' if [ ! -f .env ]; then - echo "PORT=9000" > .env + echo "PORT=9001" > .env echo "NODE_ENV=production" >> .env echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline" >> .env fi From 4e256eb4addf12a0e45263dc7a28256ecc41c521 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 16:50:34 +0530 Subject: [PATCH 16/29] Update jenkins --- jenkins | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/jenkins b/jenkins index 0a336ae..a206be2 100644 --- a/jenkins +++ b/jenkins @@ -1,5 +1,5 @@ pipeline { - agent { label 'agent' } + agent none // 👈 All stages define their own agent environment { AWS_ACCOUNT_ID = '951042686423' @@ -14,12 +14,14 @@ pipeline { stages { stage('Clone Repository') { + agent { label 'master' } steps { git url: "${GIT_REPO}", branch: 'main' } } stage('Ensure .env File Exists') { + agent { label 'master' } steps { sh ''' if [ ! -f .env ]; then @@ -32,18 +34,21 @@ pipeline { } stage('Build Docker Image with Compose') { + agent { label 'master' } steps { sh 'docker-compose build' } } stage('Tag Image for ECR') { + agent { label 'master' } steps { sh "docker tag ${REPO_NAME}:latest ${IMAGE_URI}" } } stage('Login to ECR') { + agent { label 'master' } steps { withCredentials([ usernamePassword( @@ -63,12 +68,14 @@ pipeline { } stage('Push to ECR') { + agent { label 'master' } steps { sh "docker push ${IMAGE_URI}" } } stage('Deploy Container') { + agent { label 'agent' } // 👈 Run container on agent steps { sh ''' docker-compose down || true @@ -79,14 +86,17 @@ pipeline { } post { + always { + agent { label 'master' } // Cleanup on master + steps { + sh 'docker image prune -f' + } + } success { - echo "✅ Pipeline completed successfully using Docker Compose!" + echo "✅ Pipeline completed successfully!" } failure { echo "❌ Pipeline failed!" } - always { - sh 'docker image prune -f' - } } } From 85601c943fe2d9a7a25bb03237fa5e68a60bb408 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 18:34:17 +0530 Subject: [PATCH 17/29] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 610a074..4f430d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: image: sample-node-app:latest container_name: sample-node-app ports: - - "9001:9000" + - "9000:9000" env_file: - .env # 👈 This line loads your environment variables restart: unless-stopped From c6f667e38e4c50fbbd0a58b846f35cc2820ff37d Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Fri, 8 Aug 2025 18:43:53 +0530 Subject: [PATCH 18/29] Update jenkins --- jenkins | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins b/jenkins index a206be2..7c52003 100644 --- a/jenkins +++ b/jenkins @@ -25,7 +25,7 @@ pipeline { steps { sh ''' if [ ! -f .env ]; then - echo "PORT=9001" > .env + echo "PORT=9000" > .env echo "NODE_ENV=production" >> .env echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline" >> .env fi From 02907cdd46b965dc5fa9853f31eaa0f1c9eba553 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Sat, 9 Aug 2025 15:49:31 +0530 Subject: [PATCH 19/29] Update docker-compose.yml --- docker-compose.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4f430d1..96d168f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,7 +10,5 @@ services: ports: - "9000:9000" env_file: - - .env # 👈 This line loads your environment variables + - .env restart: unless-stopped - - From 62efa99c71604a171b9a4166abf0d8ddc21fcc94 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Sat, 9 Aug 2025 15:49:59 +0530 Subject: [PATCH 20/29] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eb17994..e2f5ab0 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,4 @@ hello + Jenkins From d6d6f82c252e7665ed238c5f9800b3f376b50c76 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Sat, 9 Aug 2025 15:50:45 +0530 Subject: [PATCH 21/29] Update deployment.yaml --- node-app/deployment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node-app/deployment.yaml b/node-app/deployment.yaml index e0ee286..1322cbe 100644 --- a/node-app/deployment.yaml +++ b/node-app/deployment.yaml @@ -14,7 +14,7 @@ spec: spec: containers: - name: node-app - image: arunmagi/node-app + image: techdocker24/node-app ports: - containerPort: 9000 --- From f12b0f6ed87ed2295fb4d34aae73fc97ad583331 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Sat, 9 Aug 2025 15:57:20 +0530 Subject: [PATCH 22/29] Update jenkins --- jenkins | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins b/jenkins index 7c52003..69074bb 100644 --- a/jenkins +++ b/jenkins @@ -27,7 +27,7 @@ pipeline { if [ ! -f .env ]; then echo "PORT=9000" > .env echo "NODE_ENV=production" >> .env - echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline" >> .env + echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline path" >> .env fi ''' } From b56bd55c14c0d39bf14072807f22c3a8c4936598 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 11:16:31 +0530 Subject: [PATCH 23/29] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 96d168f..cc7873e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,5 +10,5 @@ services: ports: - "9000:9000" env_file: - - .env + - .env restart: unless-stopped From 612c78db168ceba520d45e4dbbed710cfcdac029 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 11:17:08 +0530 Subject: [PATCH 24/29] Update Dockerfile --- Dockerfile | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index af1c488..fce509c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,12 +2,19 @@ FROM node:18-alpine WORKDIR /usr/src/app +# Install dependencies COPY package*.json ./ - RUN npm install --omit=dev +# Copy all source code COPY . . +# Build frontend (React/Vue/etc.) +RUN npm run build + +# Serve built frontend via Express (public folder) +# Make sure index.js has: app.use(express.static('public')); +RUN mkdir -p public && cp -r build/* public/ + EXPOSE 9000 CMD ["npm", "start"] - From b9ce7daea3ffc264888a1976fee8673aaff7ec61 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 11:17:45 +0530 Subject: [PATCH 25/29] Update jenkins --- jenkins | 67 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/jenkins b/jenkins index 69074bb..ed09374 100644 --- a/jenkins +++ b/jenkins @@ -1,14 +1,14 @@ pipeline { - agent none // 👈 All stages define their own agent + agent none environment { - AWS_ACCOUNT_ID = '951042686423' - AWS_REGION = 'ap-south-1' - REPO_NAME = 'sample-node-app' - IMAGE_TAG = 'latest' - ECR_REGISTRY = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com" - IMAGE_URI = "${ECR_REGISTRY}/${REPO_NAME}:${IMAGE_TAG}" - GIT_REPO = 'https://github.com/MANIKANDAN242221/sample-node-app.git' + AWS_ACCOUNT_ID = '951042686423' + AWS_REGION = 'ap-south-1' + REPO_NAME = 'sample-node-app' + IMAGE_TAG = 'latest' + ECR_REGISTRY = "${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com" + IMAGE_URI = "${ECR_REGISTRY}/${REPO_NAME}:${IMAGE_TAG}" + GIT_REPO = 'https://github.com/MANIKANDAN242221/sample-node-app.git' AWS_CREDENTIALS_ID = 'aws-jenkins-creds' } @@ -20,23 +20,40 @@ pipeline { } } - stage('Ensure .env File Exists') { + stage('Ensure .env File') { agent { label 'master' } steps { sh ''' - if [ ! -f .env ]; then - echo "PORT=9000" > .env - echo "NODE_ENV=production" >> .env - echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline path" >> .env - fi + echo "PORT=9000" > .env + echo "NODE_ENV=production" >> .env + echo "CUSTOM_MESSAGE=Hello from Jenkins pipeline" >> .env ''' } } - stage('Build Docker Image with Compose') { + stage('Install Dependencies & Build UI') { agent { label 'master' } steps { - sh 'docker-compose build' + sh ''' + echo "📦 Installing Node.js dependencies..." + npm install + + echo "🏗️ Building frontend..." + npm run build + ''' + } + } + + stage('Build Docker Image') { + agent { label 'master' } + steps { + sh ''' + echo "🗑️ Removing old Docker image..." + docker rmi -f ${REPO_NAME}:latest || true + + echo "🐳 Building new Docker image..." + docker-compose build --no-cache + ''' } } @@ -61,21 +78,22 @@ pipeline { aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY aws configure set default.region ${AWS_REGION} - aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY} + aws ecr get-login-password --region ${AWS_REGION} \ + | docker login --username AWS --password-stdin ${ECR_REGISTRY} ''' } } } - stage('Push to ECR') { + stage('Push Image to ECR') { agent { label 'master' } steps { sh "docker push ${IMAGE_URI}" } } - stage('Deploy Container') { - agent { label 'agent' } // 👈 Run container on agent + stage('Deploy Container on Agent') { + agent { label 'agent' } steps { sh ''' docker-compose down || true @@ -87,16 +105,13 @@ pipeline { post { always { - agent { label 'master' } // Cleanup on master - steps { - sh 'docker image prune -f' - } + echo '🧹 Cleaning up...' } success { - echo "✅ Pipeline completed successfully!" + echo '✅ Pipeline executed successfully!' } failure { - echo "❌ Pipeline failed!" + echo '❌ Pipeline failed!' } } } From 4017c72e49a4072fd668f10f80b84aff5f26c62c Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 12:04:52 +0530 Subject: [PATCH 26/29] Update package.json --- package.json | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index b522c61..1edd8ba 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,20 @@ { "name": "docker-node-example", "version": "1.0.0", + "description": "Sample Node.js Express app for Docker & Jenkins", "main": "index.js", "scripts": { - "start": "nodemon index.js" + "start": "node index.js", + "dev": "nodemon index.js", + "build": "echo 'No build step needed for backend API'" }, "dependencies": { - "express": "^4.15.3", - "nodemon": "^1.11.0", + "express": "^4.18.2", "dotenv": "^16.0.0" - } + }, + "devDependencies": { + "nodemon": "^3.0.2" + }, + "author": "", + "license": "ISC" } From 6b35cf2783a206f3e052774b959e05afbd1e222a Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 12:09:20 +0530 Subject: [PATCH 27/29] Update Dockerfile --- Dockerfile | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index fce509c..340d1e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,19 +2,17 @@ FROM node:18-alpine WORKDIR /usr/src/app -# Install dependencies +# Copy package.json and package-lock.json first for caching COPY package*.json ./ -RUN npm install --omit=dev -# Copy all source code -COPY . . - -# Build frontend (React/Vue/etc.) -RUN npm run build +# Install dependencies (including dev if you need nodemon in container) +RUN npm install -# Serve built frontend via Express (public folder) -# Make sure index.js has: app.use(express.static('public')); -RUN mkdir -p public && cp -r build/* public/ +# Copy application source code +COPY . . +# Expose the application port EXPOSE 9000 + +# Start the app CMD ["npm", "start"] From 87319318f320791755f7afdf9a914e489fc70cef Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 14:39:00 +0530 Subject: [PATCH 28/29] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index afa90a2..577224e 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,6 @@ app.get('/', (req, res) => { }); app.listen(port, '0.0.0.0', () => { - console.log(`🚀 Server running on port ${port}`); + console.log(`🚀 Server running on port 9000`); }); From 5ec423b4b84408f7589cb8202e7fa99d1f0c8aa6 Mon Sep 17 00:00:00 2001 From: Manikandan G <158708882+MANIKANDAN242221@users.noreply.github.com> Date: Mon, 11 Aug 2025 14:43:15 +0530 Subject: [PATCH 29/29] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 577224e..afa90a2 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,6 @@ app.get('/', (req, res) => { }); app.listen(port, '0.0.0.0', () => { - console.log(`🚀 Server running on port 9000`); + console.log(`🚀 Server running on port ${port}`); });