JENKINS: ACTIVECHOICE: access build environment variables

Viewed 15

I have created a pipeline with active choice to parameterize the workflow. The code to update the field is embedded in the jenkinsfile like so:

pipeline {
 agent { label 'master' }
 stages {
  steps {
   script {
    properties {[
     parameters {[

[...some more code before...]

[$class: 'CascadeChoiceParameter',
    choiceType: 'PT_MULTI_SELECT',
    description: 'Select ScopeData',
    referencedParameters: 'ScopeToScan',
    filterLength: 1,
    filterable: true,
    name: 'ScopeData',
    script: [
        $class: 'GroovyScript',
        fallbackScript: [
        classpath: [],
        sandbox: true,
        script: '''
        return['Error loading ScopeToScan']
        '''
    ],
    script: [
        classpath: [],
        sandbox: true,
        script: '''
        import groovy.json.JsonSlurperClassic

        File json= new File("C:\\\\Program Files (x86)\\\\Jenkins\\\\workspace\\\\SOME_JOB_NAME\\\\SharedLibrary\\\\configs\\\\settings.json")

        def jsonSlurperClassic = new JsonSlurperClassic()
        Map jsonData = jsonSlurperClassic.parse(json)

        jsonSlurperClassic = ""

        if(ScopeToScan =='Region'){
        def regions = []
        regions.add('Global:selected')
        for(Region in jsonData.Regions){
        regions.add(Region.name)
        }
        return regions
        }
[....Some More Code after this....]

You can see, it is a multiple choice field, that reacts on another field. The content of it, is read from a JSON file in the Repository itself:

File json= new File("C:\\\\Program Files (x86)\\\\Jenkins\\\\workspace\\\\SOME_JOB_NAME\\\\SharedLibrary\\\\configs\\\\settings.json")

Currently the "SOME_JOB_NAME" is a static value, that needs to be tuned, if the Jenkins Job changes. I would like to get that dynamic by using "JOB_NAME" or even better "WORKSPACE" from the jenkins environment. But I fail to get that working in the parameters section of the pipeline.

Is it impossible, or do I miss anything?

Please consider before answering: I am not a Groovy expert, neither Java. The script above is mainly grabbed by Google.

any suggestions?

br

0 Answers
Related