Jenkins 参数化构建
在一些场景下参数化构建非常有必要。
对于“自由风格项目”和“流水线项目”可以直接在界面配置,问题不大。
对于“多分支流水线项目”,没有界面配置项,只能在 jenkinsfile中编写,并构建一次后生效。
-> 流水线参数化构建
jenkinsfile声明的方式对于流水线和多分支流水线均是生效的, 只是流水线项目还提供界面编辑选择
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
提示
定义的参数在运行过程中会被设定为同名环境变量, 后续可二次赋值覆盖**
-> 扩展选择参数
安装 extend choice (opens new window)插件,扩展 choice参数
parameters {
choice(name: 'test', choices: ['one', 'two'], description: '')
extendedChoice(
defaultValue: 'Nine',
description: '',
multiSelectDelimiter: ',',
name: 'SAMPLE_EXTENDED_CHOICE',
quoteValue: false,
saveJSONParameterToFile: false,
type: 'PT_RADIO',
value:'One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten',
visibleItemCount: 5
)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
-> 动态参数
安装 Active Choices (opens new window)插件,使得参数可以按层级管理
def arg = 2.0
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_RADIO',
description: '',
filterLength: 1, filterable: false,
name: 'Env',
randomName: 'choice-parameter-10625228584773874',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: [], sandbox: false, script: ''],
script: [classpath: [], sandbox: true, script: 'return ["Dev", "QA", "Stage:selected", "Prod"]']
]
],
[$class: 'CascadeChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: '',
filterLength: 1, filterable: false,
name: 'Server',
randomName: 'choice-parameter-10625228590565996',
referencedParameters: 'Env',
script: [
$class: 'GroovyScript',
fallbackScript: [classpath: [], sandbox: false, script: 'return ["NOT FOUND"]'],
script: [
classpath: [],
sandbox: true,
script: '''
if (Env.equals("Dev")){
return ["devaaa001","devaaa002","devbbb001","devbbb002","devccc001","devccc002"]
}
else if(Env.equals("QA")){
return ["qaaaa001","qabbb002","qaccc003"]
}
else if(Env.equals("Stage")){
return ["staaa001","stbbb002","stccc003"]
}
else if(Env.equals("Prod")){
return ["praaa001","prbbb002","prccc003"]
}else{
return ["NOT FOUND"]
}
'''
]
]
],
[$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
description: '',
name: 'Version',
omitValueField: true,
randomName: 'choice-parameter-10646503850602470',
referencedParameters: 'Env',
script: [
$class: 'GroovyScript', fallbackScript: [classpath: [], sandbox: true,
script: '<input name="value" value="NOT FOUND" class="setting-input" type="text">'],
script: [classpath: [], sandbox: true, script: """
if (Env.equals("Dev")){
return '<input name="value" value="1.0" class="setting-input" type="text">'
}
else if(Env.equals("QA")){
return '<input name="value" value="${arg}" class="setting-input" type="text">'
}
else if(Env.equals("Stage")){
return '<input name="value" value="3.0" class="setting-input" type="text">'
}
else if(Env.equals("Prod")){
return '<input name="value" value="4.0" class="setting-input" type="text">'
}
"""
]
]
]
])
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-> 定时参数化构建
安装 Parameterized Scheduler
插件实现该需求
triggers {
parameterizedCron('''
# leave spaces where you want them around the parameters. They'll be trimmed.
# we let the build run with the default name
*/2 * * * * % PERSON=Hola; BIOGRAPHY=Pluto
# another cron
*/3 * * * * % CHOICE=Two; TOGGLE=false
''')
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
%
为分隔符,前面为定时任务的语法,后面为参数,没有指定的则使用默认参数
注意
如果 cron编写错误(例如对于列表参数没有给出合法的选择),定时任务则会表现为不触发
-> 远程触发器
主要是调用 Jenkins提供的远程访问API (opens new window)
- 如果待构建任务没有参数,
可直接向任务 URL发生 POST请求即可,
POST on JENKINS_URL/job/JOBNAME/build
eg:curl -X POST JENKINS_URL/job/test/build --user USER:TOKEN
1JENKINS_URL/job/test/build
为任务url,从对应任务的浏览器地址栏 copy即可USER:TOKEN
为需要鉴权的登录用户名密码build
,构建指令
- 如果有参数
构建指令更换为
buildWithParameters
, 另增加传参,eg:curl -X POST JENKINS_URL/job/test/buildWithParameters \ --user USER:TOKEN \ -d Project=Test&Service=Test
1
2
3- 参数名和构建项目页的参数展示保持一致,大小写敏感
- 没有传递的参数使用默认值
- 参数名写错也会使用默认值
需要注意的是:
- 该操作的原理是给指定url发生 post请求,由 server启动对应任务构建
- 并不限定请求方式,可以使用 postman,各种编程语言编写脚本等没有限制
- 必须使用用户名密码做权限认证,否则无法通过
- 注意参数传递的合法性
- 默认参数 delay可控制延时,eg:
delay=0sec
, 立即触发构建
-> 参考
编辑此页 (opens new window)
更新于: 2019-10-01