How do pipeline parameters and jenkins GUI parameters work together?

后端 未结 2 1214
我寻月下人不归
我寻月下人不归 2021-02-03 12:41

I am using a pipeline in a jenkinsfile and I am not sure how to properly link the job in Jenkins and the pipeline.

I defined parameters in my jenkinsfile (some with def

相关标签:
2条回答
  • 2021-02-03 12:55

    This problem can be solved by adding a special parameter to avoid default values being override. Here is the sample code.

    if (!params.__INIT__) { // this value will be true once parameters get initialized
        properties([ parameters([
                string(name: 'SOURCE_URL', trim: true),
                string(name: 'TARGET_URL', trim: true),
    
                string(name: 'DEPLOY_URL', trim: true),
                credentials(name: 'DEPLOY_KEY', required: true ),
    
                string(name: 'BUILD_NODE', trim: true),
    
                booleanParam(name: '__INIT__', defaultValue: true, description: "Uncheck this field in configuration page and run this job once if you want to re init parameter."),
        ]) ])
        return // exit 
    }
    
    // Your task stars from here
    
    

    By doing this way, the parameters won't be initialized again next time because __INIT__ has been set to true. On the other hand, if you update your script and change some parameters, you can just run the job by unchecking the __INIT__ field.

    0 讨论(0)
  • 2021-02-03 12:56

    Ah, yes, it got me the first time around also.

    The first time that you run the pipeline, the jenkinsFile DSL job definition pretty much overrides the whole job definition that you have entered thru the GUI. That affects the parameters in particular.

    So make sure you define the parameters exactly how you want them in the Jenkinsfile, then run the job once, and the GUI will have the same config for the parameters, so that when you run again, it will ask for the parameters and use the default values, that you specified in the DSL. There is nothing more to it.

    Yes, having to run twice everytime you modify the parameters in the DSL is annoying. But it sort of makes sense if you consider that the job has to execute for the DSL to be evaluated, but first it needs to ask for parameters if you have some defined via the UI, BEFORE it checks out and evaluates the DSL...

    0 讨论(0)
提交回复
热议问题