2基本组成
下面针对几个核心概念,逐一进行说明
- Pipeline
指令说明:代表整条流水线,包含整条流水线的逻辑。Pipeline{}表示申明式脚本模块。
作用域:应用于全局最外层,表明该脚本为声明式pipeline
是否必须:必须
参数:无
- Agent
指令说明:agent部分指定整个Pipeline或特定阶段将在Jenkins环境中执行的位置,具体取决于该agent 部分的放置位置。该部分必须在pipeline块内的顶层定义 ,但stage级使用是可选的。
作用域:可用在全局与stage内
是否必须:是,
参数:any,none, label, node,docker,dockerfile
为了支持Pipeline可能拥有的各种用例,该agent部分支持几种不同类型的参数。这些参数可以应用于pipeline块的顶层,也可以应用在每个stage指令内。
参数说明:
参数any:在任何可用的agent 上执行Pipeline或stage。例如:agent any
pipeline{
agent any //全局必须带有agent表明此pipeline执行节点
stages{
stage("first stage"){
agent { label 'master' } //具体执行的步骤节点,非必须
steps{
echo "this is first step"
}
}
}
参数None:当在pipeline块的顶层使用none时,将不会为整个Pipeline运行分配全局agent ,每个stage部分将需要包含其自己的agent部分。
参数label:使用提供的label标签,在Jenkins环境中可用的代理上执行Pipeline或stage。例如:agent { label 'my-defined-label' }
参数node:agent { node { label 'labelName' } },等同于 agent { label 'labelName' },但node允许其他选项(如customWorkspace)。
参数docker:定义此参数时,执行Pipeline或stage时会动态供应一个docker节点去接受Docker-based的Pipelines。 docker还可以接受一个args,直接传递给docker run调用。例如:agent { docker 'maven:3-alpine' }或
docker
agent {
docker {
image 'maven:3-alpine'
label 'my-defined-label'
args '-v /tmp:/tmp'
}
}
Dockerfile:使用从Dockerfile源存储库中包含的容器来构建执行Pipeline或stage 。为了使用此选项,Jenkinsfile必须从Multibranch Pipeline或“Pipeline from SCM"加载。
默认是在Dockerfile源库的根目录:agent { dockerfile true }。如果Dockerfile需在另一个目录中建立,请使用以下dir选项:agent { dockerfile { dir 'someSubDir' } }。您可以通过docker build ...使用additionalBuildArgs选项,如agent { dockerfile { additionalBuildArgs '--build-arg foo=bar' } }。
参数示例:
//运行在任意的可用节点上
agent any
//全局不指定运行节点,由各自stage来决定
agent none
//运行在指定标签的机器上,具体标签名称由agent配置决定
agent { label 'master' }
//node参数可以扩展节点信息
agent {
node {
label 'master'
customWorkspace 'xxx'
}
}
//使用指定运行的容器
agent { docker 'python' }
常用设置:
这些是可以应用于两个或多个agent的选项。除非明确定义,否则不需要。
label
一个字符串。标记在哪里运行pipeline或stage
此选项适用于node,docker和dockerfile,并且 node是必需的。
customWorkspace
一个字符串。自定义运行的工作空间内。它可以是相对路径,在这种情况下,自定义工作区将位于节点上的工作空间根目录下,也可以是绝对路径。例如:
agent {
node {
label 'my-defined-label'
customWorkspace '/some/other/path'
}
}
reuseNode
一个布尔值,默认为false。如果为true,则在同一工作空间中。
此选项适用于docker和dockerfile,并且仅在 individual stage中使用agent才有效。
3.stages
指令说明:包含一个或多个stage的序列,Pipeline的大部分工作在此执行。建议stages至少包含至少一个stage指令,用于连接各个交付过程,如构建,测试和部署等。
作用域:全局或者stage阶段内,每个作用域内只能使用一次
是否必须:全局必须
参数:无
pipeline{
agent any
stages{
stage("first stage"){
stages{ //嵌套在stage里
stage("inside"){
steps{
echo "inside"
}
}
}
}
stage("stage2"){
steps{
echo "outside"
}
}
}
}
看下运行结果,发现嵌套的stage也是能够展现在视图里面的:
- stage
指令说明:代表流水线的阶段。每个阶段都必须有名称。
作用域:被stages包裹,作用在自己的stage包裹范围内
是否必须:必须
参数:需要一个string参数,表示此阶段的工作内容
备注:stage内部可以嵌套stages,内部可单独制定运行的agent
5.steps
指令说明:代表阶段中的一个或多个具体步骤(step)的容器。steps部分至少包含一个步骤,本例中,echo就是一个步骤。在一个stage中有且只有一个steps。
作用域:被stage包裹,作用在stage内部
是否必须:必须
参数:无
6.post
指令说明:定义Pipeline或stage运行结束时的操作。post-condition块支持post部件:always,changed,failure,success,unstable,和aborted。这些块允许在Pipeline或stage运行结束时执行步骤,具体取决于Pipeline的状态。
作用域:作用在pipeline结束后者stage结束后
条件:always、changed、failure、success、unstable、aborted
参数说明:
参数always:运行,无论Pipeline运行的完成状态如何。
参数changed:只有当前Pipeline运行的状态与先前完成的Pipeline的状态不同时,才能运行。
参数fixed:上一次完成状态为失败或不稳定(unstable),当前完成状态为成功时执行。
参数regression:上一次完成状态为成功,当前完成状态为失败、不稳定或中止(aborted)时执行。
参数failure:仅当当前Pipeline处于“失败”状态时才运行,通常在Web UI中用红色指示表示。
参数success:仅当当前Pipeline具有“成功”状态时才运行,通常在具有蓝色或绿色指示的Web UI中表示。
参数unstable:只有当前Pipeline具有“不稳定”状态,通常由测试失败,代码违例等引起,才能运行。通常在具有黄色指示的Web UI中表示。
参数aborted:只有当前Pipeline处于“中止”状态时,才会运行,通常是由于Pipeline被手动中止。通常在具有灰色指示的Web UI中表示。
参数cleanup:清理条件块。不论当前完成状态是什么,在其他所有条件块执行完成后都执行。post部分可以同时包含多种条件块。以下是post部分的完整示例。
示例:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'echo Build stage ...'
}
post {
always {
echo "post condition executed: always ..."
}
changed {
echo "post condition executed: changed ..."
}
aborted {
echo "post condition executed: aborted ..."
}
regression {
echo "post condition executed: regression ..."
}
}
}
stage('Test'){
steps {
sh 'echo Test stage ...'
}
post {
aborted {
echo "post condition executed: aborted ..."
}
failure {
echo "post condition executed: failure ..."
}
success {
echo "post condition executed: success ..."
}
}
}
stage('Deploy') {
steps {
sh 'echo Deploy stage ...'
}
}
}
post {
unstable {
echo "post condition executed: unstable ..."
}
unsuccessful {
echo "post condition executed: unsuccessful ..."
}
cleanup {
echo "post condition executed: cleanup ..."
}
}
}