简体   繁体   中英

How to user pm2 and jenkins to run the 'sudo npm start'?

I have added the jenkins user to had all permission.

jenkins ALL=(ALL) NOPASSWD: ALL

also installed the pm2 as globally, and pm2 need to run command

sudo npm start

as build process needs to create some directory, if I run sudo npm start in the app1 directory it works perfectly however with pm2 command it throws access error

sudo pm2 start config.js

config.js

  module.exports = {
      apps : [{
        name   : "app1",
        cwd : "./my-dir/app1,
        script : "sudo npm",
        args : "start"
      },{
        name   : "app2",
        cwd : "./my-dir/app2",
        script : "npm",
        args : "start"
      }]
    }

Is it error with pm2 permission or user permission?

Instead of running the command with all the sudo privileges create the user with the least number of privileges that you want to give to your app.

You already have the jenkins set to use sudo with no password, so you can use the below command to run the pm2 from jenkins as other user with least privilege.

sudo su - username -c 'pm2 start config.js'

The above command will be run as the specified user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM