gpt4 book ai didi

docker - 如何在不同的代理上运行 Jenkins 并行 cypress?

转载 作者:行者123 更新时间:2023-12-04 13:55:03 27 4
gpt4 key购买 nike

我在 Jenkins 的同一个奴隶上运行并行柏树,它正在工作,
我想更改并行阶段,以便每个阶段都在不同的从站上运行,我该怎么做?
例如:
在 slave-1 上运行“cypress tester A”。
在 slave-2 上运行“cypress tester B”。
在 slave-3 上运行“cypress tester C”。
这是我当前的 Jenkinsfile:

pipeline {
options {
timeout(time: 15, unit: 'MINUTES')
}
agent {
docker {
image 'cypress/base:12.18.2'
label 'slave-1'
}
}
parameters {
string(defaultValue: 'master', description: 'Branch name', name: 'branchName')
}

stages {
stage('build') {
steps {
echo 'Running build...'
sh 'npm ci'
sh 'npm run cy:verify'
}
}

stage('cypress parallel tests') {
environment {
CYPRESS_RECORD_KEY = 'MY_CYPRESS_RECORD_KEY'
CYPRESS_trashAssetsBeforeRuns = 'false'
}
parallel {
stage('cypress tester A') {
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
stage('cypress tester B') {
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
stage('cypress tester C') {
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
}

}
}

post {
always {
cleanWs(deleteDirs: true)
echo 'Tests are finished'
}
}
} cypress:run命令是:
cypress run --record --parallel --config videoUploadOnPasses=false --ci-build-id $BUILD_TAG

最佳答案

我能够通过明确定义 agent 来实现这一点。在每个并行阶段:

parallel {
stage('cypress tester A') {
agent {
node: {
label "slave-1"
}
}
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
stage('cypress tester B') {
agent {
node: {
label "slave-2"
}
}
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
stage('cypress tester C') {
agent {
node: {
label "slave-3"
}
}
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
}
但是,我发现的一个缺点是,现在您在每个单独的节点/虚拟机中运行 cypress,cypress 需要知道在哪里可以找到正在运行的应用程序实例。 Cypress 在 baseUrl 查看 cypress.json查看在哪里可以找到您的应用程序。通常使用本地主机地址进行开发,这意味着在 slave-1 上运行的 cypress 将寻找在 slave-1 的本地主机上运行的应用程序 - 但没有一个,所以它会失败。
为简单起见,我只是做了一个 npm installnpm start & npx wait-on http://localhost:3000在每个节点中:
stage('cypress tester A') {
agent {
node: {
label "slave-1"
}
}
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm install --silent"
sh "npm start & npx wait-on http://localhost:3000"
sh "npm run cypress:run"
}
}
这显然不是很有效,因为您必须在每个节点上安装和运行应用程序。但是,您可能会在专用节点(例如 slave-0)上设置前一阶段来安装和服务您的项目,并使用它。在您的 Jenkinsfile 中,您需要知道 slave-0 的 IP,或者您可以 get it dynamically within your Jenkinsfile .然后,不是在 slave-1、2 和 3 上安装和运行您的项目,而是仅在 slave-0 上安装和运行它,并使用 CYPRESS_BASE_URL env 变量告诉 cypress 在哪里可以找到应用程序的运行实例。如果 slave-0 的 IP 是 2222.2222.2222.2222,你可以尝试这样的事情:
pipeline {
stage ('Serve your project'){
agent {
label 'slave-0'
}
steps {
sh 'npm install --silent'
sh 'npm start & npx wait-on http://localhost:3000'
}
}
stage('Cypress'){
environment {
CYPRESS_BASE_URL=2222.2222.2222.2222:3000
// other env vars
}
parallel {
stage {
agent {
label 'slave-1'
}
steps {
echo "Running build ${env.BUILD_ID}"
sh "npm run cypress:run"
}
}
// more parallel stages
}
}
}
你可以做很多变化,但希望这能让你开始。

关于docker - 如何在不同的代理上运行 Jenkins 并行 cypress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64013383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com