Jenkins - 执行多个步骤(step)

Pipelines 由多个步骤(step)组成,允许你构建、测试和部署应用。 Jenkins Pipeline 允许您使用一种简单的方式组合多个步骤, 以帮助您实现多种类型的自动化构建过程。

可以把“步骤(step)”看作一个执行单一动作的单一的命令。 当一个步骤运行成功时继续运行下一个步骤。 当任何一个步骤执行失败时,Pipeline 的执行结果也为失败。

当所有的步骤都执行完成并且为成功时,Pipeline 的执行结果为成功。

Linux、BSD 和 Mac OS

在 Linux、BSD 和 Mac OS(类 Unix ) 系统中的 shell 命令, 对应于 Pipeline 中的一个 sh 步骤(step)。

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}

Windows

基于 Windows 的系统使用 bat 步骤表示执行批处理命令。

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                bat 'set'
            }
        }
    }
}

超时、重试和更多

Jenkins Pipeline 提供了很多的步骤(step),这些步骤可以相互组合嵌套,方便地解决像重复执行步骤直到成功(重试)和如果一个步骤执行花费的时间太长则退出(超时)等问题。

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flakey-deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}

“Deploy”阶段(stage)重复执行 flakey-deploy.sh 脚本3次,然后等待 health-check.sh 脚本最长执行3分钟。 如果 health-check.sh 脚本在 3 分钟内没有完成,Pipeline 将会标记在“Deploy”阶段失败。

内嵌类型的步骤,例如 timeout 和 retry 可以包含其他的步骤,包括 timeout 和 retry 。

我们也可以组合这些步骤。例如,如果我们想要重试部署任务 5 次,但是总共花费的时间不能超过 3 分钟。

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                timeout(time: 3, unit: 'MINUTES') {
                    retry(5) {
                        sh './flakey-deploy.sh'
                    }
                }
            }
        }
    }
}

完成时动作

当 Pipeline 运行完成时,你可能需要做一些清理工作或者基于 Pipeline 的运行结果执行不同的操作, 这些操作可以放在 post 部分。

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AnyaPapa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值