Asked  3 Months ago    Answers:  5   Viewed   421 times

I'm using the Active Choices Reactive Reference Parameter plugin in a dsl job here the code

 parameters {
                  activeChoiceParam('choice1') {
                      description('select your choice')
                      choiceType('RADIO')
                      groovyScript {
                          script("return['aaa','bbb']")
                          fallbackScript('return ["error"]')
                      }
                  }
                  activeChoiceReactiveParam('choice2') {
                      description('select your choice')
                      choiceType('RADIO')
                      groovyScript {
                          script("if(choice1.equals("aaa")){return ['a', 'b']} else {return ['aaaaaa','fffffff']}")
                          fallbackScript('return ["error"]')
                      }
                      referencedParameter('choice1')
                  }

and it work's fine what i want now is how to use the activeChoiceReactiveParam in a jenkinsFile for pipeline job i did that :

properties(
    [
            [
                    $class              : 'ParametersDefinitionProperty',
                    parameterDefinitions: [
                            [
                                    $class     : 'ChoiceParameterDefinition',
                                    choices    : 'aaanbbb',
                                    description: 'select your choice : ',
                                    name       : 'choice1'
                            ]

but how can i add the choice2 parameter !!!

 Answers

69

Instead of using the Active Choices Reactive Reference Parameter i did below and it's working fine !!!

node('slave') {
    def choice1
    def choice2

    stage ('Select'){
        choice1 = input( id: 'userInput', message: 'Select your choice', parameters: [ [$class: 'ChoiceParameterDefinition', choices: 'aanbb', description: '', name: ''] ])
        if(choice1.equals("aa")){
            choice2 = input( id: 'userInput', message: 'Select your choice', parameters: [ [$class: 'ChoiceParameterDefinition', choices: 'yynww', description: '', name: ''] ])
        }else{
            choice2 = input( id: 'userInput', message: 'Select your choice', parameters: [ [$class: 'ChoiceParameterDefinition', choices: 'ggnkk', description: '', name: ''] ])
        }
    }
}
Thursday, July 15, 2021
 
Claudio
answered 3 Months ago
73

TL;DR

  • variables defined with def in the main script body cannot be accessed from other methods.
  • variables defined without def can be accessed directly by any method even from different scripts. It's a bad practice.
  • variables defined with def and @Field annotation can be accessed directly from methods defined in the same script.

Explanation

When groovy compiles that script it actually moves everything to a class that roughly looks something like this

class Script1 {
    def pr() {
        def another_var = "another " + some_var
        echo "${another_var}"
    }
    def run() {
        def some_var = "some value"
        pipeline {
            agent any
            stages {
                stage ("Run") {
                    steps {
                        pr()
                    }
                }
            }
        }
    }
}

You can see that some_var is clearly out of scope for pr() becuse it's a local variable in a different method.

When you define a variable without def you actually put that variable into a Binding of the script (so-called binding variables). So when groovy executes pr() method firstly it tries to find a local variable with a name some_var and if it doesn't exist it then tries to find that variable in a Binding (which exists because you defined it without def).

Binding variables are considered bad practice because if you load multiple scripts (load step) binding variables will be accessible in all those scripts because Jenkins shares the same Binding for all scripts. A much better alternative is to use @Field annotation. This way you can make a variable accessible in all methods inside one script without exposing it to other scripts.

import groovy.transform.Field

@Field 
def some_var = "some value"

def pr() {
    def another_var = "another " + some_var
    echo "${another_var}"
}
//your pipeline

When groovy compiles this script into a class it will look something like this

class Script1 {
    def some_var = "some value"

    def pr() {
        def another_var = "another " + some_var
        echo "${another_var}"
    }
    def run() {
        //your pipeline
    }
}
Wednesday, July 14, 2021
 
phpmeh
answered 3 Months ago
50

========I made it====== here is the code.

 parameters([
                    choice(name:"NeedUpgradePC",choices:['yes','no'],description: "Do you need upgrade your PC"),
                    [$class: 'DynamicReferenceParameter',
                            choiceType: 'ET_FORMATTED_HTML',
                            omitValueField: true,
                            description: 'Please provide a Elastic alias label',
                            name: 'PC_RAM',
                            randomName: 'choice-parameter-5631314456178624',
                            referencedParameters: 'NeedUpgradePC',
                            script: [
                                    $class: 'GroovyScript',
                                    fallbackScript: [
                                            classpath: [],
                                            sandbox: true,
                                            script:
                                                    'return['nothing.....']'
                                    ],
                                    script: [
                                            classpath: [],
                                            sandbox: true,
                                            script:
                                                    """
                                    if(NeedUpgradePC.equals('yes')) {
                                        inputBox="<input name='value' type='text' value='Kingston 8GB'>"
                                    } else {
                                        inputBox="<input name='value' type='text' value='Kingston 8GB' disabled>"
                                    }
                                """
                                    ]
                            ]
                         ],
                            [$class: 'DynamicReferenceParameter',
                                    choiceType: 'ET_FORMATTED_HTML',
                                    omitValueField: true,
                                    description: 'Please provide a Elastic alias label',
                                    name: 'PC_CPU',
                                    randomName: 'choice-parameter-5631314456178624',
                                    referencedParameters: 'NeedUpgradePC',
                                    script: [
                                            $class: 'GroovyScript',
                                            fallbackScript: [
                                                    classpath: [],
                                                    sandbox: true,
                                                    script:
                                                            'return['nothing.....']'
                                            ],
                                            script: [
                                                    classpath: [],
                                                    sandbox: true,
                                                    script:
                                                            """
                                    if(NeedUpgradePC.equals('yes')) {
                                        inputBox="<input name='value' type='text' value='Intel Core i5'>"
                                    } else {
                                        inputBox="<input name='value' type='text' value='Intel Core i5' disabled>"
                                    }
                                """
                                            ]
                                    ]
                            ]

            ])
Friday, August 27, 2021
 
Martin Liesén
answered 2 Months ago
53

You could write the build number to a file in the upstream jobs and include that in the archive. Then you copy that artifact forward. You could use the pipeline utility steps plugin to gather information from the file, but if you keep the file simple (i.e. write nothing but the build number), then something like this would get you the build number:

def backendBuildId = sh script: "cat backendBuild.txt", returnStdout: true

or on Windows:

def backendBuildId = bat script: "type backendBuild.txt", returnStdout: true
Wednesday, September 1, 2021
 
rubenvb
answered 2 Months ago
78

It's supported with two-level security structure, example:

rol1 ".*holaArtifactoryMultibranch.*" --> read

rol2 ".*holaArtifactoryMultibranch/master.*" --> build

Now, you can associate a user/group to rol1 and rol2

UPDATE:

I see your point, if you have an intermediate folder:

rol1    "^FOLDER$"   --> read

rol2    "^FOLDER/holaArtifactoryMultibranch.*$"--> read

You'll see the holaArtifactoryMultibranch folder, but not others.

Friday, October 1, 2021
 
KHM
answered 2 Weeks ago
KHM
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :