jenkins运行日志时间与linux,Jenkins 用户文档(运行多个步骤)

运行多个步骤

管道由多个步骤组成,允许你构建、测试和部署应用程序,Jenkins管道允许你以简单的方式组成多个步骤,可以帮助你为任何类型的自动化过程建模。

将“步骤”想象成执行单个操作的单个命令,当一个步骤成功时,它将进入下一步,当一个步骤无法正确执行时,管道将失败。

当管道中的所有步骤都已成功完成时,将认为管道已成功执行。

Linux、BSD和Mac OS

在Linux、BSD和Mac OS(类Unix)系统上,sh步骤用于在管道中执行shell命令。

Jenkinsfile (Declarative Pipeline)

pipeline {

agent any

stages {

stage('Build') {

steps {

sh 'echo "Hello World"'

sh '''

echo "Multiline shell steps works too"

ls -lah

'''

}

}

}

}

脚本管道(高级):

Jenkinsfile (Scripted Pipeline)

node {

stage('Build') {

sh 'echo "Hello World"'

sh '''

echo "Multiline shell steps works too"

ls -lah

'''

}

}

Windows

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

Jenkinsfile (Declarative Pipeline)

pipeline {

agent any

stages {

stage('Build') {

steps {

bat 'set'

}

}

}

}

脚本管道(高级):

Jenkinsfile (Scripted Pipeline)

node {

stage('Build') {

bat 'set'

}

}

超时、重试等等

有一些强大的步骤可以“包装”其他步骤,这些步骤可以轻松解决问题,例如重试(retry)步骤直到成功或退出(如果步骤花费太长时间(timeout))。

Jenkinsfile (Declarative Pipeline)

pipeline {

agent any

stages {

stage('Deploy') {

steps {

retry(3) {

sh './flakey-deploy.sh'

}

timeout(time: 3, unit: 'MINUTES') {

sh './health-check.sh'

}

}

}

}

}

脚本管道(高级):

Jenkinsfile (Scripted Pipeline)

node {

stage('Deploy') {

retry(3) {

sh './flakey-deploy.sh'

}

timeout(time: 3, unit: 'MINUTES') {

sh './health-check.sh'

}

}

}

“Deploy”阶段重试flakey-deploy.sh脚本3次,然后等待最多3分钟对于执行health-check.sh脚本,如果运行状况检查脚本在3分钟内未完成,则管道将在“Deploy”阶段标记为已失败。

“包装”步骤(如timeout和retry)可能包含其他步骤,包括timeout或retry。

我们可以将这些步骤组合在一起,例如,如果我们想要重试我们的部署5次,但在阶段失败之前永远不想花费超过3分钟:

Jenkinsfile (Declarative Pipeline)

pipeline {

agent any

stages {

stage('Deploy') {

steps {

timeout(time: 3, unit: 'MINUTES') {

retry(5) {

sh './flakey-deploy.sh'

}

}

}

}

}

}

脚本管道(高级):

Jenkinsfile (Scripted Pipeline)

node {

stage('Deploy') {

timeout(time: 3, unit: 'MINUTES') {

retry(5) {

sh './flakey-deploy.sh'

}

}

}

}

完成

管道执行完成后,你可能需要运行清理步骤或根据管道的结果执行某些操作,这些操作可以在post部分中执行。

Jenkinsfile (Declarative Pipeline)

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'

}

}

}

脚本管道(高级):

Jenkinsfile (Scripted Pipeline)

node {

try {

stage('Test') {

sh 'echo "Fail!"; exit 1'

}

echo 'This will run only if successful'

} catch (e) {

echo 'This will run only if failed'

// Since we're catching the exception in order to report on it,

// we need to re-throw it, to ensure that the build is marked as failed

throw e

} finally {

def currentResult = currentBuild.result ?: 'SUCCESS'

if (currentResult == 'UNSTABLE') {

echo 'This will run only if the run was marked as unstable'

}

def previousResult = currentBuild.previousBuild?.result

if (previousResult != null && previousResult != currentResult) {

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'

}

echo 'This will always run'

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值