Search code examples
jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovy

Jenkins Active Choice Reactive Reference Parameter Formatted HTML in MultiBranch pipeline job get current branch name in script


I am trying for a lot of time to get the current branch name in MultibranchPipeline Job inside an Active Choice Reactive Reference Parameter Formatted HTML parameter script block

[
    $class: 'DynamicReferenceParameter',
    choiceType: 'ET_FORMATTED_HTML',
    name: 'TestParam',
    omitValueField: true,
    description: 'Test.',
    script: [
        $class: 'GroovyScript',
        fallbackScript: [
            classpath: [],
            sandbox: false,
            script: '''
                return """
                    <p>FallbackScript. Error in main script</p>
                """
            '''
        ], 
        script: [
            classpath: [],
            sandbox: false,
            script: '''
                String branchName = env.BRANCH_NAME
                return """
                    <p>${branchName}</p>
                """
            '''
        ]
    ]
]

The thing is that, I believe, the BRANCH_NAME param is injected after you press the Build button.

I've tried a lot of things, and I mean, A LOT, still I didn't manage to find a way. The scm variable doesn't exist as well, I tried to find something with the jenkins.model.Jenkins.instance but no luck.

Is it possible? I would love to ask this question on their Github repo, but issues are not allowed to be opened. Also to open an issue on Jenkins you need a Jira account or something. SO is the only place.


Solution

  • Have a look at Groovy's string interpolation.

    tl;dr You can access values by using """ and ${variable}

    script: """
        return <p>${env.BRANCH_NAME}</p>
    """