如何在 Jenkins 中 运行 节点测试 docker
How to run node test inside docker within Jenkins
描述
我正在尝试创建一个 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": {
"start": "node server.js",
"test": "NODE_ENV=test npx mocha"
},
运行 在 podman 上测试
在尝试其他任何事情之前 运行 在 podman 上进行测试以快速检查它
问题
当我执行 Jenkins 管道(使用多分支管道)时,我从控制台输出中收到以下错误:
由于错误建议从错误日志中添加了 chown,所以这就是我在 Jenkinsfile 中包含它的原因,尽管它不识别 sudo:
还尝试在不使用 sudo 的情况下将 chown
添加到 Jenkinsfile,这是错误消息:
你能试试这个 docker 文件吗?
FROM node
WORKDIR /usr/src/app
COPY app/package*.json ./
RUN npm install -D && chown -R 1000:1000 ~/.npm/
COPY app .
EXPOSE 3000
CMD ["npm", "start"]
注意:我们需要在创建图像时更正权限。
似乎由于使用inside
时jenkins管道完成的uid映射,无法使其工作。
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()
}
}
描述
我正在尝试创建一个 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": {
"start": "node server.js",
"test": "NODE_ENV=test npx mocha"
},
运行 在 podman 上测试
在尝试其他任何事情之前 运行 在 podman 上进行测试以快速检查它
问题
当我执行 Jenkins 管道(使用多分支管道)时,我从控制台输出中收到以下错误:
由于错误建议从错误日志中添加了 chown,所以这就是我在 Jenkinsfile 中包含它的原因,尽管它不识别 sudo:
还尝试在不使用 sudo 的情况下将 chown
添加到 Jenkinsfile,这是错误消息:
你能试试这个 docker 文件吗?
FROM node
WORKDIR /usr/src/app
COPY app/package*.json ./
RUN npm install -D && chown -R 1000:1000 ~/.npm/
COPY app .
EXPOSE 3000
CMD ["npm", "start"]
注意:我们需要在创建图像时更正权限。
似乎由于使用inside
时jenkins管道完成的uid映射,无法使其工作。
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()
}
}