Jenkins(八)

参数化流水线

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

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

640?wx_fmt=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
字符参数
  • 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
页面展示

Extended Choice Parameter

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

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

640?wx_fmt=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
颜色选择

完整流水线

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
共享库

新建一个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
文件结构
640?wx_fmt=jpeg
修改

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

640?wx_fmt=jpeg
应用
640?wx_fmt=jpeg
查看

最后

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

下面推荐一些插件

  • 凭证管理:HashiCorp Vault

  • 制品管理-版本号管理:Version Number

  • 可视化构建:Build Monitor View

  • 自动化部署:Ansible

  • 通知:Email Extension,集成钉钉机器人,HTTP Request

  • Jenkins备份:Periodic Backup

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值