3

I have a parameterized pipeline project with active choices parameter, where choice list is dynamically populated by groovy script. I need to retrieve and use current job name in the script. The following line works for me in Freestyle Projects:

def jobName = this.binding.jenkinsProject.name

However when I try to use it in Pipeline Project I get:

No such property: jenkinsProject for class: groovy.lang.Binding

In Retrieving Jenkins job name using groovy script in Active Choice Reactive Parameter it's stated that this has been resolved in Active Choices plugin v1.4. I'm using version 2.2.1 and the issue still persists. Is this property not available in Pipeline Project? Is there a workaround or an alternative?

MaroonedMind
  • 141
  • 3
  • 7

3 Answers3

0

If you trying to get the current build job name inside running job There is a builtin env variable for it: JOB_BASE_NAME

You can see list of available env variable in your Jenkins at
http://{hostname}/job/{jobname}/pipeline-syntax/globals
Jest replace hostname with your Jenkins address and jobname with some job you have in your Jenkins.

My Jenkins

  • Jenkins version: 2.176.2
  • Active Choices Plug-in: 2.2.1

Worked with pipeline job.


If you trying to do it in the parameters script I'm not sure that possible.

Shmuel
  • 1,568
  • 10
  • 20
  • Those are environment variables, they are only available during build execution. I'm talking about accessing job properties from groovy script in project configurations, which runs in "Build with parameters" tab before you actually run the job itself. – MaroonedMind Sep 23 '19 at 14:25
0

I faced the same issue and came up with a solution. You can use Jenkins pipeline currentBuild variable, more about its properties - open page jenkins-server-url/jenkins/pipeline-syntax/globals on your Jenkins server

String currentJobParentFolderName = currentBuild.fullProjectName.split('/')[0]
String currentJobName = currentBuild.projectName
String paramValue = getParamValue(currentJobName)

properties([
        buildDiscarder(logRotator(daysToKeepStr: '10')),
        disableResume(),
        parameters([
                [$class: 'CascadeChoiceParameter',
                 name: 'PARAM',
                 choiceType: 'PT_SINGLE_SELECT',
                 description: 'param1',
                 filterLength: 1,
                 filterable: false,
                 randomName: 'choice-parameter-99999999999',
                 script: [
                         $class: 'GroovyScript',
                         fallbackScript: [
                                 classpath: [],
                                 sandbox: false,
                                 script:
                                         'return ["Failed to get values"]'
                         ],
                         script: [
                                 classpath: [],
                                 sandbox: false,
                                 script:
                                         """return ['$paramValue']"""
                         ]
                 ]
                ]
        ])
])

timestamps {
    job's main logic
}

private static def getParamValue(String jobName) {
    Map paramValMapping = [
            'jobName1': 'value1', 
            'jobName2': 'value2',  
    ]
    String paramValue = paramValMapping.get(jobName)
    if (!paramValue) {
        throw new Exception("Failed to get value")
    }
    return paramValue
}

currentBuild.fullProjectName - job name including upper level folders (I needed exactly this) currentBuild.projectName - just a job name

Unfortunately I didn't manage to place all this logic inside of CascadeChoiceParameter script section. Also I needed only one value, but this approach can be used for a list of values as well, just don't forget about quotes for string values.

Pay attention that such script changes may require script approve from Jenkins admin in jenkins/scriptApproval for EACH incoming value paramValue

smoke_lp
  • 113
  • 1
  • 8
0

So for everyone who's still looking for a solution to this problem, there are actually three ways you could go about solving this.

  1. The reason why the jenkinsProject variable is not available in Pipeline jobs is due to an issue with the active choices plugin itself. You can read more about this in the following ticket: https://issues.jenkins.io/browse/JENKINS-29407. There's a separate feature branch in the Active Choices project on GitHub that (kinda) solves this issue: https://github.com/jenkinsci/active-choices-plugin/tree/pipeline-support. It wasn't merged into master (and it looks like it never will be), because adding this to Pipeline jobs breaks this functionality for the Freestyle jobs. What you can do is you can compile the plugin from the source yourself and install it on your Jenkins. Pipeline jobs will have the binding variables available in their active choice parameters, however Freestyle jobs will no longer have them. Only use this if the following two options are for some reason not possible in your case.
  2. You can use the properties{} step to configure job parameters from the pipeline run. During pipeline run you could simply use the JOB_BASE_NAME environment variable as an expression of the GString that you'd pass as a script text. The only disadvantage here is that the parameters will only become available after the first build. So if you have a new job you'd need to trigger it once before it becomes parameterized.
  3. You could just use the input() step. Your job won't be parametrized, but users will be able to provide their input to the build during its run. It's not very convenient if you need it to be triggered by some other job or an external webhook, but for cases where the job is expected to only be triggered manually it's probably the best option.
MaroonedMind
  • 141
  • 3
  • 7