美文网首页
Jenkins(八)

Jenkins(八)

作者: 测试游记 | 来源:发表于2019-09-14 16:09 被阅读0次

    欢迎关注我公众号呀~「测试游记」「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

    相关文章

      网友评论

          本文标题:Jenkins(八)

          本文链接:https://www.haomeiwen.com/subject/sbttyctx.html