Search code examples
jenkinsgroovyparameter-passing

Generating a dynamic parameter in Jenkins based off a parameter from the same job


I've got a Jenkins build with a choice box for build prefixes by release. It helps trigger a job based off the value of whatever specific build the person wanted.

I wanted to take the value of that choice box and transform the variable into the correct prefix based off the naming conventions typically used on this server for triggering the job based off its name.

So let's say I've got build prefix choices specifically for, ReleaseOne ReleaseTwo none

For none, meaning the parameters used won't try to access or set any specific release-based info by triggering the non-release-specified build.

I wanted to take the value of Release_Prefix and transform it, if needed, for the job that I trigger later. I was hoping to accomplish this with a dynamic parameter or similar mechanism. I'm not sure if my script is bugged, or something fundamental is not working to my intent. This might be the case, based off some alluded feedback from a similar question.

Can I do something like this snippet below? If not with Dynamic Parameter plugin + GroovyScript, what would you suggest? This currently seems to return nothing, regardless of my choice.

Formatted_Prefix parameter, Dynamic Parameter

switch(binding.getVariables().get("Release_Prefix"))
{
    case "none":
        return "";
    case "ReleaseOne":
        return "ReleaseOne_";
    case "ReleaseTwo":
        return "ReleaseTwo_";
    default:
        def prefix = binding.getVariables().get("Release_Prefix")
        return "$prefix_";
}

There's multiple ways I can overcome this, but if I can do it at the initial parameter stage, that would be best for me.


Solution

  • You can use EnvInject Plugin for this.

    Check the checkbox Prepare an environment for the run and write your script inside Evaluated Groovy script text box

    def prefix1 = Release_Prefix + "mydata"
    return[prefix:prefix1]