I am running Jenkins from a Dockerfile, and have successfully integrated with Bitbucket cloud using the local docker.sock. I have setup a new pipeline using Blue Ocean which downloads the repo and builds a simple nodejs app from the Dockerfile.
My files look like this :
Node app
FROM node:8.6.0-slim
WORKDIR app
# Install dependencies
COPY package.json .
RUN npm install
RUN ls
# Import source files
COPY . .
Jenkinsfile
pipeline {
agent { dockerfile true }
stages {
stage('Test') {
steps {
sh 'ls -l'
sh 'npm test'
}
}
}
}
However when I run the npm test step, it is complaining that it can't find the node_modules. The step works fine if I run npm install from the Jenkinsfile like sh 'npm install'.
Maybe I am missing something but it looks like jenkins is not running the steps in the build image, which means I have to replicate the npm install and potentially other commands both in the app Dockerfile and the Jenkinsfile.
Is there a way to run the tests in the image originally built or do I have to replicate the build steps from the Dockerfile to the Jenkinsfile?
Jenkins mounts the project in its own directory. If you use the dockerfile arg it won't necessarily install your dependencies in the right place.
If you look in the console for your build you should see a line like this:
docker run -t -d -u 500:500 -w /var/lib/jenkins/workspace/ev-team_***_master-D5DTMAWQCFAZ7O7BLEJB3CZGNIHTH72HCI7AEOKAAA7A4XQN3COQ -v /var/lib/jenkins/workspace/ev-***_master-D5DTMAWQCFAZ7O7BLEJB3CZGNIHTH72HCI7AEOKAAA7A4XQN3COQ:/var/lib/jenkins/workspace/ev-team_**_master-D5DTMAWQCFAZ7O7BLEJB3CZGNIHTH72HCI7AEOKAAA7A4XQN3COQ:rw,z -v /var/lib/jenkins/workspace/ev-team_**_master-D5DTMAWQCFAZ7O7BLEJB3CZGNIHTH72HCI7AEOKAAA7A4XQN3COQ@tmp:/var/lib/jenkins/workspace/ev-team_**_master-D5DTMAWQCFAZ7O7BLEJB3CZGNIHTH72HCI7AEOKAAA7A4XQN3COQ@tmp:rw,z -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** 53c72bed1642cf1703ce0385a7b622121709b9af cat
Note the the -w and -v flags. Jenkins mounts into those directories which is different than the WRKDIR that you specify in your Dockerfile above.
I haven't yet found a great solution to this except installing dependencies globally but that also isn't great. If others have fixed this in other ways I'd love to know.
During docker build Jenkins executes npm install instruction from Dockerfile.
But, as dkez correctly said, Jenkins mounts his workspace to image, so npm test executed in /var/lib/jenkins/workspace/job_name and leads to missing mode_module folder error.
You may specify folder where npm command will be executed, so just point it to your workdir:
pipeline {
agent { dockerfile true }
stages {
stage('Test') {
steps {
sh 'ls -l'
sh 'npm test --prefix app'
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With