一. Pipline 介绍
一系列Jenkins插件将整个持续集成用解释性代码Jenkinsfile来描述
Jenkinsfile使用方法:
Jenkins任务页面输入
源码工程中编辑
实战:
登陆Jenkins,创建流水线任务,在流水线定义模块:可以直接写pipline脚本 或者从git拉取pipline脚本。
二. Jenkins file语法
1. jenkinsfile语法:
1.1 Declarative pipline - 结构化方法
必须包含在一个pipeline块内,具体来说是:pipeline{ }
基本的部分是“step”,steps告诉Jenkins要做什么
语句分类具体包含Sections,Directives,Steps,赋值等几大类
1.1.1 agent语法说明
agent:定义pipline执行节点
参数:
必须出现的指令
any:可以在任意agent上执行pipeline
none:pipeline将不分配全局agent,每个stage分配自己的agent
label:指定运行节点的label
node:自定义运行节点配置,
指定 label
指定 customWorkspace
docker:控制目标节点上的docker运行相关内容
例子:
pipleline{
agent{
node {
label "myslave"
customWorkspace "myWorkspace"
}
}
}
1.1.2 stages: 包含一个或者多个stage的序列,pipline的大部分工作在此执行
必须出现的指令,无参数,每个pipeline代码区间中必须只有一个stages
stage:包含在stages中,pipeline完成的所有实际工作都需要包含到stage中
必须出现的指令,无参数,需要定义stage的名字
steps:必须出现的指令,无参数,具体执行步骤,包含在stage代码区间中
stages{
stage('git pull souce code'){
steps{
echo "sync updated code"
git "https://github.com/xxx/xxx.git"
}
}
}
1.1.3 post:定义pipeline或stage运行结束时的操作
参数:不是必须出现的指令
always:无论pipeline运行的完成状态如果都会运作
changed:只有当前pipeline运行的状态与先前完成的pipeline的状态不同时,才 能运行。
failure:仅当当前pipeline处于失败状态时才运行。
success:仅当当前pipeline处于成功状态时才运行。
unstable: 仅当当前pipeline处于不稳定状态时才运行。
aborted:只有当前pipeline 仅当当前pipeline处于中止状态时才运行。
post{
success{
echo 'goodbye pipeline success!'
sleep 2
}
always {
echo 'always say goodbye'
}
}
1.1.4 environment: 定义Pipeline或者stage运行时的环境变量
参数:
不是必须出现的指令
无参数
environment {
testHlw = 'hello world'
}
stages {
stage('Print environment_1') {
step {
echo testHlw
}
}
}
1.1.5 options: 定义pipeline的专有属性
参数: 不是必须出现的指令,
buildDiscarder:保持构建的最大个数
disableConcurrentBuilds: 不允许并行执行pipeline任务
timeout:pipeline 超时时间
retry:失败后,重试整个pipeline的次数
timestamps:预定义由pipeline生成的所有控制台输出时间
skipStagesAfterUnstable:一旦构建状态进入了“Unstable” 状态,
就跳过此stage。
options {
timeout(time:30, unit:'SECONDS')
buildDiscarder(logRotator(numToKeepStr:'2'))
retry(5)
}
1.1.6 parameters: 定义pipeline的专有参数列表
参数:不是必须出现的指令
支持数据类型:booleanParam,choice,credentials,file,
text,password,run,string
类似参数化构建的选项
parameters{
string(name:'PERSON', defaultValue:'Jenkins', description:'输入的文本参数')
}
stages{
stage('Test parameters'){
step{
echo "Hello ${params.PERSON}"
}
}
}
1.1.7 triggers: 定义了Pipeline自动化触发的方式
参数:不是必须出现的指令
cron: 接受一个cron风格的字符串来定义pipeline触发的常规间隔
pollSCM:定义Jenkins检查SCM源更改的常规间隔,如果存在
新的更改,则pipeline将被重新触发。
1.2 Scripts pipeline - 基于groovy的语法
TBC

本文介绍了Jenkinsfile的使用方法和语法,包括Declarative Pipeline的结构,如agent、stages、post、environment、options、parameters和triggers的详细说明,用于定义持续集成的流程。通过示例展示了如何创建和配置Jenkinsfile,以实现自动化构建和部署。
7686

被折叠的 条评论
为什么被折叠?



