1

Is this really such an uncommon situation that I can't find answers anywhere? Anyway. I have a Ubuntu VM server on DigitalOcean and I am running Jenkins on this server to do build, test and local deploy of a web app (essentially copy the web app directory to other location and restart the systemctl service that runs it).

Issue is that the user that jenkins generates in the system (with which you can't login btw), has somehow messed up node modules directory. It is installed in the jenkins home folder /var/lib/jenkins/, when performing npm install (despite there being no -g option), instead of build deploy directory and package resolution from within the application can not resolve modules installed in jenkins home (despite being run by jenkins, so by jenkins user).

I suspect that the solution would be to allow somehow managed installation of node, such that every user can install and require global modules without the need to use sudo, but so far every solution I looked up (nvm, n-install, various scripts) are for single user, or sudo. I can not really do anything to the jenkins (or any other program-generated) user. So what's the best solution in this case?

Megakoresh
  • 111
  • 3

1 Answers1

0

Looks like it really is surprisingly uncommon case, judging by absence of any activity. Anyway my solution now is to include node.js installation into the pipeline in my Jenkinsfile and run it using sh command. The installation script looks like this:

#!/usr/bin/env bash
command_exists () {
  command -v $1 >/dev/null 2>&1
}

if command_exists 'node' && command_exists 'npm'; then
  echo 'Node installed, exiting'
  exit 0
else
  if [ -d $HOME/n ]; then
    echo 'n-install directory exists, path likely not configured, exiting'
    exit 0
  fi
  echo 'Node not installed, installing'
  curl -L https://git.io/n-install | bash -s -- -y
  echo 'Reloading bashrc'
  . $HOME/.bashrc
  if command_exists 'node' && command_exists 'npm'; then
    echo 'Node installed successfully. Updating npm'
    npm i -g npm
    echo 'Node version:'
    node -v
    echo 'NPM version:'
    npm -v
    echo 'NPM prefix:'
    npm config get prefix
    exit 0
  else
    echo 'Node installation failed!'
    exit 1
  fi
fi

That is, of course, not ideal, since it doesn't actually install node.js and make it available for every user, but have private npm repositories for each so they dont need sudo to install or read them. Therefore I have to separately make sure that the user which the systemd service uses to run the app also has node.js installed and I have to do that manually.

If anyone has a better solution, tell me. I'll mark this as the answer if nothing better shows up.

Megakoresh
  • 111
  • 3