参数化流水线
Jenkins pipeline中定义参数使用parameters
指令,只允许放在pipeline块下
在http://127.0.0.1:8080/directive-generator/可以自动生成
![生成 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/e08b1d748050e4c3ed2e265b670d7eca.jpeg)
parameters {
booleanParam defaultValue: false, description: '布尔值参数', name: 'FLAG'
}
defaultValue:默认值
description:描述信息
name:参数名
使用方法:${params.FLAG}
pipeline {
agent any
parameters {
booleanParam(defaultValue: true,description: '',name:'userFlag')
}
stages {
stage('foo'){
steps {
echo "flag: ${params.userFlag}"
}
}
}
}
支持的参数类型
string
parameters {
string defaultValue: 'none', description: '666', name: 'D_ENV', trim: true
}
![字符参数 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/84acfa73870c5d0d0ba58dfa91447c94.jpeg)
text
parameters {
text defaultValue: 'a\nb\nc\n', description: '', name: 'D_TEXT'
}
booleanParam
choice
parameters {
choice choices: 'a\nb\nc\n', description: '', name: 'D_CHOICE'
}
file(有BUG不要用)
password
parameters {
password name: 'a\nb\nc\n', description: '', name: 'D_CHOICE'
}
综上:
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'
}
![页面展示 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/2bd8466bab59b8b9a70404baee3ec6f4.jpeg)
Extended Choice Parameter
一个实现复杂的参数化选择的插件
https://wiki.jenkins.io/display/JENKINS/Extended+Choice+Parameter+plugin
![插件安装 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/1fe53f3a9178d5921d331cb551ba3320.jpeg)
使用官网的一个例子:
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"
}
}/);
![颜色选择 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/0c0412b5c4b74852f7fcf2342de164a4.jpeg)
完整流水线
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代码太长了,所以将它进行提取
创建共享库
![共享库 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/8485490d0748124db1fb002239ebbb5a.jpeg)
新建一个sayHello.groovy
文件
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()
@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"
}
}
}
}
![文件结构 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/b7e8378b237eab625e1e2e1301458817.jpeg)
![修改 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/79d54c79e5e7d00666a6b2fcd287d765.jpeg)
点一下In-Process Script Approval
再点一下Approve
![应用 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/3a133c1ba49cd4a35e9bd7f7b886ea48.jpeg)
![查看 640?wx_fmt=jpeg](https://i-blog.csdnimg.cn/blog_migrate/a2002cfa493372cf2f04bc73767e87f2.jpeg)
最后
到此Jenkins as code的常用部分都简单过了一遍
下面推荐一些插件
凭证管理:HashiCorp Vault
制品管理-版本号管理:Version Number
可视化构建:Build Monitor View
自动化部署:Ansible
通知:Email Extension,集成钉钉机器人,HTTP Request
Jenkins备份:Periodic Backup