【问题标题】:Unable to access yaml objects after reading yaml from jenkins pippeline从 jenkins 管道读取 yaml 后无法访问 yaml 对象
【发布时间】:2019-07-26 08:17:00
【问题描述】:

我正在尝试通过 readYaml 从 yaml 获取地图和数组列表等对象 进入变量 'configObj' 并将 'configObj' 传递给 vars/ 文件夹中的 groovy 脚本以进行提取。来自“configObj”的映射和数组。 这是在 jenkins DSL Pipeline 和共享库中

我尝试了 readYaml 并将 yaml 对象传递给 groovy 脚本。发现在 groovy 脚本中收到的对象为 NULL。

//-----Jenkinsfile------

@Library('Library')_

configObj = ""

pipeline{
    ...
script{

    configObj = readYaml file : 'config/TestbedSpecificConfig.yaml'

    echo("${configObj.setup_steps}") 

    flowManager operation : "INI_ZN", config : configObj
 }
       ...    
}

//---------------Yaml-----------------

---
-
  setup_steps:
    - stop_tomcat
    - featurestress_backup
    - update_release_type
    - update_branch_name
    - update_testcase
- snapshotInfo:
    -  snapshot_name: vSphere65U2
    -  infra_ip: 10.173.124.1
    -  esxi_base_name: vEsxi-173-
    -  esxi_start_index: 101
    -  esxi_end_index: 200
    - revert_appliances :
        - Embedded_60_65_Upgrade: vc65
    - delete_target_vc  :
        -  Embedded_65_67_Upgrade
        -  Embedded_67_68_Upgrade
        -  Embedded_65_68_Upgrade
        -  Embedded_60_68_Upgrade
        -  extpsc.st.local

//------------flowManager------------

def call(Map propertes){

    FolderUtils    futils  =  new FolderUtils(this)
    CliUtils       cutils  =  new CliUtils(this)
    RestUtils      rutils  =  new RestUtils(this)
    TestBedUtils   tutils  =  new TestBedUtils(this)
    WebAppUtils    wutils  =  new WebAppUtils(this)
    TemplateHelper thelpar =  new TemplateHelper(this) 


    switch("${propertes.operation}") {
        case "INI_ZN":
            log.info("OPERATION : ${propertes.operation}" )
            log.info("CONFIG :  ${properties.config}") <=== This prints NULL
            wutils.init(properties.config)
        break
        ...
       }
}

预期
在 groovy 脚本中获取 configObj 预计将 setup_setps 的值访问为: configObject.setup_steps.each{ echo("${it}")}

类似地映射 yaml 中 snapShotInfo 表示的对象。

实际:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String setup_steps
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:409)
    at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:289)

【问题讨论】:

    标签: jenkins yaml jenkins-pipeline shared-libraries dsl


    【解决方案1】:

    发现调用实现除了一个LinkedHashList类型的参数。 为了克服这个要求。

    • 我使用“${STAGE_NAME}”获取舞台信息

    詹金斯文件

    @Library('Library')_
    
    configObj = ""
    
    pipeline{
        ...
    script{
    
        configObj = readYaml file : 'config/TestbedSpecificConfig.yaml'
    
        flowManager  config : configObj
     }
           ...    
    }
    

    vars/flowManager,groovy

    def call(def config){
    
       switch("${STAGE_NAME}"){
           case "Pipeline Initialization":
                wutils.initialization(config)
           break
       }
    }
    

    这个逻辑对我来说很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多