簡體   English   中英

從jenkins pippeline讀取Yaml后無法訪問yaml對象

[英]Unable to access yaml objects after reading yaml from jenkins pippeline

我試圖通過readYaml從yaml中獲取諸如map和array list之類的對象進入變量'configObj'並將'configObj'傳遞給vars /文件夾中的groovy腳本以進行提取。 從“ configObj”映射和排列。 這在帶有共享庫的詹金斯DSL管道中

我嘗試了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)

發現該調用實現除了一個類型為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
   }
}

這種邏輯對我來說很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM