歡迎關注我公眾號呀~「測試游記」「zx94_11」
參數化流水線
Jenkins pipeline中定義參數使用parameters
指令,只允許放在pipeline塊下
在http://127.0.0.1:8080/directive-generator/可以自動生成
生成
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
}
字符參數
- 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'
}
頁面展示
Extended Choice Parameter
一個實現復雜的參數化選擇的插件
https://wiki.jenkins.io/display/JENKINS/Extended+Choice+Parameter+plugin
插件安裝
使用官網的一個例子:
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"
}
}/);
顏色選擇
完整流水線
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
文件
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"
}
}
}
}
文件結構
修改
點一下In-Process Script Approval
再點一下Approve
應用
查看
最后
到此Jenkins as code的常用部分都簡單過了一遍
下面推薦一些插件
- 憑證管理:HashiCorp Vault
- 制品管理-版本號管理:Version Number
- 可視化構建:Build Monitor View
- 自動化部署:Ansible
- 通知:Email Extension,集成釘釘機器人,HTTP Request
- Jenkins備份:Periodic Backup