9
votes

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 default values, some without) and have them initialized from the parameters coming from jenkins gui. The problem is that it seams the parameters in my pipeline are overiding my job parameters, EVEN when no default value is specified in the pipeline which means the pipeline is overriding my job setup in jenkins.

for example, one of my job is setup to run the pipeline with some specific values (all NON-EMPTY), if I trigger the job, the pipeline seem to reset the properties to '' for fields b and c.

How to I get the pipeline to not touch my jenkins job definition?

e.g. the params in the pipeline:

    properties([
      parameters([
        string(name: 'a',   defaultValue: 'Default A value', description: '', ),
        string(name: 'b',   description: '', ),
        string(name: 'c',   description: '', ),
       ])
])

I am not find any help in the documentation at https://jenkins.io/doc/book/pipeline/syntax/#parameters-example

2
I gave up on the pipelines for now, it has too many aspects undocumented, many designs such as params management are very hard to work with, create too much code duplication, etc. You'd think the added value of pipelines is testability, simplicity but since you cannot test or execute locally pipelines you end up spending much time clicking again and again in the interface, so there is not much added value compared to the traditional jobs (these appear more mature).NicolasW

2 Answers

12
votes

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
votes

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.