2

I'm trying to convert old Jenkins jobs to declarative pipeline code.

When trying to use the choice parameter in the script I implement a function which should return updated values, if the values are not the most recent ones - the job will fail.

The problem is that after the first build which looks ok, the values stay static, they don't get updated afterwards which as I said above - fails my job. It's like the function that i wrote runs only one time at the first build and doesn't run ever again.

I've tried writing the code in a way that the output will be sent to a file and be read from it - thus maybe the function will get updated by getting the text from a file - that didn't work.

I've tried looking at the Jenkins documentation / a lot of other threads and didn't find a thing.

My code looks like this:

def GetNames() {
    def workspace = "..."
    def proc = "${workspace}/script.sh list".execute()
    return proc.text
}

${workspace} - Is just my workspace, doesn't matter.

script.sh - A script that 100% works and tested

return proc.text - Does return the values, I've tested it in my Jenkins website/script section and the values do return properly and updated.

My parameters section:

parameters {
    choice(name: 'Names', choices: GetNames(), description: 'The names')
}

First build I get 5 names, which is good because those are the updated values, seconds build I know there are 10 values but I still get the 5 from before, and every build after that I will still get the same 5 names - they do not get updated at all, the function does not get triggered again.

It seems like this is a very long running issue which still didn't get patched, the only thread that had this mentioned was this one: Jenkins dynamic declarative pipeline parameters but the solution is in a scripted and not declarative way.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Zvika Ravet
  • 41
  • 1
  • 5
  • Have you checked this https://stackoverflow.com/questions/44518963/jenkins-declarative-pipeline-with-extended-choice-parameter?rq=1 – Akshay barahate Apr 29 '19 at 07:25
  • @Akshaybarahate yeah I did, but the solution is not in a declarative pipeline way, which means it's not written in code - meaning that you manually need to add the parameter section and the groovy script. – Zvika Ravet Apr 29 '19 at 07:39
  • One option to do it as-code is JobDSL https://github.com/jenkinsci/job-dsl-plugin - but that would be outside the declarative pipeline scope – fishi0x01 Apr 29 '19 at 08:10

1 Answers1

2

Well, i've finally figured it out, the solution is combining declarative and scripted ways,

(using active parameter plugin).

node {
    properties([
        parameters([
            [$class: 'ChoiceParameter', 
                choiceType: 'PT_SINGLE_SELECT', 
                description: 'The names', 
                filterLength: 1, 
                filterable: true, 
                name: 'Name', 
                randomName: 'choice-parameter-5631314439613978', 
                script: [
                    $class: 'GroovyScript', 
                    script: [
                        classpath: [], 
                        sandbox: false, 
                        script: '''
                                    some code.....
                                    return something'''
                    ]
                ]
            ], 
        ])
    ])
}

pipeline {
    agent any
.
.

This way the script part of the active parameter initiates every time you load the page and the values get returned updated every time.

Zvika Ravet
  • 41
  • 1
  • 5
  • 1
    Could you elaborate further on your solution? I am in a similar situation to you where I want to populate a parameter dynamically for a declarative pipeline where I want to obtain git tags (or possibly run a shell script which does the same) like your original question. I wasn't able to get this active parameter plugin solution to work – Brendan Jul 22 '19 at 22:51