jenkinsdockerfilejenkins-groovynvm

Custom Docker in Jenkins throws script.sh: 1: npm: Permission denied


I am trying to run a npm command during a Jenkins build inside a custom docker image. During the build I get errors for everything concerning node or npm: Behaviour while just trying to check versioning:

/var/jenkins_home/workspace/Frontend-testing-v2/vd2Deployed@tmp/durable-c1cb5884/script.sh: 1: node: Permission denied script returned exit code 127

This is my dockerfile:

FROM ubuntu:22.04

ENV SHELL /bin/bash

# Install nvm with node and npm
RUN apt update && apt install -y nano git gnupg curl wget net-tools dpkg

# Update Linux
RUN apt-get update -y && \
    apt-get upgrade -y

ENV NODE_VERSION v18.18.2
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
RUN . ~/.nvm/nvm.sh && \
    nvm install $NODE_VERSION && \
    nvm use ${NODE_VERSION} && \
    nvm alias default $NODE_VERSION && \
    nvm use default

# add node and npm to path so the commands are available
ENV NODE_PATH ~/.nvm/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH /root/.nvm/versions/node/$NODE_VERSION/bin:$PATH

The important parts of my jenkinsfile look the following:

withEnv([
       'npm_config_cache=npm-cache',
       'HOME=.',
     ]) {
 def wdi5Image = docker.build('wdi5-image', './')
 int statusCode = 99
 wdi5Image.inside {
 //sh "chmod 777 -R ${env.WORKSPACE}/${launchpadName}"
 sh 'node --version'
 sh 'npm --version'
 statusCode = sh script:"npm run ${launchpadName}", returnStatus: true
 }}

When trying to solve the issue I have tried to set permissions for the ~/.nvm directory, inside of the jenkins script and inside of the dockerfile. Additionally I tried to set user to 1002 and set all the permissions for nvm etc. for that user, which also didn't work. I seem to have root permissions, but anything that has to do with node or npm results in the permission error.

I assume I am missing something glaringly obvious, but after hours of trying and not succeeding, it would be amazing if you were able to help.

I would be happy to supply any additional info.

Best of wishes, Pascal


Solution

  • Solution: Hello. I solved the issue by installing nvm in another path, it seems to have been a permission error, that did not allow me to properly install nvm / node. This how I solved it:

    ENV NODE_VERSION v18.18.2
    RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
    # Create the directory to store nvm
    RUN mkdir -p /usr/local/nvm
    ENV NVM_DIR /usr/local/nvm
    ENV NODE_VERSION v18.18.2
    
    # Download NVM
    RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
    RUN /bin/bash -c "source $NVM_DIR/nvm.sh"
    
    # Add NVM to Path
    ENV NODE_PATH ${NVM_DIR}/versions/node/${NODE_VERSION}/lib/node_modules
    ENV PATH ${NVM_DIR}/versions/node/${NODE_VERSION}/bin:$PATH
    ENV PATH ${NVM_DIR}/.nvm:$PATH
    
    # --delete-prefix necessary?
    RUN . ${NVM_DIR}/nvm.sh && \
        nvm install ${NODE_VERSION} && \
        nvm use --delete-prefix ${NODE_VERSION} && \
        nvm alias default ${NODE_VERSION} && \
        nvm use default