peachy__kat
peachy__kat

Reputation: 61

NodeJs Jenkins plug-in is not working with dockerfile agent

I'm trying to use NodeJs plug-in on Jenkins. I follow NodeJs document and it work fine with its example code which is using agent any

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                nodejs(nodeJSInstallationName: 'NodeJs test') {
                    sh 'npm config ls'
                }
            }
        }
    }
}

But if I use dockerfile agent like the code below

pipeline {
    options {
        timeout(time:1,unit:'HOURS')
    }
    environment {
        docker_image_name = "myapp-test"
        HTTP_PROXY = "${params.HTTP_PROXY}"
        JENKINS_USER_ID = "${params.JENKINS_USER_ID}"
        JENKINS_GROUP_ID = "${params.JENKINS_GROUP_ID}"
    }
    agent {
        dockerfile {
            additionalBuildArgs '--tag myapp-test --build-arg "JENKINS_USER_ID=${JENKINS_USER_ID}" --build-arg "JENKINS_GROUP_ID=${JENKINS_GROUP_ID}" --build-arg "http_proxy=${HTTP_PROXY}" --build-arg "https_proxy=${HTTP_PROXY}"'
        filename 'Dockerfile'
        dir '.'
        label env.docker_image_name
        }
    }

    stages {
        stage('Build') {
            steps {
                nodejs(nodeJSInstallationName: 'NodeJs test') {
                    sh 'npm config ls'
                }
            }
        }
    }
}

It will return npm: command not found error. My guess is, It couldn't find the path of nodejs... I want to try to export PATH=$PATH:?? too but I also don't know the nodejs path.

How can I make the NodeJS plug-in work with dockerfile?

Upvotes: 1

Views: 276

Answers (2)

peachy__kat
peachy__kat

Reputation: 61

Thank you fredericrous for the answer. Unfortunately in my system, the dockerfile can't be modified. But from your information that

NodeJS plugin won't inject itself into a docker.

I decide to run the NodeJS plugin in another agent instead of dockerfile(running multiple agents) With the code below I manage to run it successfully.

pipeline {
    options {
        timeout(time:1,unit:'HOURS')
    }
    environment {
        docker_image_name = "myapp-test"
        HTTP_PROXY = "${params.HTTP_PROXY}"
        JENKINS_USER_ID = "${params.JENKINS_USER_ID}"
        JENKINS_GROUP_ID = "${params.JENKINS_GROUP_ID}"
    }
    agent {
        dockerfile {
            additionalBuildArgs '--tag myapp-test --build-arg "JENKINS_USER_ID=${JENKINS_USER_ID}" --build-arg "JENKINS_GROUP_ID=${JENKINS_GROUP_ID}" --build-arg "http_proxy=${HTTP_PROXY}" --build-arg "https_proxy=${HTTP_PROXY}"'
        filename 'Dockerfile'
        dir '.'
        label env.docker_image_name
        }
    }

    stages {
        stage('Build') {
            steps {
                sh 'ls'
            }
        }
    }
}
stage('Test'){
  node('master'){
  checkout scm
  try{
      nodejs(nodeJSInstallationName: 'NodeJs test') {
          sh 'npm config ls'
      }
    }
    finally {
      sh 'echo done'
    }
  }
}

Upvotes: 0

fredericrous
fredericrous

Reputation: 3038

NodeJS plugin won't inject itself into a docker. However you could make an ARG build argument in your dockerfile that takes the version of nodeJS to install. You will then need to get read of the nodejs step

Upvotes: 1

Related Questions