简体   繁体   中英

Unable to access yaml objects after reading yaml from jenkins pippeline

I am trying to get objects like map and array list from yaml via readYaml Step in to variable 'configObj' and pass 'configObj' to groovy script in vars/ folder for extracting . Map and array from 'configObj'. This is in jenkins DSL Pipeline with Shared Library

I tried readYaml and pass the yaml object to groovy script. Object received in groovy script was found to be 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
        ...
       }
}

Expected
to get configObj in groovy script Expected to access values of setup_setps as : configObject.setup_steps.each{ echo("${it}")}

Similarly map objests represented by snapShotInfo in yaml.

Actual :

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)

Found that call implementation excepts one parameter of type LinkedHashList. To overcome this requirement.

  • I used "${STAGE_NAME}" to get stage Information

jenkinsfile

@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
   }
}

This logic works fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM