|NO.Z.00367|——————————|CloudNative|——|KuberNetes&CI/CD.V05|——|Jenkins.v05|声明式流水线|
一、Jenkins声明式流水线
### --- Jenkins-pipeline流水线英文文档官方地址
~~~ 地址:https://www.jenkins.io/doc/book/pipeline/syntax/
### --- Jenkins-pipeline流水线中文文档官方地址
~~~ 地址:https://www.jenkins.io/zh/doc/book/pipeline/syntax/


二、Jenkins声明式流水线
### --- 以pipeline的形式生成一个变量
~~~ 指令提供了一个用户在触发流水线时应该提供的参数列表。
~~~ 这些用户指定参数的值可通过 params 对象提供给流水线步骤, 了解更多请参考示例。
Required No
Parameters None
Allowed Only once, inside the pipeline block.
~~~ 可用参数
string
~~~ 字符串类型的参数, 例如: parameters { string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: '') }
booleanParam
~~~ 布尔参数, 例如: parameters { booleanParam(name: 'DEBUG_BUILD', defaultValue: true, description: '') }
### --- 创建一个Job
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
}
}
}
}
三、通过web控制台添加一个变量:创建一个Job
### --- 通过web控制台添加一个变量:创建一个Job
~~~ Create a job——>Enter an item name:pipeline-test——>Pipeline——>OK——>
~~~ General——>This project is parameterized——>String Parameter——>Name:TEST
~~~ ——>Default Value:a ——>Descriptions:test env——>
~~~ Pipeline——>Definition:Pipeline script
~~~ ——>Script:try sample Pipeline:Hello World——>修改如下变量——>Save——>
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo "${TEST}"
}
}
}
}

~~~ Build with Parameters——>Pipeline Pipeline-test:TEST:a:test env——>变量已经打印出来
~~~ Build构建够输出结果
~~~ # 输出结果
#SuccessConsole Output
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
a
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
四、通过pipeline的方式通过parameters的方式自动的生成一个变量:创建一个Job
### --- 通过pipeline的方式通过parameters的方式自动的生成一个变量:创建一个Job
~~~ Create a job——>Enter an item name:pipeline-test——>Pipeline——>OK——>
~~~ General——>This project is parameterized——>String Parameter
~~~ ——>Name:TEST——>Default Value:a ——>Descriptions:test env——>
~~~ Pipeline——>Definition:Pipeline script
~~~ ——>Script:try sample Pipeline:添加变量配置——>Save
~~~ # 修改后的变量是有bug,需要把之前的job执行一次:Build withParameters才会出现
~~~ ——>这个变量就生成了
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
}
}
}
}

五、Input参数
### --- Input
~~~ stage 的 input 指令允许你使用 input step提示输入。 在应用了 options 后,
~~~ 进入 stage 的 agent 或评估 when 条件前, stage 将暂停。
~~~ 如果 input 被批准, stage 将会继续。
~~~ 作为 input 提交的一部分的任何参数都将在环境中用于其他 stage。
### --- 配置项
message // 必需的。 这将在用户提交 input 时呈现给用户。
id // input 的可选标识符, 默认为 stage 名称。
ok // `input`表单上的"ok" 按钮的可选文本。
submitter // 可选的以逗号分隔的用户列表或允许提交 input 的外部组名。默认允许任何用户。
submitterParameter // 环境变量的可选名称。如果存在,用 submitter 名称设置。
parameters // 提示提交者提供的一个可选的参数列表。 更多信息参见 [parameters]。
### --- Input参数
~~~ Create a job——>Enter an item name:pipeline-test——>Pipeline——>OK——>
~~~ General——>This project is parameterized——>String Parameter——>Name:TEST
~~~ ——>Default Value:a ——>Descriptions:test env——>
~~~ Pipeline——>Definition:Pipeline script
~~~ ——>Script:try sample Pipeline:添加变量配置——>Save
### --- Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Example') {
input {
message "Should we continue?"
ok "Yes, we should."
submitter "alice,bob"
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
}
steps {
echo "Hello, ${PERSON}, nice to meet you."
}
}
}
}
~~~ # 修改后的变量是有bug,需要把之前的job执行一次:Build withParameters才会出现
~~~ ——>这个变量就生成了
### --- 输出结果
#SuccessConsole Output
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example)
[Pipeline] input
Input requested
Approved by admin
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
Hello, Mr Jenkins, nice to meet you.
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
~~~ # 显示Input requested——>点击Input requested——>Yes we shouldj:继续
~~~ ——>Job执行完成——>END
~~~ 说明Abort:终止

六、timeout:添加超时参数:
### --- timeout:添加超时参数:
~~~ 添加超时参数:10秒钟之后自动执行下一步——>Save
~~~ 使用input的同时,需要设置超时参数,到了images之后就不执行了
pipeline {
agent any
options {
timeout(time: 10, unit: 'SECONDS')
} # 设定超时参数,10秒中之后自动执行下一操作
stages {
stage('Example') {
input {
message "Should we continue?"
ok "Yes, we should."
submitter "alice,bob"
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
}
}
steps {
echo "Hello, ${PERSON}, nice to meet you."
}
}
}
}
### --- 输出结果
#AbortedConsole Output
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline-test
[Pipeline] {
[Pipeline] timeout
Timeout set to expire in 10 sec // 时间10秒钟已经定义了
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Example)
[Pipeline] input
Input requested
Cancelling nested steps due to timeout // 处于超时状态
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Rejected by SYSTEM
Finished: ABORTED
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通