Jenkins(八)

Jenkins(八)

参数化流水线

Jenkins pipeline中定义参数使用parameters指令,只允许放在pipeline块下

http://127.0.0.1:8080/directive-generator/可以自动生成

生成

1
2
3
parameters {
booleanParam defaultValue: false, description: '布尔值参数', name: 'FLAG'
}
  • defaultValue:默认值
  • description:描述信息
  • name:参数名 使用方法:${params.FLAG}
1
2
3
4
5
6
7
8
9
10
11
12
13
pipeline {
agent any
parameters {
booleanParam(defaultValue: true,description: '',name:'userFlag')
}
stages {
stage('foo'){
steps {
echo "flag: ${params.userFlag}"
}
}
}
}

支持的参数类型

  • string
1
2
3
parameters {
string defaultValue: 'none', description: '666', name: 'D_ENV', trim: true
}

字符参数

  • text
1
2
3
parameters {
text defaultValue: 'a\nb\nc\n', description: '', name: 'D_TEXT'
}
  • booleanParam
  • choice
1
2
3
parameters {
choice choices: 'a\nb\nc\n', description: '', name: 'D_CHOICE'
}
  • file(有BUG不要用)
  • password
1
2
3
parameters {
password name: 'a\nb\nc\n', description: '', name: 'D_CHOICE'
}

综上:

1
2
3
4
5
6
7
parameters {
string defaultValue: 'none', description: '字符串', name: 'D_ENV', trim: true
text defaultValue: 'a\nb\nc\n', description: '文本', name: 'D_TEXT'
choice choices: 'a\nb\nc\n', description: '选一个', name: 'D_CHOICE'
booleanParam defaultValue: false, description: '布尔值参数', name: 'FLAG'
password name: 'PASSWORD',defaultValue:'SECRET',description: 'password'
}

页面展示

Extended Choice Parameter

一个实现复杂的参数化选择的插件

https://wiki.jenkins.io/display/JENKINS/Extended+Choice+Parameter+plugin

插件安装

使用官网的一个例子:

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
import org.boon.Boon;

def jsonEditorOptions = Boon.fromJson(/{
disable_edit_json: true,
disable_properties: true,
no_additional_properties: true,
disable_collapse: true,
disable_array_add: true,
disable_array_delete: true,
disable_array_reorder: true,
theme: "bootstrap2",
iconlib:"fontawesome4",
schema: {
"title": "Color Picker",
"type": "object",
"properties": {
"color": {
"type": "string",
"format": "color"
}
}
},
startval: {
color :"red"
}
}/);

颜色选择

完整流水线

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
pipeline{
agent any
parameters {
extendedChoice bindings: '', description: '', groovyClasspath: '', groovyScript: '''
import org.boon.Boon;
def jsonEditorOptions = Boon.fromJson(/{
disable_edit_json: true,
disable_properties: true,
no_additional_properties: true,
disable_collapse: true,
disable_array_add: true,
disable_array_delete: true,
disable_array_reorder: true,
theme: "bootstrap2",
iconlib:"fontawesome4",
schema: {
"title": "Color Picker",
"type": "object",
"properties": {
"color": {
"type": "string",
"format": "color"
}
}
},
startval: {
color :"red"
}}/);
''', multiSelectDelimiter: ',', name: 'Policy', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_JSON', visibleItemCount: 5
}

stages{
stage('Example'){
steps{
withPythonEnv('/usr/bin/python'){
sh 'python -m pip install pytest '
sh 'python -m pip install allure-pytest'
sh 'python -m pytest -v test_allure.py --alluredir=allure-results'
}
exit 0
}
}
}
post{
always{
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}
}
}

可以发现这段Groovy代码太长了,所以将它进行提取

创建共享库

共享库

新建一个sayHello.groovy文件

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
def call() {
return """
import org.boon.Boon;
def jsonEditorOptions = Boon.fromJson(/{
disable_edit_json: true,
disable_properties: true,
no_additional_properties: true,
disable_collapse: true,
disable_array_add: true,
disable_array_delete: true,
disable_array_reorder: true,
theme: "bootstrap2",
iconlib:"fontawesome4",
schema: {
"title": "Color Picker",
"type": "object",
"properties": {
"color": {
"type": "string",
"format": "color"
}
}
},
startval: {
color :"red"
}
}/);
"""
}

使用共享库

修改Jenkinsfile为:

导包@Library('extended-library') _

引用:sayHello()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Library('extended-library') _
pipeline {
agent any
parameters {
extendedChoice bindings: '', description: '', groovyClasspath: '', groovyScript: sayHello(), multiSelectDelimiter: ',', name: 'Policy', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_JSON', visibleItemCount: 5
}
stages {
stage('build') {
steps{
echo "Hello"
}
}
}
}

文件结构

修改

点一下 In-Process Script Approval再点一下Approve

应用

查看

最后

到此Jenkins as code的常用部分都简单过了一遍

下面推荐一些插件

  • 凭证管理:HashiCorp Vault
  • 制品管理-版本号管理:Version Number
  • 可视化构建:Build Monitor View
  • 自动化部署:Ansible
  • 通知:Email Extension,集成钉钉机器人,HTTP Request
  • Jenkins备份:Periodic Backup
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!