Hi,

I have to manage a bunch of jobs/pipelines defined as code which they have 
a list parameter choice populated from its a reactiveChoice parameter 
Groovy script.

That reactiveChoice parameter Groovy script consists in a Jenkins secret 
query passed to a custom SDK (custom JAR loaded in the classpath) and it 
queries data from my inventory APIs. Then that data populates a list 
variable returned at the end.

It works but I'm trying to organize this by not repeating all the script. 
And I tried a lot of things to load a shared library 
(var/customFunction.groovy) into that Groovy environment but it won't work.

Here is the DSL:

pipelineJob("APP/GET_LOGS") {

    displayName("Get logs")
    description("""Job to retrieve logs""")

    configure { project ->
        project / 'properties' / 
'org.jenkinsci.plugins.workflow.job.properties.DurabilityHintJobProperty' {
            hint('PERFORMANCE_OPTIMIZED')
        }
    }

    parameters {
        reactiveChoice {
            name('service_name')
            description('Service target')
            filterable(false)
            randomName('')
            filterLength(0)
            choiceType('PT_SINGLE_SELECT')
            referencedParameters('app')
            script {
                groovyScript {
                    script {
                        script("""
                            import jenkins.model.Jenkins
                            import 
com.cloudbees.plugins.credentials.CredentialsProvider
                            import 
com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials
                            import 
com.cloudbees.plugins.credentials.domains.DomainRequirement
                            import groovy.json.JsonSlurper
                            import com.myorg.sdk.*

                            def getCredentialsById(String credentialsId) {
                                def jenkins = Jenkins.instance
                                def creds = null

                                for (domainCredentials in 
CredentialsProvider.lookupCredentials(
                                        
StandardUsernamePasswordCredentials.class,
                                        jenkins,
                                        null,
                                        new 
LinkedList<DomainRequirement>())) {
                                    if (domainCredentials.id == 
credentialsId) {
                                        creds = domainCredentials
                                        break
                                    }
                                }
                                return creds
                            }

                            def SDKInit(creds, timeout) {
                                String username = creds.username
                                String password = 
creds.password.getPlainText()

                                def sdkInstance = new SDK(username, 
password, timeout)
                                return sdkInstance
                            }

                            def inventoryQuery(sdk, queryTag) {
                                def result = 
sdk.inventory.getTagServices(queryTag, 300)
                                return result
                            }

                            def formatList(content) {
                                def jsonSlurper = new JsonSlurper()
                                def jsonParse = 
jsonSlurper.parseText(content)

                                def output = jsonParse.results.collect { 
result ->
                                    result.tenants_list.collect { tenant -> 
tenant.dns_name }
                                }.flatten()
                                return output
                            }

                            def Creds = getCredentialsById('prod-auth')
                            def SDK = SDKInit(Creds, 30)
                            def query = inventoryQuery(SDK, "app")
                            def list = formatList(query)
                            return list.sort()
                        """)
                        sandbox(false)
                        classpath{
                            classpathEntry {
                                path('/usr/share/jenkins/ref/lib/sdk.jar')
                                shouldBeApproved(true)
                            }
                        }
                    }
                    fallbackScript {
                        script("""
                            return ['ERROR']
                        """)
                        sandbox(true)
                    }
                }
            }
        }

        reactiveChoice {
            name('log_file')
            description('Name of log file to get')
            filterable(false)
            randomName('')
            filterLength(0)
            choiceType('PT_SINGLE_SELECT')
            referencedParameters('app')
            script {
                groovyScript {
                    script {
                        script("return ['logfile.out': 'General log']")
                        sandbox(true)
                    }
                    fallbackScript {
                        script("return ['Unable to list logs']")
                        sandbox(true)
                    }
                }
            }
        }

        choiceParam("number_lines", ['100', '1000', '10000', '50000'])
    }

    definition {
        cps {
            script('''
                library 'myorg'

                pipeline {
                    agent {label 'agent:latest'}

                    stages {
                        stage('Set build info') {
                            steps {
                                script {
                                    buildName "#${BUILD_NUMBER} - 
${service_name}"
                                }
                            }
                        }

                        stage('Bootstraping node') {
                            steps {
                                ansibleBootstrap()
                            }
                        }

                        stage('Execute Role') {
                            steps {
                                dir('ansible-playbooks') {
                                    sh \'''
                                        ansible-playbook -i inventory.yml 
get-log.yml
\'''
                                }

                            }
                        }
                    }
                }

            '''.stripIndent())
        sandbox()
        }
    }
}

Do you know any way to achieve that?

Thank you so much, regards.

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/50934ea3-5893-4775-9beb-cc8063d7b820n%40googlegroups.com.

Reply via email to