Search code examples
node.jswindowsnext.jsazure-devopspm2

running PM2 on windows server using classic azure pipeline


I am trying to build release pipeline using classic azure pipelines for nextjs project to deploy it on self hosted windows server and I used iis as a redirection server to the nextjs application, my issue is how can I keep the nextjs application running to enable redirection to it from iis?

  • I tried npm run start in CMD and it keeps the release pipeline running
  • I tried iisnode and it cause errors
  • I tried using PM2/ PM2-runtime and it keeps the release pipeline in running state .

tasks which I used

 steps:
    - task: NodeTool@0
      displayName: 'Use Node 20.x'
      inputs:
        versionSpec: 20.x
    steps:
- powershell: |
   npm i
  workingDirectory: 'some directory'
  displayName: 'npm i'

steps:
- powershell: |
   npm install pm2 -g
   
  workingDirectory: 'some directory'
  displayName: 'npm install pm2 -g'

steps:
- powershell: 'pm2-runtime start ecosystem.config.js --env production'
  workingDirectory: 'some directory'
  displayName: 'pm2-runtime start ecosystem.config.js --env production'
  env:
    HOME: ~

Solution

  • I can reproduce the same situation when using the same pm2-runtime start command.

    The Pipeline will keep running after the app starting.

    enter image description here

    As far as I know, pm2-runtime designed for Docker container which keeps an application in the foreground which keep the container running,

    pm2 is designed for normal usage where you send or run the application in the background.

    To solve this issue, you can change to use the pm2 command for the react.js app on windows:

    pm2 start ecosystem.config.js --env production --watch
    

    Result:

    enter image description here

    Update:

    To avoid the Finalize job clean the process, you can set the variable: process.clean = false in Release Pipeline

    enter image description here

    Result:

    enter image description here