0

I have created a Jenkins-file, which first pulls the sources of a Express-app from the GitHub-Repository, then installs the dependencies, then starts the Express-App.

pipeline {
    agent any
    tools {
       nodejs 'NodeJS'
    }
    stages {
        stage('Build') {
            steps {
               sh 'npm install'
               echo "install dependencies."
            }
        }
        stage('Deploying') {
            steps {
               sh 'node index.js'
               echo "run express-app ..."
            }
        }
    }
}

Now I have configured "Scan Repository Triggers" to 15 minutes. So, that Jenkins runs the Jenkins-file every 15 minutes, in case there have been changes in the GitHub-repository. The problem is, that the previous app is still running and occupying the port, which is defined in the sources.

How can I stop the older, running app and replace it with the updated app? The target is, that the respective most current version of the app is supplied, if one enters the URL.

cluster1
  • 4,968
  • 6
  • 32
  • 49

1 Answers1

1

There are multiple ways to get this done. One way is to use something like nodemon. Another clean way to manage our node server is by using something like forever. Then you can Gracefully manage the server.

forever start app.js

forever restart app.js

If you don't want to rely on additional tools. You can kill the Node server before starting it again. There are multiple ways to do this. One option is to get the process ID by the port and then kill the server. You can refer to this question.

ycr
  • 12,828
  • 2
  • 25
  • 45