I am using the jobDSL-plugin to allow myself and my team to use infrastructure-as-code principles for our Jenkins setup. We have a bunch of jobs defined in scripts but a large majority of them share some metadata, for example, almost all of them contain a list of available environments that our Dev team could deploy to e.g:
environments = [
'Development',
'Test',
'Production'
]
I would like to be able to define this list once and then import it into each jobDSL script so that we are not repeating ourselves 20+ times and have to modify 20+ lists every time there is a change. I was hoping I could use our jenkins-shared-library to do this by having @Library('our-shared-library') _
at the top of the file but when I run our seed job I get a unable to resolve class Library , unable to find class for annotation
error, so that seems like shared-libraries cannot be imported into jobDSL script files.
Is there any other way to have data or classes in a centralised location/repository that I can import into all my jobDSL script files?
I think the most ideal method for this would be to store the shared metadata in files within the repo that stores all our jobDSL files, but if they need to be stored somewhere else and imported, I'd be happy with that approach too.
EDIT1: For more information on what the jobDSL plugin is and how it enables IaC principles within Jenkins, you can look at this question as a taster. I have a large number of jobs and many of them use the same data, e.g. a list of environments that our teams can deploy to. I would like to define this list 1 time and then pull it into my pipelineJob definitions somehow. I attempted to import the our jenkins-shared-library using the @Library('ourLibrary')
annotation, but I don't think that is possible.
Here is the Github page for the plugin.
EDIT2: I am going to try and rephrase this question and just write it out better now that I've had my morning coffee. I am using the jobDSL plugin to define my Jenkins jobs in an IaC format. I now have over 20 jobs and some data is repeated across a number of jobs, for example, I have a number of jobs that require a list of environments as a parameter for a user to choose from. I would like to define this list once and only once, then import the list into the files that require the list.
Here is an example jobDSL script (this is what I have 20+ files of, with some differences)
def SERVICES = [
'Service-1',
'Service-2
]
def ENVIRONMENTS = [
'Development',
'Testing',
'Production'
]
for (SVC in SERVICES) {
SVC_NAME = SVC.replaceAll('-', ' ')
SVC_PATH = SVC.replaceAll('-', '_')
pipelineJob("DEPLOY/${SVC_NAME}") {
displayName("DEPLOY ${SVC_NAME.toUpperCase()}")
parameters {
choiceParam('ENVIRONMENT', ENVIRONMENTS, 'Select the environment to deploy to')
}
logRotator {
numToKeep(30)
}
definition {
cpsScm {
scm {
git {
remote {
...
}
}
scriptPath('deploy/Jenkinsfile_deploy_services.groovy')
}
}
}
}
Now I want to define the ENVIRONMENTS
list somewhere and then import it into all my 20+ files instead of defining it at the top of every file, making my repo a lot more clean and maintainable. How do I do this?