Search code examples
jenkinsjenkins-pluginsjenkins-groovyextended-choice-parameter

Jenkins parameters from property file based on global env vars


Context:

input parameters to jenkins job are defined in a property file. property file location changes based on environment in which jenkins is running

ex: for dev environment path be like /app/dev/some/nested/path/propertyfile

for prod environment path be like /app/prod/some/nested/path/propertyfile

presently using extended choice parameter plugin to read property file. this works well if path to property file is absolute.

Problem:

is there a way to include global env variable in property file path? can it be done using active-choices plugin?


Solution

  • using active-choices plugin

    def choices=[]
    textFile= new File("/app/${Gloabl_Var}/some/nested/path/propertyfile")
    textFile.eachLine{ line ->
    if(line.startsWith('<Property-Key-Name>')) {
     line.split('=')[1].split(',').each {
    choices.add(it)
    }}}
    return choices
    

    using extended choice parameter plugin

    can't use property file directly, will need to use groovy script

    import hudson.slaves.EnvironmentVariablesNodeProperty
    import jenkins.model.Jenkins
    def global_env_var=Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars['<Global_Var>']
    def props = new Properties()
    def stream = new FileInputStream("/app/${global_env_var}/some/nested/path/propertyfile")
    try {
    props.load(stream)
    } finally {
    stream.close()
    }
    return props.getProperty('<Property-Key-Name>').split(',')
    

    References:

    extended-choice

    active-choices