Jenkins - Pipeline Retry

Jenkins - Pipeline Retry

引言

日常运行自动化测试用例,通常是晚上定时启动 pipeline job,一个 pipeline 脚本可能会涉及到多个 Job, 最后 post 发邮件汇总测试 report。有时会遇到 Jenkins 环境问题导致某 Job 失败,第二天需要手动触发 job 重跑,这样影响工作效率。那我们是否可以在 pipeline 脚本里自动 retry 呢?

retry

在 Jenkins Pipeline 中,可以使用 retry 指令来重试某个步骤。以下是一个基本的示例:

groovy 复制代码
pipeline {
    agent any

    stages {
        stage('Test') {
            steps {
                retry(3) {
                    // 这里放置你想要重试的步骤
                }
            }
        }
    }
}

在这个示例中,如果步骤失败,Jenkins 会尝试重新运行它,最多重试 3 次。你可以根据需要更改重试次数。

retry 实例

可以运行下面这个 pipeline 脚本实例,其中 Build stage 中有加 retry 3 次。

groovy 复制代码
pipeline {
    agent { label "xxx" }

    stages {
        stage('Preparation') {
            steps {
                echo 'Preparing...'
            }
        }
        stage('Build') {
            steps {
                retry(3) {
                    script {
                        echo "Attempting to build..."
                        // 可能失败的命令
                        sh 'make build'
                    }
                }
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
    }
}

jenkins pipeline 执行 log,Build stage 执行了 3 次均失败,所以 Test stage 将不再执行。

bash 复制代码
20:08:23  [Pipeline] {
20:08:23  [Pipeline] stage
20:08:23  [Pipeline] { (Preparation)
20:08:23  [Pipeline] echo
20:08:23  Preparing...
20:08:23  [Pipeline] }
20:08:23  [Pipeline] // stage
20:08:23  [Pipeline] stage
20:08:23  [Pipeline] { (Build)
20:08:23  [Pipeline] retry
20:08:23  [Pipeline] {
20:08:23  [Pipeline] script
20:08:23  [Pipeline] {
20:08:23  [Pipeline] echo
20:08:23  Attempting to build...
20:08:23  [Pipeline] sh
20:08:24  + make build
20:08:24  make: *** No rule to make target 'build'.  Stop.
20:08:24  [Pipeline] }
20:08:24  [Pipeline] // script
20:08:24  [Pipeline] }
20:08:24  ERROR: script returned exit code 2
20:08:24  Retrying
20:08:24  [Pipeline] {
20:08:24  [Pipeline] script
20:08:24  [Pipeline] {
20:08:24  [Pipeline] echo
20:08:24  Attempting to build...
20:08:24  [Pipeline] sh
20:08:24  + make build
20:08:24  make: *** No rule to make target 'build'.  Stop.
20:08:24  [Pipeline] }
20:08:24  [Pipeline] // script
20:08:24  [Pipeline] }
20:08:24  ERROR: script returned exit code 2
20:08:24  Retrying
20:08:24  [Pipeline] {
20:08:25  [Pipeline] script
20:08:25  [Pipeline] {
20:08:25  [Pipeline] echo
20:08:25  Attempting to build...
20:08:25  [Pipeline] sh
20:08:25  + make build
20:08:25  make: *** No rule to make target 'build'.  Stop.
20:08:25  [Pipeline] }
20:08:25  [Pipeline] // script
20:08:25  [Pipeline] }
20:08:25  [Pipeline] // retry
20:08:25  [Pipeline] }
20:08:25  [Pipeline] // stage
20:08:25  [Pipeline] stage
20:08:25  [Pipeline] { (Test)
20:08:25  Stage "Test" skipped due to earlier failure(s)
20:08:25  [Pipeline] }
20:08:25  [Pipeline] // stage
20:08:25  [Pipeline] }
20:08:26  [Pipeline] // node
20:08:26  [Pipeline] End of Pipeline
20:08:26  ERROR: script returned exit code 2
20:08:26  Finished: FAILURE
相关推荐
运维&陈同学14 小时前
【Elasticsearch05】企业级日志分析系统ELK之集群工作原理
运维·开发语言·后端·python·elasticsearch·自动化·jenkins·哈希算法
csdn_金手指1 天前
Jenkins持续交付web应用,通过docker制作相关的镜像进行发布部署
运维·jenkins
龙少95431 天前
【SpringBoot中怎么使用ElasticSearch】
spring boot·elasticsearch·jenkins
紫菜(Nori)2 天前
Jenkins 中 写 shell 命令执行失败,检测失败问题
jenkins
jwensh2 天前
【Jenkins】pipeline 的基础语法以及快速构建一个 jenkinsfile
pipeline·jenkins
测试工程师成长之路2 天前
解锁 Jenkins+Ant+Jmeter 自动化框架搭建新思路
jmeter·自动化·jenkins
听说唐僧不吃肉2 天前
CICD篇之通过Jenkins中书写pipeline构建编译打包发布流程
运维·jenkins
iRayCheung2 天前
python elasticsearch 8.x通过代理发起请求方法
python·elasticsearch·jenkins
理不为2 天前
常用es命令
大数据·elasticsearch·jenkins
vvw&3 天前
如何在 Ubuntu 22.04 服务器上安装 Jenkins
linux·运维·服务器·ubuntu·ci/cd·自动化·jenkins