From 37877c56ac81702970daff585afc1b8c61293da8 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 15:59:54 +1300
Subject: [PATCH 01/11] Add initial Jenkinsfile

---
 Jenkinsfile | 10 ++++++++++
 1 file changed, 10 insertions(+)
 create mode 100644 Jenkinsfile

diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 000000000..177e014de
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,10 @@
+pipeline {
+    agent any
+    stages {
+        stage('Build') { 
+            steps {
+                sh 'npm install' 
+            }
+        }
+    }
+}

From 37782c7c38546f0fcaec08584c4071312678ae80 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 16:06:22 +1300
Subject: [PATCH 02/11] Update Jenkinsfile

---
 Jenkinsfile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index 177e014de..88638b173 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,5 +1,5 @@
 pipeline {
-    agent any
+    agent { docker { image 'node:22.11.0-alpine3.20' } }
     stages {
         stage('Build') { 
             steps {

From 5e84b628108b5dded0f786bea6f91038db5358f0 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 16:11:53 +1300
Subject: [PATCH 03/11] Add test stage

---
 Jenkinsfile | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/Jenkinsfile b/Jenkinsfile
index 88638b173..cba694eb7 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -6,5 +6,10 @@ pipeline {
                 sh 'npm install' 
             }
         }
+         stage('Test') {
+            steps {
+                sh './jenkins/scripts/test.sh'
+            }
+        }
     }
 }

From 4e673d20112dcfe1ce7a4b4e4672a0c99286b2f6 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 16:16:07 +1300
Subject: [PATCH 04/11] Change Jenkins agent

---
 Jenkinsfile | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index cba694eb7..c94f3e3c1 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,5 +1,10 @@
 pipeline {
-    agent { docker { image 'node:22.11.0-alpine3.20' } }
+    agent {
+        docker {
+            image 'node:lts-buster-slim'
+            args '-p 3000:3000'
+        }
+    }
     stages {
         stage('Build') { 
             steps {

From 84aecb77b4c0c4332d1943759067d059ba3626c1 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 16:18:24 +1300
Subject: [PATCH 05/11] Add Deliver stage

---
 Jenkinsfile | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/Jenkinsfile b/Jenkinsfile
index c94f3e3c1..4f7dc385e 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -16,5 +16,12 @@ pipeline {
                 sh './jenkins/scripts/test.sh'
             }
         }
+          stage('Deliver') {
+            steps {
+                sh './jenkins/scripts/deliver.sh'
+                input message: 'Finished using the web site? (Click "Proceed" to continue)'
+                sh './jenkins/scripts/kill.sh'
+            }
+        }        
     }
 }

From a387cb58db442746dce9b15ea8a0544741c44dfa Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 16:21:11 +1300
Subject: [PATCH 06/11] Update TCP port

---
 Jenkinsfile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index 4f7dc385e..885ca0c6b 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -2,7 +2,7 @@ pipeline {
     agent {
         docker {
             image 'node:lts-buster-slim'
-            args '-p 3000:3000'
+            args '-p 3333:3000'
         }
     }
     stages {

From 34f6da476e7b199b5dde5ce7e7a95ae53c8a6ce4 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 17:07:13 +1300
Subject: [PATCH 07/11] Update Jenkinsfile

---
 Jenkinsfile | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/Jenkinsfile b/Jenkinsfile
index 885ca0c6b..f4ab6ac41 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -6,6 +6,9 @@ pipeline {
         }
     }
     stages {
+        stage('Clean Workspace'){
+            cleanWs()
+        }
         stage('Build') { 
             steps {
                 sh 'npm install' 
@@ -24,4 +27,27 @@ pipeline {
             }
         }        
     }
+    post {
+        always {
+            /* 
+            Record and aggregate test results.
+            This will always grab the test results and let Jenkins track them, 
+            calculate trends and report on them. 
+            A Pipeline that has failing tests will be marked as "UNSTABLE", 
+            denoted by yellow in the web UI. 
+            That is distinct from the "FAILED" state, denoted by red.
+            */
+            junit 'build/reports/**/*.xml'            
+        }
+        failure {
+            echo 'This will run only if failed'
+            mail to: 'michael.zhang@mclarens.co.nz',
+                 subject: "Failed pipeline: ${currentBuild.fullDisplayName}",
+                 body: "Something is wrong with ${env.BUILD_URL}"
+        }
+        cleanup {
+            echo 'Cleaning the workspace'
+            cleanWs()
+        }
+    }
 }

From 3b13ddc00312ce0fae0d26a13eddf6ee6fc4ff57 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 17:07:58 +1300
Subject: [PATCH 08/11] Update Jenkinsfile

---
 Jenkinsfile | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index f4ab6ac41..bb11812c3 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -6,9 +6,6 @@ pipeline {
         }
     }
     stages {
-        stage('Clean Workspace'){
-            cleanWs()
-        }
         stage('Build') { 
             steps {
                 sh 'npm install' 

From 057b88cb779891c0eb9e6b20ab5536e25cd1c70b Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Fri, 22 Nov 2024 17:14:27 +1300
Subject: [PATCH 09/11] Disabled workspace cleanup step.

---
 Jenkinsfile | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/Jenkinsfile b/Jenkinsfile
index bb11812c3..d96ebb05f 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -42,9 +42,14 @@ pipeline {
                  subject: "Failed pipeline: ${currentBuild.fullDisplayName}",
                  body: "Something is wrong with ${env.BUILD_URL}"
         }
+        /*
+        Cleaning the workspace will delete all test reports,
+        bringing a failure result to the pipeline.
+        Therefore, I commented out this step. 
         cleanup {
             echo 'Cleaning the workspace'
             cleanWs()
         }
+        */
     }
 }

From c1f1d3092bc98a1a3fffcd4472ad29eb347ea460 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Sun, 1 Dec 2024 21:50:52 +1300
Subject: [PATCH 10/11] Create Dockerfile

---
 Dockerfile | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100644 Dockerfile

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 000000000..2d8fb091a
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,16 @@
+# Specify the base image
+FROM node:alpine
+# Set the working directory
+WORKDIR /app
+# Copy the package.json and package-lock.json files
+COPY package*.json ./
+# Install the dependencies
+RUN npm install
+# Copy the app files
+COPY . .
+# Build the app
+RUN npm run build
+# Expose the port
+EXPOSE 3000
+# Run the app
+CMD ["npm", "start"]

From 0645ada98139d04b246172812b12319f05b85a92 Mon Sep 17 00:00:00 2001
From: Michael Zhang <michael.zhang@mclarens.co.nz>
Date: Sun, 1 Dec 2024 22:42:21 +1300
Subject: [PATCH 11/11] Update Jenkinsfile

---
 Jenkinsfile | 93 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 61 insertions(+), 32 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index d96ebb05f..f115e70f7 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,40 +1,78 @@
 pipeline {
-    agent {
-        docker {
-            image 'node:lts-buster-slim'
-            args '-p 3333:3000'
-        }
+    agent any
+    environment {
+        IMAGE_NAME = "mz-react-todo"
+        IMAGE_TAG = "latest"
+        NEXUS_URL = "nginx-nexus.connectme365.com/hosted"
+        DOCKER_SWARM_MANAGER = "jenkins@10.3.31.84"
+        NEXUS_CREDENTIALS_ID = "jenkins-mz-rd"
+        SSH_CREDENTIALS_ID = "publish_via_ssh"
     }
     stages {
-        stage('Build') { 
+        stage('Clone Repo'){
+            steps{
+                echo "Clean the workspace"
+                deleteDir()
+                echo "Cloning source code ..."
+                git branch: 'master', credentialsId: 'systemservices-github', url: 'git@github.com:mcl-michaelz/simple-node-js-react-npm-app.git'
+            }
+        }
+        stage('Build App'){
+            agent {
+                docker {
+                    image 'node:16'
+                    args '-u root'
+                    // Run the container on the node specified at the
+                    // top-level of the Pipeline, in the same workspace,
+                    // rather than on a new node entirely:
+                    reuseNode true
+                }
+            }
             steps {
-                sh 'npm install' 
+                echo "Building React App ..."
+                sh '''
+                    pwd & ls -l
+                    npm install
+                    npm run build
+                '''
             }
         }
-         stage('Test') {
+        stage('Build Docker Image') {
             steps {
-                sh './jenkins/scripts/test.sh'
+                echo 'Building Docker image...'
+                sh '''
+                    docker build -t ${NEXUS_URL}/${IMAGE_NAME}:${IMAGE_TAG} .
+                '''
             }
         }
-          stage('Deliver') {
+        stage('Push Image to Nexus') {
             steps {
-                sh './jenkins/scripts/deliver.sh'
-                input message: 'Finished using the web site? (Click "Proceed" to continue)'
-                sh './jenkins/scripts/kill.sh'
+                echo 'Pushing Docker image to Nexus...'
+                withCredentials([usernamePassword(credentialsId: "${NEXUS_CREDENTIALS_ID}", usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASS')]) {
+                    sh '''
+                        echo ${NEXUS_PASS} | docker login ${NEXUS_URL} -u ${NEXUS_USER} --password-stdin
+                        docker push ${NEXUS_URL}/${IMAGE_NAME}:${IMAGE_TAG}
+                    '''
+                }
             }
-        }        
+        } 
+        stage('Deploy to Docker Swarm') {
+            steps {
+                echo 'Deploying to Docker Swarm...'
+                withCredentials([sshUserPrivateKey(credentialsId: "${SSH_CREDENTIALS_ID}", keyFileVariable: 'PK')]) {
+                    sh '''
+                        ssh -i $PK -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" ${DOCKER_SWARM_MANAGER} "
+                        docker service update --image ${NEXUS_URL}/${IMAGE_NAME}:${IMAGE_TAG} ${IMAGE_NAME} ||
+                        docker service create --name ${IMAGE_NAME} --replicas 3 ${NEXUS_URL}/${IMAGE_NAME}:${IMAGE_TAG}"
+                    '''
+                }
+            }
+        }
     }
     post {
         always {
-            /* 
-            Record and aggregate test results.
-            This will always grab the test results and let Jenkins track them, 
-            calculate trends and report on them. 
-            A Pipeline that has failing tests will be marked as "UNSTABLE", 
-            denoted by yellow in the web UI. 
-            That is distinct from the "FAILED" state, denoted by red.
-            */
-            junit 'build/reports/**/*.xml'            
+        }
+        success {
         }
         failure {
             echo 'This will run only if failed'
@@ -42,14 +80,5 @@ pipeline {
                  subject: "Failed pipeline: ${currentBuild.fullDisplayName}",
                  body: "Something is wrong with ${env.BUILD_URL}"
         }
-        /*
-        Cleaning the workspace will delete all test reports,
-        bringing a failure result to the pipeline.
-        Therefore, I commented out this step. 
-        cleanup {
-            echo 'Cleaning the workspace'
-            cleanWs()
-        }
-        */
     }
 }
