gpt4 book ai didi

jenkins - 如何在管道脚本中使 Jenkins 节点离线

转载 作者:行者123 更新时间:2023-12-02 20:22:01 28 4
gpt4 key购买 nike

我们希望在开始耗时的集成测试之前验证 Jenkins 节点是否正常。如果节点健康,我们希望将其脱机,以便有人可以连接并修复它。

最佳答案

经过相当多的反复试验,我最终得到了这个解决方案:

def ExecuteTestsOnSelectedNode() {
stage("integrationtests") {
testSystemVerified = false
while(!testSystemVerified) { // Retry if test system verification failed - hopefully another node can execute this test...
node("nodelabel") {
// ... code for unstashing test assemblies omitted
try {
testSystemVerified = checkTestSystem(info, testSetup, testAssemblies);
}
catch (Exception e) {
echo "!!!!!!!!!!!!!!!!!! Test system verification failed !!!!!!!!!!!!!!!!!!!!!!!!\n" + e
throw e
}
if (testSystemVerified) {
// Then it is safe to execute the actual tests
}
}
}
}
}

def checkTestSystem(info, testSetup, testAssemblies) {
try {
// Execute some command that (quickly) verifies the health of the node
bat "$info.test_runner $test_args"
return true // If we get here the test system is OK
}
catch (hudson.AbortException e) {
message = "!!!!!!!!!!!!!!!!!! Test system verification aborted !!!!!!!!!!!!!!!!!!!!!!!!\n" + e
echo message
throw e
}
catch (Exception e) {
message = "!!!!!!!!!!!!!!!!!! Test system verification failed !!!!!!!!!!!!!!!!!!!!!!!!\n" + e
echo message
markNodeOffline(message)
return false
}
}

@NonCPS
def markNodeOffline(message) {
node = getCurrentNode(env.NODE_NAME)
computer = node.toComputer()
computer.setTemporarilyOffline(true)
computer.doChangeOfflineCause(message)
computer = null
node = null
}

@NonCPS
def getCurrentNode(nodeName) {
for (node in Jenkins.instance.nodes) {
if (node.getNodeName() == nodeName) {
echo "Found node for $nodeName"
return node
}
}
throw new Exception("No node for $nodeName")
}

关于jenkins - 如何在管道脚本中使 Jenkins 节点离线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152785/

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