Using global shared libraries in Jenkins to define parameter options

Viewed 1271

I am trying to use a global class that I've defined in a shared library to help organise job parameters. It's not working, and I'm not even sure if it is possible.

My job looks something like this:

pipelineJob('My-Job') {
    definition {
        // Job definition goes here
    }
    parameters {
        choiceParam('awsAccount', awsAccount.ALL)
    }
}

In a file in /vars/awsAccount.groovy I have the following code:

class awsAccount implements Serializable {
    final String SANDPIT = "sandpit",
    final String DEV = "dev",
    final String PROD = "prod"

    static String[] ALL = [SANDPIT, DEV, PROD]
}

Global pipeline libraries are configured to load implicitly from the my repository's master branch.

When attempting to update the DSL scripts I receive the error:

ERROR: (myJob.groovy, line 67) No such property: awsAccount for class: javaposse.jobdsl.dsl.helpers.BuildParametersContext

Why does it not find the class, and is it even possible to use shared library classes like this in pipeline job?

1 Answers

Disclaimer: I know it works using Jenkinsfile. Unfortunatelly, not tested usng Declarative Pipelines - but no answers yet, so it may be worth a try

Regarding your first question: there are some reasons why a class from your shared-lib could not be found. Starting from the library import, the library syntax, etc. But they definitvely work for DSL. To be more precise about it, additional information would be great. But be sure that:

  • You have your groovy class definition using exactly the directory structure as described in the documentation (https://www.jenkins.io/doc/book/pipeline/shared-libraries/)
  • Give a name to the shared-lib in jenkins as you configure it and be sure is exactly the name you use in the import
  • Use the import as described in the documentation (under Using Libraries)

Regarding your second question (the one that names this SO question): yes, you can include parameter jobs from information in your shared-lib. At least, using Jenkinsfiles. You can even define properties to be included in the pipelie. I got it working with a tricky syntax due to different problems.

Again, I am using Jenkinsfile and this is what worked for me:

  1. In my shared-lib class, I added a static function that introduces the build parameters. Notice the input parameters that function needs and its usage:
class awsAccount implements Serializable {
  //
  static giveMeParameters (script) {
    return [
      // Some parms
      script.string(defaultValue: '', description: 'A default parameter', name: 'textParm'),
      script.booleanParam(defaultValue: false, description: 'If set to True, do whatever you need - otherwise, do not do it', name: 'boolOption'),
    ]
  }
}
  1. To introduce those parameters in the pipeline, you need to place the returned value of the function into the parameters array
properties (
  parameters (
    awsAccount.giveMeParameters (this)
  )

Again, notice the syntax when calling the function. Similar to this, you can also define functions in the shared-lib that return properties and use them in multiple jobs (disableConcurrentBuilds, buildDiscarder, etc)

Related