Jenkins Pipelines and their dirty secrets 2.

Richard Lenkovits on July 24, 2017

Solving parameterizing problems with Pipelines: In the following section, I will go through several Jenkins workflow handling examples w... [Read Full]
markdown guide
 

I found another way to prevent jenkins from overriding parameter values with each run. Seems to be working with latest declarative pipelines:

string(name: 'BRANCH', defaultValue: params.BRANCH ?: 'master')

If no param yet then it will use master, otherwise it will use whatever has been set in the defaults "manually" in the job configuration.

 

Thanks a lot, Crazily This info is not available any where.

 

I was new to Jenkisfiles. I'm using scripted pipeline and this posted helped me a lot, so thanks.

There is still an issue I'm trying to work around and had no luck so far.

As you stated "when you can't set your parameters yet, your job basically just starts running with its default parameters. This can be bad, as you may not want that."

I've used you're trick to avoid this but tbh in my case it doesn't make much of a diference because they will still be set the second time... and on a 3rd run it will still have the parameters from the second run...

In my case this is important because I'm trying to run 3 diferent tools that share some parameters but differ on others...

Using input and a switch case I can add those in later... what I do is to set the parameters they all share in an initParams var

so I do:

props = []
initParams = [
    choice(
        description: "Please specify which tool to run.",
        name: "tool",
        choices: "tool1\ntool2\ntool3"
    )
]

tool1Params = [ .... ]

switch (params.tool) {
    case "tool1":
        extraParams = input( id: "extraParams",
            message: "Tool 1 was selected,  please provide extra params:",
            parameters: tool1params
        )
        initParams +=  tool1params
        break
   .....
}


props.add(parameters(initParams))
properties(props)

with this in the first run, I sue "build now" and now "Build with parameters is available" showing only the initParams and moving to a input asking for the rest...

The problem is if I go to "Parameters" it still tells me I've only used the intParams... and I want to be able to see all there...

If I don't to the workaround it will show all args there.... but if I run it again for "tool2" it will list always the parameters used in the first run...

Sadly jenkins docs aren't the best... specially for Jenkisfiles and scripted pipeline so maybe you know another trick to accomplish this or tell me I'm doing something very wrong :P

 

Replying to myself... from what I was able to google "properpties" can only be used once in the Jenkinsfile and it also overwrites whatever was in the UI (if we are using it which is not my case)

So what I want is pretty much impossible to do.

All I can do is have the initial args in the properties then use input for the rest (as I do, but not attempt to set them in properties(parameters(...)) as this would simple configure the job to use whatever my first choice is.

Would be great if we could save the parms for "input" too as this offers easy visibility, but it seems it is not the case.

 

Hi, Is there a way, through with i can pass a value to the job and use it in the job ? I want to send the branch name from pipeline, which can be used in the job to build , is it possible?

the branch name should be available with the workspace env env.BRANCH_NAME

I have a few jobs where I use it to only allow triggered jobs if the branch is master.

if (env.BRANCH_NAME == 'master')
  props.add(pipelineTriggers([
                parameterizedCron('''
                H 19 * * * %param=value
            ''')]))

properties(props)

Hi, I believe that BRANCH_NAME is only populated when running a multi-branch pipeline. FYI then.
thanks
Gustavo.

 

Dear Mr. Lenkovits,

I read these communications from 2 years ago
issues.jenkins-ci.org/browse/JENKI...
(describing example need but closed as a duplicate of
issues.jenkins-ci.org/browse/JENKI...
where this feature was planned but its implementation had never progressed)

Also I read SO communications like this : stackoverflow.com/questions/443979...
and conclude that I am not alone with this need (to pass all parameters from top-level job to down-level when calling them through 'build' function)

And apparently Jenkins still does not allow to do this in short and easy way.
Do you have your own ideas on how to resolve this problem ?
I am starting to lose my hope a bit.
Had to glue my separate jobs into one monster-job with parts controlled by Boolean parameters.
Would appreciate your useful response. Thank you in advance.

 

Hi,
To begin with, if I was supposed to solve such a problem I'd definitely try to figure out if I could eliminate having too many parameters for a jenkins job. This is not the best possible design, and it can be eliminated with modularized thinking and good configuration management.

To answer the question, I don't think Jenkins has a simple solution for this. I would solve the issue somehow probably with using config files, or property files to pass many values. That could be done using pipeline-utility-steps plugin, see here: github.com/jenkinsci/pipeline-util...

 

I was hopeing for a strategy to make it easy to re-run a parameterized job, but to have the last values populated in the defaults, but obviously only when run interactively. Is there a mechanic for doing this using "storage" that get persisted across runs, can I store data by pushing stuff into the properties/props collection? Like at the end of a job I could write these "last used" values back into the job?

 

Hey what about if I want to use the user input during pipeline job, but instead of pre set choices, I want that choice be filled in a drop box like the extensible choice parameter with some files in Workspace folder. I actually can do this but before the job starts like I show you in images. But what if I want that in the workspace folder, I can choose those files during pipeline in step 2 o 3 for example after code is checkout and have the newest SQL files ? Is this possible? Thanks man. (edit) hmmm images are not loading

 
 
code of conduct - report abuse