我有一个并行步骤的Jenkins管道。
步骤A是构建一个spring-boot应用程序,而步骤B是启动另一个spring-boot应用程序(mvn spring-boot:run),这意味着在测试和数据库之间架起一座桥梁。
我的目标是结束步骤B (spring-boot:stop ?)当步骤A完成时(成功或失败)。
我尽量避免使用超时,因为它不是很优化。
你有什么解决方案吗?
非常感谢。
我曾尝试启动spring-boot:在我的测试通过后停止,但没有效果。设置一个布尔变量来停止while循环也是一样的。
parallel(
a: {
Sonar: {
withSonarQubeEnv {
withMaven(maven: 'Apache Maven 3.3.9') {
sh '''
echo "lauching sonar check"
cd git-42c
mvn -Dmaven.test.failure.ignore verify sonar:sonar
cd ..
'''
}
}
}
},
b: {
// Run the maven build
withMaven(maven: 'Apache Maven 3.3.9') {
dir('git-proxy') {
echo "launching mvn spring-boot:run"
sh "mvn spring-boot:run -Dpmd.skip=true -Dcpd.skip=true -Dfindbugs.skip=true"
}
}
}
)
}我希望步骤B在步骤A完成后停止(总是),但我的构建会无限期地挂起,因为步骤B正在运行应用程序。
发布于 2019-07-09 14:45:56
我能想到的一个优雅的解决方案是sidecar容器,但这需要docker和脚本化的管道。
node {
checkout scm
docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
docker.image('mysql:5').inside("--link ${c.id}:db") {
/* Wait until mysql service is up */
sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
}
docker.image('centos:7').inside("--link ${c.id}:db") {
/*
* Run some tests which require MySQL, and assume that it is
* available on the host name `db`
*/
sh 'make check'
}
}
}当然,有一个选项可以在一个标志上同步,比如:
stop = false
parallel 'long': {
sleep 20
println "finished long process"
stop = true
}, 'short': {
while ( !stop ) {
println "work"
sleep 1
}
println "stopped by other branch"
}但这对你不起作用,因为你在任何地方都没有循环。
并行上的failFast也不会。
看起来,即使你从Jenkins REST API中取消了阶段,构建仍然会失败。
那么你想要的结果是什么呢?如果你不知道构建失败了什么,你必须引入一些机制来同步状态。
发布于 2019-07-10 16:37:55
好吧,我找到了一个解决方案。
提醒:我的目标是在构建的同时启动和停止一个spring boot应用程序。
因为我找不到一种方法来远程删除与她的步骤平行的步骤,所以我不得不使用一种不太优雅的方法:超时。
步骤超时不起作用,因为只有当命令没有启动时才会超时,但是spring-boot:run会启动。不起作用:timeout(time: 1, unit: 'MINUTES') { [...] }
所以超时必须在命令本身。这是它最初看起来的样子:
sh "timeout -s KILL 1m mvn spring-boot:run -Dpmd.skip=true -Dcpd.skip=true -Dfindbugs.skip=true
所以在1分钟后,我的跑步就结束了。这意味着一个新的问题,kill会使并行步骤失败,所以即使作业在构建成功后完成,它仍然被认为是失败的,因为作业的一个分支已经“失败”了。
现在,为了避免失败,一个解决方案是认为spring-boot步骤总是成功的。这是通过使用command || true完成的。
如下所示:
sh "timeout -s KILL 1m mvn spring-boot:run -Dpmd.skip=true -Dcpd.skip=true -Dfindbugs.skip=true || true"
现在,我的并行步骤在绿色成功中完成。
根据我在问题中的示例,这是相同的示例:
stage('Scan Sonar') {
parallel(
a: {
Sonar: {
withSonarQubeEnv {
withMaven(maven: 'Apache Maven 3.3.9') {
sh '''
echo "lauching sonar check"
cd git-42c
mvn -Dmaven.test.failure.ignore verify sonar:sonar
cd ..
'''
}
}
}
},
b: {
// Run the maven build
withMaven(maven: 'Apache Maven 3.3.9') {
dir('git-proxy') {
echo "launching mvn spring-boot:run"
sh "timeout -s KILL 1m mvn spring-boot:run -Dpmd.skip=true -Dcpd.skip=true -Dfindbugs.skip=true || true"
}
}
}
)
}https://stackoverflow.com/questions/56938695
复制相似问题