How to access parameters in a Parameterized Build?

前端 未结 10 2081
你的背包
你的背包 2020-11-29 04:44

How do you access parameters set in the \"This build is parameterized\" section of a \"Workflow\" Jenkins job?

TEST CASE

10条回答
  •  北海茫月
    2020-11-29 05:31

    I tried a few of the solutions from this thread. It seemed to work, but my values were always true and I also encountered the following issue: JENKINS-40235

    I managed to use parameters in groovy jenkinsfile using the following syntax: params.myVariable

    Here's a working example:

    Solution

    print 'DEBUG: parameter isFoo = ' + params.isFoo
    print "DEBUG: parameter isFoo = ${params.isFoo}"
    

    A more detailed (and working) example:

    node() {
       // adds job parameters within jenkinsfile
       properties([
         parameters([
           booleanParam(
             defaultValue: false,
             description: 'isFoo should be false',
             name: 'isFoo'
           ),
           booleanParam(
             defaultValue: true,
             description: 'isBar should be true',
             name: 'isBar'
           ),
         ])
       ])
    
       // test the false value
       print 'DEBUG: parameter isFoo = ' + params.isFoo
       print "DEBUG: parameter isFoo = ${params.isFoo}"
       sh "echo sh isFoo is ${params.isFoo}"
       if (params.isFoo) { print "THIS SHOULD NOT DISPLAY" }
    
       // test the true value
       print 'DEBUG: parameter isBar = ' + params.isBar
       print "DEBUG: parameter isBar = ${params.isBar}"
       sh "echo sh isBar is ${params.isBar}"
       if (params.isBar) { print "this should display" }
    }
    

    Output

    [Pipeline] {
    [Pipeline] properties
    WARNING: The properties step will remove all JobPropertys currently configured in this job, either from the UI or from an earlier properties step.
    This includes configuration for discarding old builds, parameters, concurrent builds and build triggers.
    WARNING: Removing existing job property 'This project is parameterized'
    WARNING: Removing existing job property 'Build triggers'
    [Pipeline] echo
    DEBUG: parameter isFoo = false
    [Pipeline] echo
    DEBUG: parameter isFoo = false
    [Pipeline] sh
    [wegotrade-test-job] Running shell script
    + echo sh isFoo is false
    sh isFoo is false
    [Pipeline] echo
    DEBUG: parameter isBar = true
    [Pipeline] echo
    DEBUG: parameter isBar = true
    [Pipeline] sh
    [wegotrade-test-job] Running shell script
    + echo sh isBar is true
    sh isBar is true
    [Pipeline] echo
    this should display
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    

    I sent a Pull Request to update the misleading pipeline tutorial#build-parameters quote that says "they are accessible as Groovy variables of the same name.". ;)

    Edit: As Jesse Glick pointed out: Release notes go into more details

    You should also update the Pipeline Job Plugin to 2.7 or later, so that build parameters are defined as environment variables and thus accessible as if they were global Groovy variables.

提交回复
热议问题