npm install fails in jenkins pipeline in docker

前端 未结 12 907
野性不改
野性不改 2020-12-07 17:45

I\'m following a tutorial about Jenkins pipeline and I can get a \"hello world\" working under at node 6.10 docker container.

But, when I added a default EmberJS app

相关标签:
12条回答
  • 2020-12-07 18:11

    from https://github.com/jenkins-infra/jenkins.io/blob/master/Jenkinsfile

    docker.image('openjdk:8').inside {
        /* One Weird Trick(tm) to allow git(1) to clone inside of a
        * container
        */
        withEnv([
            /* Override the npm cache directory to avoid: EACCES: permission denied, mkdir '/.npm' */
            'npm_config_cache=npm-cache',
            /* set home to our current directory because other bower
            * nonsense breaks with HOME=/, e.g.:
            * EACCES: permission denied, mkdir '/.config'
            */
            'HOME=.',
        ]) {
                // your code
        }
    }
    
    0 讨论(0)
  • 2020-12-07 18:12

    I add the same issue. I solved it using the root user to run the Docker image:

    node {
        stage("Prepare environment") {
            checkout scm
            // Build the Docker image from the Dockerfile located at the root of the project
            docker.build("${JOB_NAME}")
        }
    
        stage("Install dependencies") {
            // Run the container as `root` user
            // Note: you can run any official Docker image here
            withDockerContainer(args: "-u root", image: "${JOB_NAME}") {
                sh "npm install"
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 18:16

    In my case this solved the problem

                agent {
                    docker {
                        image 'node:10-stretch'
                        args '-v /home/jenkins/.ssh:/home/jenkins/.ssh:ro -u 0'
                    }
                }
    
    0 讨论(0)
  • 2020-12-07 18:22

    In my case the problem was that inside the container I was user jenkins instead of root. I got there by setting whoami inside the container and got error like cannot determine user 111 (which happens to be jenkins). So I did the following:

    stage('Run build') {
            webappImage.inside("-u root") {
                sh "yarn run build"
            }
        }
    
    0 讨论(0)
  • 2020-12-07 18:23

    Adding the environments and setting the Home to '.' solves this as below.

    pipeline {
        agent { docker { image 'node:8.12.0' } }
        environment {
            HOME = '.'
        }
        stages {
            stage('Clone') {
                steps {
                    git branch: 'master',
                        credentialsId: '121231k3jkj2kjkjk',
                        url: 'https://myserver.com/my-repo.git'
                }
            }
            stage('Build') {
                steps {
                    sh "npm install"
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 18:23

    You can install nvm on the fly before building, in a local directory with NVM_DIR without setting it as global dependency :

    mkdir -p node_dir
    export NVM_DIR=$(pwd)/node_dir
    curl https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
    source $(pwd)/node_dir/nvm.sh
    nvm install 7
    nvm use 7
    

    The new locations are :

    $ which node
    ~/someDir/node_dir/versions/node/v7.7.2/bin/node
    
    $ which npm
    ~/someDir/node_dir/versions/node/v7.7.2/bin/npm
    
    0 讨论(0)
提交回复
热议问题