Jenkins 如何有条件地隐藏参数

Jenkins 如何有条件地隐藏参数,jenkins,jenkins-plugins,Jenkins,Jenkins Plugins,我能够使输入显示和显示与积极的选择插件。但参数的名称并不是动态隐藏的,我不知道如何实现这一点 导言 我想在复选框为true时显示参数,在为false时隐藏它。我通过主动选择插件实现了这一点。不幸的是,若您为参数命名,该名称将不会隐藏。所以我想出了两个解决办法 第一种解决方案 这个解决方案有一点缺陷。因为position设置为“绝对”,所以只要窗口大小不变,一切都可以。这段代码应该作为groovy脚本在“Active-Choices-Reactive-Reference-Parameter”和Re

我能够使输入显示和显示与积极的选择插件。但参数的名称并不是动态隐藏的,我不知道如何实现这一点

导言 我想在复选框为true时显示参数,在为false时隐藏它。我通过主动选择插件实现了这一点。不幸的是,若您为参数命名,该名称将不会隐藏。所以我想出了两个解决办法

第一种解决方案 这个解决方案有一点缺陷。因为position设置为“绝对”,所以只要窗口大小不变,一切都可以。这段代码应该作为groovy脚本在“Active-Choices-Reactive-Reference-Parameter”和Reference-PARAM_1中运行。参数应返回“格式化的HTML”

if(参数1){
def FullHTML=“”
输入
"""
返回完整的HTML
}
第二种解决方案 此解决方案使用jquery修改“设置名称”元素。这段代码应该作为groovy脚本在“Active-Choices-Reactive-Reference-Parameter”和Reference-PARAM_1中运行。参数应返回“格式化隐藏HTML”

if(参数1){
def FullHTML=“”
"""
返回完整的HTML
}
再添加一个包含此代码的“有功选择无功参考参数”。再次作为groovy脚本运行并返回“格式化隐藏HTML”

编辑

我修改了这段代码,以便在页面上未显示元素时隐藏空白。因为詹金斯把它放在桌子上,即使是空的,每一行也占用了空间

def FullHTML = """
<script>/*
 * arrive.js
 * v2.4.1
 * https://github.com/uzairfarooq/arrive
 * MIT licensed
 *
 * Copyright (c) 2014-2017 Uzair Farooq
 */
var Arrive=function(e,t,n){"use strict";function r(e,t,n){l.addMethod(t,n,e.unbindEvent),l.addMethod(t,n,e.unbindEventWithSelectorOrCallback),l.addMethod(t,n,e.unbindEventWithSelectorAndCallback)}function i(e){e.arrive=f.bindEvent,r(f,e,"unbindArrive"),e.leave=d.bindEvent,r(d,e,"unbindLeave")}if(e.MutationObserver&&"undefined"!=typeof HTMLElement){var o=0,l=function(){var t=HTMLElement.prototype.matches||HTMLElement.prototype.webkitMatchesSelector||HTMLElement.prototype.mozMatchesSelector||HTMLElement.prototype.msMatchesSelector;return{matchesSelector:function(e,n){return e instanceof HTMLElement&&t.call(e,n)},addMethod:function(e,t,r){var i=e[t];e[t]=function(){return r.length==arguments.length?r.apply(this,arguments):"function"==typeof i?i.apply(this,arguments):n}},callCallbacks:function(e,t){t&&t.options.onceOnly&&1==t.firedElems.length&&(e=[e[0]]);for(var n,r=0;n=e[r];r++)n&&n.callback&&n.callback.call(n.elem,n.elem);t&&t.options.onceOnly&&1==t.firedElems.length&&t.me.unbindEventWithSelectorAndCallback.call(t.target,t.selector,t.callback)},checkChildNodesRecursively:function(e,t,n,r){for(var i,o=0;i=e[o];o++)n(i,t,r)&&r.push({callback:t.callback,elem:i}),i.childNodes.length>0&&l.checkChildNodesRecursively(i.childNodes,t,n,r)},mergeArrays:function(e,t){var n,r={};for(n in e)e.hasOwnProperty(n)&&(r[n]=e[n]);for(n in t)t.hasOwnProperty(n)&&(r[n]=t[n]);return r},toElementsArray:function(t){return n===t||"number"==typeof t.length&&t!==e||(t=[t]),t}}}(),c=function(){var e=function(){this._eventsBucket=[],this._beforeAdding=null,this._beforeRemoving=null};return e.prototype.addEvent=function(e,t,n,r){var i={target:e,selector:t,options:n,callback:r,firedElems:[]};return this._beforeAdding&&this._beforeAdding(i),this._eventsBucket.push(i),i},e.prototype.removeEvent=function(e){for(var t,n=this._eventsBucket.length-1;t=this._eventsBucket[n];n--)if(e(t)){this._beforeRemoving&&this._beforeRemoving(t);var r=this._eventsBucket.splice(n,1);r&&r.length&&(r[0].callback=null)}},e.prototype.beforeAdding=function(e){this._beforeAdding=e},e.prototype.beforeRemoving=function(e){this._beforeRemoving=e},e}(),a=function(t,r){var i=new c,o=this,a={fireOnAttributesModification:!1};return i.beforeAdding(function(n){var i,l=n.target;(l===e.document||l===e)&&(l=document.getElementsByTagName("html")[0]),i=new MutationObserver(function(e){r.call(this,e,n)});var c=t(n.options);i.observe(l,c),n.observer=i,n.me=o}),i.beforeRemoving(function(e){e.observer.disconnect()}),this.bindEvent=function(e,t,n){t=l.mergeArrays(a,t);for(var r=l.toElementsArray(this),o=0;o<r.length;o++)i.addEvent(r[o],e,t,n)},this.unbindEvent=function(){var e=l.toElementsArray(this);i.removeEvent(function(t){for(var r=0;r<e.length;r++)if(this===n||t.target===e[r])return!0;return!1})},this.unbindEventWithSelectorOrCallback=function(e){var t,r=l.toElementsArray(this),o=e;t="function"==typeof e?function(e){for(var t=0;t<r.length;t++)if((this===n||e.target===r[t])&&e.callback===o)return!0;return!1}:function(t){for(var i=0;i<r.length;i++)if((this===n||t.target===r[i])&&t.selector===e)return!0;return!1},i.removeEvent(t)},this.unbindEventWithSelectorAndCallback=function(e,t){var r=l.toElementsArray(this);i.removeEvent(function(i){for(var o=0;o<r.length;o++)if((this===n||i.target===r[o])&&i.selector===e&&i.callback===t)return!0;return!1})},this},s=function(){function e(e){var t={attributes:!1,childList:!0,subtree:!0};return e.fireOnAttributesModification&&(t.attributes=!0),t}function t(e,t){e.forEach(function(e){var n=e.addedNodes,i=e.target,o=[];null!==n&&n.length>0?l.checkChildNodesRecursively(n,t,r,o):"attributes"===e.type&&r(i,t,o)&&o.push({callback:t.callback,elem:i}),l.callCallbacks(o,t)})}function r(e,t){return l.matchesSelector(e,t.selector)&&(e._id===n&&(e._id=o++),-1==t.firedElems.indexOf(e._id))?(t.firedElems.push(e._id),!0):!1}var i={fireOnAttributesModification:!1,onceOnly:!1,existing:!1};f=new a(e,t);var c=f.bindEvent;return f.bindEvent=function(e,t,r){n===r?(r=t,t=i):t=l.mergeArrays(i,t);var o=l.toElementsArray(this);if(t.existing){for(var a=[],s=0;s<o.length;s++)for(var u=o[s].querySelectorAll(e),f=0;f<u.length;f++)a.push({callback:r,elem:u[f]});if(t.onceOnly&&a.length)return r.call(a[0].elem,a[0].elem);setTimeout(l.callCallbacks,1,a)}c.call(this,e,t,r)},f},u=function(){function e(){var e={childList:!0,subtree:!0};return e}function t(e,t){e.forEach(function(e){var n=e.removedNodes,i=[];null!==n&&n.length>0&&l.checkChildNodesRecursively(n,t,r,i),l.callCallbacks(i,t)})}function r(e,t){return l.matchesSelector(e,t.selector)}var i={};d=new a(e,t);var o=d.bindEvent;return d.bindEvent=function(e,t,r){n===r?(r=t,t=i):t=l.mergeArrays(i,t),o.call(this,e,t,r)},d},f=new s,d=new u;t&&i(t.fn),i(HTMLElement.prototype),i(NodeList.prototype),i(HTMLCollection.prototype),i(HTMLDocument.prototype),i(Window.prototype);var h={};return r(f,h,"unbindAllArrive"),r(d,h,"unbindAllLeave"),h}}(window,"undefined"==typeof jQuery?null:jQuery,void 0);
</script>

<script type='text/javascript'>
var nameElement;
function showText(textToShow, elementToEdit){
jQuery(elementToEdit).text(textToShow);
}

function hideElement(elementToHide) {
jQuery(elementToHide).css({"visibility" : "hidden", "position" : "absolute"});
}

function showElement(elementToShow) {
jQuery(elementToShow).css({"visibility" : "", "position" : ""});
}

jQuery(document).arrive("#input_field", function() { 
nameElement = jQuery('#input_field').parent().parent().parent().parent().find(".setting-name");
    showText("input_field", nameElement ); 
showElement(nameElement.parent().parent())
});

jQuery(document).leave("#input_field", function() { 
    showText("", nameElement); 
hideElement(nameElement.parent().parent());
});
</script>
"""
return FullHTML
def FullHTML=“”
/*
*到达.js
*v2.4.1
* https://github.com/uzairfarooq/arrive
*麻省理工学院许可
*
*版权所有(c)2014-2017 Uzair Farooq
*/

var-arrival=function(e,t,n){“使用严格”;function r(e,t,n){l.addMethod(t,n,e.unbindeventwithselectorcallback),l.addMethod(t,n,e.unbindEventWithSelectorAndCallback)}function i(e){e.arrival=f.bindEvent,r(f,e,“unbindEvent到达”),e.leave=d.bindEvent,r(d,e,e,“unbindEvent”)}如果(e.ionobserver&“未定义的类型”{var o=0,l=function(){var t=HTMLElement.prototype.matches | HTMLElement.prototype.webkitMatchesSelector | HTMLElement.prototype.mozMatchesSelector | HTMLElement.prototype.msMatchesSelector;return{matchesSelector:function(e,n){return e instanceof HTMLElement&&t.call(e,n)},addMethod:function(e,t,t,t,r){i=e[t];e[t]=function(){return r.length==arguments.length?r.apply(this,arguments):“function”==typeof i.apply(this,arguments):n},回调:function(e,t){t&&t.options.onceOnly&&1==t.firedElems.length&(e=[e[0]]);for(var n,r=0;n=e[r];r++)n&&n.callback n&&n.callback&n.callback&n.callback&n.callback.callback.call(n.elem,n.elem)t&&t.options.onceOnly&&1==t.firedElems.length&&t.me.unbindeventwithselector和callback.call(t.target,t.selector,t.callback)},checkchildnodes安全地:函数(e,t,n,r){for(var i,o=e[o];o++n(i,t,r)&&r.push({callback callback t:t.callback,elem:i}),i.childNodes.length>0&&l.checkchildesr安全地(i.node,o,o=0;i.node,o=e,o=e[o];i=e[o];数组,mergersin{var n,r={{{};r{{{{{n n,r={{};r{{{n n,r{{};r{n,r={{{};r{n,r={{};r{};r};r};r};r{n(n在e.n在e.e.Haowowown)e.e.HaOOOwn财产(n)和(n)e[n[n[n[n[n]=e[n[n[n][n[n][n[n]n]n]n]n]n]为;e;e(n)为(n)为(n)为(n在t.t)为(n在t)t.t.t.t.t.haown)为(n)为(n)t.t.t.t.t.t.haow属性(n)为(n)为(n)为(n)t.(e,t,n,r){var i={target:e,selector:t,options:n,callback:r,firedElems:[]};返回此值。{u-beforeAdding&&this。{u-beforeAdding(i),this。{u-eventsBucket.push(i),i},e.prototype.removeEvent=function(e){for(var t,n=this。{u-eventsBucket length-1;t=this.{eventsBucket[n];n--)如果(e(e(t)){this.\u-beforeAdding&&this.\u-eventsBucketr&&r.length&(r[0].callback=null)},e.prototype.beforeAdding=function(e){this.{beforeAdding=e},e.prototype.beforeAdding=function(e){this.{beforeAdding=e},e}(),a=function(t,r){VARI=new c,o=this,a={fireOnAttributesModification:!1};return i.beforeAdding(function)(function(function)(function)(function)(function(n){VARI,l=n.target e.document e},l==Get124E=e},e.document Get Agnese=e.html)(“文档”)“”[0]),i=newmutationobserver(function(e){r.call(this,e,n)});var c=t(n.options);i.observer(l,c),n.observer=i,n.me=o}),即在删除(function(e){e.observer.disconnect()})之前,this.bindEvent=function(e,t,n){t=l.mergeArray(a,t);对于(var r=l.toElementsArray(this),o=0;o我的Jenkins版本是2.272,我也通过主动选择插件实现了这一点

  • 添加一个名为
    PARA_1
  • 添加一个名为
    PARA_2
    Active Choices Reactive Reference参数
    ,选中
    PARA_1
    时要隐藏该参数
  • 使用
    第1段设置
    参考参数
  • 使用
    格式化HTML设置
    选择类型
  • 写剧本
  • 第1段
    脚本:

    return "<input name='value' type='checkbox' checked='checked'>"
    
    
    if (PARA_1) {
        return """
      <input name="value" id="PARA_2"  value="PARA_2">
      <img src="force/failed/load/image" style="display: none;" onerror='document.getElementById("PARA_2").closest(".form-group").style.display=""'>
    """  
    } else {
      return """
      <input name="value" id="PARA_2"  value="PARA_2">
      <img src="force/failed/load/image" style="display: none;" onerror='document.getElementById("PARA_2").closest(".form-group").style.display="none"'>
    """ 
    }
    

    表单组
    是Jenkins parameters HTML找到的类名

    您能分享一些更多的实现细节吗?可能是一些屏幕截图吗?这是否会在构建页面上根据之前的选择隐藏输入?(如被动参考参数?)@DenhamCoote我添加了第二个解决方案和稍微好一点的描述。很抱歉,我不能提供更多帮助-我正在按照同样的思路修补一些东西,但没有达到您的要求。另一个选项,根据您的偏好,可能不必费心隐藏字段,而是禁用它们。可能更容易/更不容易出错。@DenhamCoote I ad还有一个功能。正因为如此,它才能完全按照我的预期工作。@StKob请告诉我需要在哪里添加JS脚本并在groovy脚本中调用FullHTML
    return "<input name='value' type='checkbox' checked='checked'>"
    
    
    if (PARA_1) {
        return """
      <input name="value" id="PARA_2"  value="PARA_2">
      <img src="force/failed/load/image" style="display: none;" onerror='document.getElementById("PARA_2").closest(".form-group").style.display=""'>
    """  
    } else {
      return """
      <input name="value" id="PARA_2"  value="PARA_2">
      <img src="force/failed/load/image" style="display: none;" onerror='document.getElementById("PARA_2").closest(".form-group").style.display="none"'>
    """ 
    }