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.

1 answer

  • answered 2021-04-15 06:28 Michael Kemmerzell

    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>
    """