jenkins学习笔记-官网翻译

来源:互联网 发布:网络语言暴力工作视频 编辑:程序博客网 时间:2024/05/16 00:40


目录

运行多个步骤...1

Linxu系统,bsd系统,Mac系统...1

Windows系统...2

超时,重试等等...2

本章小结:...4

 

 

运行多个步骤

通道是由多个步骤组成的,通过这些步骤你可以构建,测试,部署应用。Jenkins通道允许用简单的方式来组合这多个步骤,以便帮助你模块化任何类型的自动化构建过程。

“步骤”可以类比为一个执行单个命令的单一动作。当一个步骤成功执行之后,就会跳到下一个步骤。当一个步骤执行失败,通道就失败了。

当通道里的所有步骤都执行成功,那么就可以认为是通道执行成功了。

 

Linxu系统,bsd系统,Mac系统

linuxbsdmacunix系列)的操作系统上,sh步骤被用于在通道中执行shell命令。

 

Jenkinsfile(Declarative Pipeline)

pipeline {

   agent any

   stages {

       stage('Build') {

           steps {

               sh'echo "Hello World"'

               sh'''

                   echo "Multiline shellsteps works too"

                   ls -lah

               '''

           }

       }

   }

}

 

Toggle Scripted Pipeline (高级)

Jenkinsfile(Scripted Pipeline)

node {

   stage('Build') {

       sh'echo "Hello World"'

       sh'''

           echo "Multiline shell stepsworks too"

           ls -lah

       '''

   }

}

 

Windows系统

基于windows的操作系统应该使用bat步骤来用于执行批量命令

 

Jenkinsfile(Declarative Pipeline)

pipeline {

   agent any

   stages {

       stage('Build') {

           steps {

               bat'set'

           }

       }

   }

}

 

Toggle Scripted Pipeline (高级)

 

Jenkinsfile(Scripted Pipeline)

node {

   stage('Build') {

       bat'set'

   }

}

 

超时,重试等等

有一些有效的步骤来包裹其他步骤。这些有效的步骤能轻易解决诸如成功前重试和超时退出等问题

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'

               }

           }

       }

   }

}

ToggleScripted Pipeline (Advanced)

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脚本三遍,然后给/health-check.sh脚本执行三分钟。如果health-check.sh三分钟内没有执行完毕,通道就会标记“Deploy”阶段执行失败。

Wrapper(嵌套)步骤比如超时和重试,可能包含其他的步骤,包括超时和重试步骤。

你可以将这些步骤组合起来。比如,如果我们想重试我们的部署五次,但该阶段失败之前的总花费时长又不想超过三分钟。

Jenkinsfile(Declarative Pipeline)

pipeline {

   agent any

   stages {

       stage('Deploy') {

           steps {

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

                   retry(5) {

                       sh'./flakey-deploy.sh'

                   }

               }

           }

       }

   }

}

Toggle Scripted Pipeline (高级)

Jenkinsfile(Scripted Pipeline)

node {

   stage('Deploy') {

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

           retry(5) {

               sh'./flakey-deploy.sh'

           }

       }

   }

}

本章小结:

当通道已经结束执行,你可能需要运行clean-up(清理)步骤或者执行一些基于通道输出的动作。这些动作可以在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 haschanged'

           echo'For example, if the Pipeline was previously failing butis now successful'

       }

   }

}

 

 

Toggle Scripted Pipeline (高级)

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'recatching the exception in order to report on it,

       // we need tore-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 haschanged'

           echo'For example, if the Pipeline was previously failing butis now successful'

       }

 

       echo'This will always run'

   }

}