我有一个Jenkinsfile,它带有多个节点参数,并旋转了许多赛普拉斯节点(对于这个问题并不重要)来并行运行测试。
这是Jenkinsfile
#!groovy
String hipChatRoom = "${HC_ROOM}" //'Ecom WSS'
def projectPath = "${projectPath}"
def numberOfNodes = "${numberOfNodes}".toInteger()
def failure = false
def parallelStageMap = [:]
def generateStage() {
return {
node("Cypress") {
try {
build(job: 'Cypress-Kickoff', wait: true, parameters: [[$class: 'StringParameterValue', name: 'BRANCH_NAME', value: BRANCH_NAME], [$class: 'StringParameterValue', name: 'recordKey', value: recordKey], [$class: 'StringParameterValue', name: 'projectPath', value: projectPath], [$class: 'StringParameterValue', name: 'CI_BUILD_ID', value: BUILD_ID]])
} catch(Exception e) {
println e
failure = true
}
}
}
}
stage("Cypress Run") {
node("Cypress") {
currentBuild.setDisplayName("${projectPath}")
deleteDir()
git url: 'http://github.com/REPO/REPO.git', branch: BRANCH_NAME
stepsForParallel = [:]
for (int i = 0; i < numberOfNodes; i++) {
def s = "Cypress Runner ${i}"
stepsForParallel[s] = generateStage()
}
parallel stepsForParallel
if (failure) {
sendToRoom(hipchatRoom, "${projectPath} failed", "RED")
currentBuild.result = "FAILURE"
}
}
}
当我运行上面的Jenkinsfile时,我得到了
[Cypress Runner 0] Scheduling project: Cypress-Kickoff
[Pipeline] [Cypress Runner 2] build (Building Cypress-Kickoff)
[Cypress Runner 2] Scheduling project: Cypress-Kickoff
[Pipeline] [Cypress Runner 1] build (Building Cypress-Kickoff)
[Cypress Runner 1] Scheduling project: Cypress-Kickoff
[Pipeline] [Cypress Runner 3] {
[Pipeline] [Cypress Runner 4] {
[Pipeline] [Cypress Runner 3] build (Building Cypress-Kickoff)
[Cypress Runner 3] Scheduling project: Cypress-Kickoff
[Pipeline] [Cypress Runner 4] build (Building Cypress-Kickoff)
[Cypress Runner 4] Scheduling project: Cypress-Kickoff
[Cypress Runner 0] Starting building: Cypress-Kickoff #14
[Cypress Runner 2] Starting building: Cypress-Kickoff #14
[Cypress Runner 1] Starting building: Cypress-Kickoff #14
[Cypress Runner 3] Starting building: Cypress-Kickoff #14
[Cypress Runner 4] Starting building: Cypress-Kickoff #14
[Pipeline] [Cypress Runner 0] echo
所需的结果将是类似的
[Cypress Runner 0] Starting building: Cypress-Kickoff #14
[Cypress Runner 2] Starting building: Cypress-Kickoff #15
[Cypress Runner 1] Starting building: Cypress-Kickoff #16
[Cypress Runner 3] Starting building: Cypress-Kickoff #17
[Cypress Runner 4] Starting building: Cypress-Kickoff #18
赛普拉斯启动Jenkins文件非常简单,我的猜测与该问题无关
stage('Run Cypress') {
node('Cypress') {
def unique_id = BUILD_TAG.toLowerCase()
deleteDir()
git url: 'http://github.com/REPO/REPO.git', branch: BRANCH_NAME
sh "docker build --rm --no-cache -t cypress_with_tests_${unique_id} cypress-functional/"
def exitCode = sh script: "docker run --env JENKINS_URL=${JENKINS_URL} --env BUILD_ID=${BUILD_ID} --env BUILD_URL=${BUILD_URL} --env BUILD_NUMBER=${BUILD_NUMBER} cypress_with_tests /bin/bash -c \"cypress run -P ${projectPath} --parallel --record --key ${recordKey} --ci-build-id ${CI_BUILD_ID}\"", returnStatus: true
sh "docker rmi -f cypress_with_tests_${unique_id}"
if (exitCode != 0) {
currentBuild.result = 'FAILURE'
}
}
}
如何获取多个实例而不是一个实例?