【问题标题】:execute multi-line commands inside jenkins DSL plugin在 jenkins DSL 插件中执行多行命令
【发布时间】:2019-07-23 06:12:53
【问题描述】:

使用 Jenkins 2

想从 git 存储库生成动态选择。因此,在 groovySrcipt 的脚本部分中,

job('example') {
parameters {
 activeChoiceParam('CHOICE-1') {
  description('Allows user choose from multiple choices')
  filterable()
  choiceType('SINGLE_SELECT')
  groovyScript {
   // script('["choice1", "choice2"]')
   script('-DYNANIC CHOICES-REF-PESUDO-CODE')
   fallbackScript('"fallback choice"')
  }
 }
}}

伪代码

groovyScript {
 script('
   // call git repo and fetch some branches info.
   // collect them in some local variable and return them as choices
   def branches = "git branch -l"
   return branches 
 ')
 fallbackScript('"fallback choice"')
}

在我的情况下,它总是要回退脚本。 请分享您的意见。

【问题讨论】:

    标签: jenkins plugins dsl


    【解决方案1】:

    Jenkins 作业或管道输入在作业真正开始之前显示。
    我遇到了同样的要求,最后完成了一项更新它自己的参数设置并要求重新运行的工作。

    即:

    • 检查作业设置是否正常
      • 如果不行,创建它并使用“CANCEL”更新作业状态,并向用户提供消息或描述
      • 工作正常,表示用户在这个阶段已经提供了参数

    编辑

    示例代码:

    
    EXPECTED_VERSION = "v1.1"
    
    // check setup : read variable "SETUP_VERSION" and compare with constant in code
    if ( "${SETUP_VERSION}" != "${EXPECTED_VERSION}" ) {
      // if different, run setup and stop build
      createSetup()
      echo "Setup updated, please restar"
      currentBuild.result = 'ABORTED'
    }
    // else continue processing
    ...
    
    // create setup
    void createSetup() {
    
        def myParameters = [choice(
                choices: "${EXPECTED_VERSION}"
                , description: 'JenkinsFile version for internal testing only'
                , name: 'SETUP_VERSION'
        )]
    
       // stuff
       myParameters << string(
            defaultValue: ''
            , description: 'some input'
            , name: 'MY_INPUT')
    }
    

    【讨论】:

    • 感谢 Grunnpi 对帖子的评论。答案对我来说仍然是董事会。你能用一些代码sn-p或参考文档解释一下吗?
    • 用最少的代码更新了答案。您应该调整“刷新设置”条件,以便将当前版本与预期版本进行比较(我依赖版本号)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2014-01-11
    • 2012-11-02
    • 1970-01-01
    • 2020-05-19
    • 2019-03-02
    • 2017-03-14
    相关资源
    最近更新 更多