Issue
Description
I'm trying to create a Jenkinsfile that builds and pushes an image to a registry. However I would like that before pushing it, the pipeline makes sure that all tests passes.
Environment
Jenkinsfile
node {
checkout scm
def customImage = docker.build("my-image:test")
customImage.inside {
dir("app"){
// chown was added to try to solve the problem with the suggested solution
// from the error log,but doesn't work.
sh 'sudo chown -R 126:133 "/.npm"'
sh 'npm run test'
}
}
}
Made sure that the app directory, just to discard that possibility.
Dockerfile
FROM node
WORKDIR /usr/src/app
COPY app/package*.json ./
RUN npm install -D
COPY app .
EXPOSE 3000
CMD ["npm", "start"]
NPM Scripts
"scripts": {
"start": "node server.js",
"test": "NODE_ENV=test npx mocha"
},
Running test on podman
Before anything else tried running the test on podman to quickly check it out
Problem
When I execute the Jenkins pipeline (using a multibranch pipeline) I get the following error from the console output:
As the error suggest added the chown from the error log, so that's why in the Jenkinsfile I included that, although it doesn't recognize sudo:
Also tried to add chown
to Jenkinsfile without sudo, here is the error message:
Solution
It seems that due to the uid mapping done by jenkins pipeline when using inside
, there is no way to make it work.
The docker command that is being run by the plugin can be seen in the console output given in the images of the question. So thought of an hybrid solution, using an
sh
step in between to run the test with docker cli. Here is the Jenkinsfile:
node {
checkout scm
def app = docker.build("itasahobby/app")
stage('Test'){
sh 'docker run itasahobby/app npm run test'
}
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub'){
app.push()
}
}
Answered By - itasahobby