Jenkins Pipeline:AWS Spot Agent终止时如何重试对应阶段?
Jenkins Pipeline:针对AWS Spot Agent终止的阶段重试方案
直接给你可用的语法和完整示例,解决之前的编译异常问题:
核心要点
retry步骤的condition参数需要传入AgentErrorCondition的完整类路径字符串,不能直接写类名或实例化对象,这是之前报错的关键。
完整Pipeline示例
pipeline { agent none stages { stage('并行测试执行') { parallel { stage('测试套件A') { steps { // 最多重试3次,仅当Agent终止类错误触发重试 retry(maxAttempts: 3, condition: 'hudson.plugins.pipeline.agent.AgentErrorCondition') { // 指定AWS Spot Agent标签 node('aws-spot-agent') { script { // 替换为你的实际测试命令 sh 'npm run test:suite-a' } } } } } stage('测试套件B') { steps { retry(maxAttempts: 3, condition: 'hudson.plugins.pipeline.agent.AgentErrorCondition') { node('aws-spot-agent') { script { sh 'npm run test:suite-b' } } } } } } } } }
关键细节说明
maxAttempts:设置允许的最大重试次数,按需调整condition:必须写全类路径hudson.plugins.pipeline.agent.AgentErrorCondition,Jenkins会自动识别该内置错误条件,仅当Agent被终止这类错误发生时才触发重试- 每个并行阶段独立配置重试,某一个套件的Agent终止不会影响其他套件的执行
- 每次重试都会重新分配可用的AWS Spot节点,避免复用已终止的Agent
额外注意事项
- 确保你的Jenkins安装了Pipeline Basic Steps插件且版本在2.20以上,才支持
retry的condition参数 - 测试逻辑要保证幂等性,比如避免重试时重复生成测试数据、重复执行写操作等
- 如果需要自定义更复杂的重试条件,可以在
script块内结合catchError捕获特定异常,再配合retry使用
内容的提问来源于stack exchange,提问作者Boris Katz
相关产品推荐
相关产品推荐

