【发布时间】:2021-09-18 05:33:14
【问题描述】:
我在 Jenkins 中跨不同代理/节点并行运行某些阶段时遇到问题。我正在使用以下代码从可用代理列表中动态创建阶段:
// Create empty collection to store parallel stages:
def parallelStages = [:]
// Define list of agents to be used for cypress parallel stages:
agents = [
"agent1",
"agent2",
...
]
// Add as many parallel stages as defined in agents list:
agents.eachWithIndex { label, index ->
parallelStages["Parallel Stage ${index + 1}"] = {
stage("Parallel Stage ${index + 1}") {
node(label) {
sh 'npm install --silent'
sh 'npm start & npx wait-on http://localhost:3000'
sh "npm run another_command"
}
}
}
}
然后我在并行块中使用这些阶段:
pipeline {
agent {
node {
label 'agent1'
}
}
stages {
stage('first-non-parallel-stage'){
steps {
sh 'npm install --silent'
sh 'npm run lint'
sh 'npm run build'
sh "npm run storybook:build"
sh 'npm run test -- --watchAll=false'
}
}
stage ('Parallel stuff'){
steps {
script {
parallel parallelStages
}
}
}
}
}
这是有效的。但是,对于 agent1 上的阶段,我在 Jenkins 日志中收到以下错误:
+ npx wait-on http://localhost:3000
+ npm start
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/vine/workspace/tend-multibranch_jenkins-testing@3/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/vine/workspace/tend-multibranch_jenkins-testing@3/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
一些可能相关的细节,但我不确定:
-
在运行并行阶段之前,您可以看到我正在运行
first-non-parallel-stage,在有问题的代理上 - 这可能相关吗?我在first-non-parallel-stage中没有问题,只是在那个代理的并行块中。first-non-parallel-stage完成后,我不应该能够在并行块中重用 agent1 吗? -
当我使用重复的详细并行块时,我没有遇到这个问题,也就是说,我没有创建一个用
stage块填充集合的函数,而是手动将它们写出来,如下所示:
parallel {
stage { ...stage_code }
stage { ...stage_code }
stage { ...stage_code }
}
但显然这很冗长,不利于轻松添加更多节点。
为什么会发生这种情况?
【问题讨论】:
-
当您在调用
npm install之前已经拥有package-lock.json时,往往会发生此错误,agent1就是这种情况。尝试先删除并行代码块中或非并行阶段之后的package-lock.json(如果存在)。 -
在每个阶段开始时做一个
deleteDir()会不会有点矫枉过正?或者,如果我运行`sh "rm package-lock.json"`,代理是否已经在文件树中的正确位置找到它? -
这两个选项都应该做的工作
-
不幸的是,没有人提供帮助。我试过删除 package-lock.json,在每个阶段之前或之后都试过
deleteDir(),我试过cleanWs()...似乎没有任何效果。现在更糟糕的是,问题发生在 更多 个节点上,而不仅仅是一个节点。我不确定我做错了什么
标签: jenkins npm parallel-processing jenkins-groovy jenkins-declarative-pipeline