Hi,
First of all, I have to admit, I just started using jenkins pipelines and job dsl. We were using Jenkins 1.6 up till now and used some small C# tools to create jenkins jobs on an xml basis.
Now I want to use latest version of jenkins and make use of pipelines and job dsl to fully automate the process of job creation and have all the configuration living in the repo.
I'm trying to fully automate the creation of jobs based on the contents of a repository (the repository contains several .sln files (Visual Studio solutions)).
For that reason, I manually create one GitHub Organization job in jenkins. Within each branch of each repository, there is a "Jenkinsfile" using declerative pipeline syntax.
The jenkinsfile is very small:
#!/usr/bin/env groovy
dotNetStandardPipeline {
message = "Hello World!"
}
The dotNetStandardPipeline lives in another repository where I want to put all the shared libraries code. the content of that pipeline file looks as follows:
#!/usr/bin/env groovy
import javaposse.jobdsl.dsl.Job
import breuer.jenkins.utils.JobUtils
def call(body) {
// evaluate the body block, and collect configuration into the object
def pipelineParams= [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = pipelineParams
body()
pipeline {
agent any
stages {
stage('Scan for new jobs') {
steps {
echo 'Scanning...'
script {
def solutions = findFiles glob: '**/*.sln'
echo "Solution count: ${solutions.size()}"
solutions.each {
JobUtils.addDefaults(currentJob)
currentJob.With {
shell('echo Hello World!')
}
}
}
}
}
stage('Build jobs') {
steps {
echo pipelineParams.message
}
}
}
}
}
The important part is:
JobUtils.addDefaults(currentJob)
currentJob.With {
shell('echo Hello World!')
}
JobUtils again is another utility class that performs certain configurations with the job.
Now when executing this, jenkins complains:
java.lang.NoSuchMethodError: No such DSL method 'job' found among steps
when using freestyleJob instead of job, it will complain that:
Cannot cast object '@freeStyleJob(<anonymous>=HelloWorld.sln)' with class 'org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable' to class 'javaposse.jobdsl.dsl.Job'
When searching for a solution, I found that it seems that job-dsl and pipeline jobs are somewhat incompatible. I've found a hint on how to mix them here: https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts
but this doesn't seem to allow for usage of utility methods to extend the jobs behavior.
Am I missing something here? Is there any other way to fully automate the job creation using only the pipeline job created by the jenkinsfile?
Thank you very much in advance for any assistance here. I'm somewhat lost currently.
Best regards,
Tobias