前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jenkins(八)

Jenkins(八)

作者头像
zx钟
发布2019-07-18 10:51:51
1.5K0
发布2019-07-18 10:51:51
举报
文章被收录于专栏:测试游记测试游记

参数化流水线

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

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

生成

代码语言:javascript
复制
parameters {
  booleanParam defaultValue: false, description: '布尔值参数', name: 'FLAG'
}
  • defaultValue:默认值
  • description:描述信息
  • name:参数名

使用方法:${params.FLAG}

代码语言:javascript
复制
pipeline {
    agent any
    parameters {
        booleanParam(defaultValue: true,description: '',name:'userFlag')
    }
    stages {
        stage('foo'){
            steps {
                echo "flag: ${params.userFlag}"
            }
        }
    }
}

支持的参数类型

  • string
代码语言:javascript
复制
parameters {
  string defaultValue: 'none', description: '666', name: 'D_ENV', trim: true
}

字符参数

  • text
代码语言:javascript
复制
parameters {
  text defaultValue: 'a\nb\nc\n', description: '', name: 'D_TEXT'
}
  • booleanParam
  • choice
代码语言:javascript
复制
parameters {
  choice choices: 'a\nb\nc\n', description: '', name: 'D_CHOICE'
}
  • file(有BUG不要用)
  • password
代码语言:javascript
复制
parameters {
  password name: 'a\nb\nc\n', description: '', name: 'D_CHOICE'
}

综上:

代码语言:javascript
复制
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

插件安装

使用官网的一个例子:

代码语言:javascript
复制
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"
        }
}/);

颜色选择

完整流水线

代码语言:javascript
复制
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文件

代码语言:javascript
复制
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()

代码语言:javascript
复制
@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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 测试游记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参数化流水线
    • 支持的参数类型
      • Extended Choice Parameter
        • 创建共享库
        • 使用共享库
    • 最后
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档