一. 简介
Pipeline支持两种语法: Declarative Pipeline(声明式pipeline,在pipeline2.5中引入,结构化方式)和Scripted Pipeline(脚本式pipeline),两者都支持建立连续输送的Pipeline。
相关资料:
https://stackoverflow.com/questions/43484979/jenkins-scripted-pipeline-or-declarative-pipeline
http://jenkins-ci.361315.n4.nabble.com/Declarative-pipelines-vs-scripted-td4891792.html
声明式Pipeline是后续Open Blue Ocean所支持类型,建议使用声明式Pipeline的方式进行编写,从jenkins社区动向看,很明显这种语法结构会是未来的趋势。
- 声明式pipeline可以内嵌脚本式pipeline
- 声明式pipeline必须包含在固定格式的pipeline{}内
- 块(Block{}): 只能包含章节Sections,指令Directives,步骤Steps或者赋值语句
- 章节(Sections): 通常包括一个或多个指令或步骤,如agent,post,stages,steps
- 指令(Directives): environment,options,parameters,triggers,stage,tools,when
- 步骤(steps): 执行脚本式pipeline,如script{}
Sections(章节)
agent
Pipeline或特定阶段将在Jenkins环境中执行的位置,具体取决于该agent 部分的放置位置;必须在pipeline顶层定义。
参数:
- any: 在任何可用的agent 上执行Pipeline或stage。例如:agent any。
- none: 当在pipeline块的顶层使用none时,将不会为整个Pipeline运行分配全局agent ,每个stage部分将需要包含其自己的agent部分。
- label: 使用有label标签的agent,例如:agent { label 'my-defined-label' }。
- node: agent { node { label 'labelName' } },等同于 agent { label 'labelName' },但node允许其他选项(如customWorkspace)。
- docker: 动态供应一个docker节点去执行pipeline或stage,docker还可以接受一个args,直接传递给docker run调用。
agent {
docker {
image 'maven:3-alpine'
label 'my-defined-label'
args '-v /tmp:/tmp'
}
}
- dockerfile: Dockerfile源存储库中包含的容器来构建执行Pipeline或stage。使用此参数,jenkinsfile必须从代码中加载使用“pipeline from SCM”或者“Multibranch Pipeline”加载
默认是Dockerfile在根目录: agent { dockerfile true }
如果Dockerfile在另一个目录,使用dir参数: agent { dockerfile { dir 'someSubDir' } }
可以使用docker build添加参数: agent { dockerfile { additionalBuildArgs '--build-arg foo=bar' } }
pipeline {
agent { dockerfile true }
stages {
stage('Test') {
steps {
sh 'node --version'
sh 'svn --version'
}
}
}
}