Using job-dsl in pipeline

742 views
Skip to first unread message

Tobias Breuer

unread,
Nov 6, 2017, 9:49:08 AM11/6/17
to job-dsl-plugin
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 {
        echo "Building job for ${it.name}"
        Job currentJob = job(it.name)
        JobUtils.addDefaults(currentJob)
        currentJob.With {
        shell('echo Hello World!')
        }
      }
      }
    }
    }
  
    stage('Build jobs') {
    steps {
      echo pipelineParams.message
      
    }
    }
  }
  }
}

The important part is:

Job currentJob = job(it.name)
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



Tobias Breuer

unread,
Nov 6, 2017, 10:50:26 AM11/6/17
to job-dsl-plugin
Maybe I'm one step further now (or maybe not...)
When assuming, that job-dsl is not directly compatible with pipeline I thought about the following approach now.

Based on whats written here, I've changed the pipeline to perform the following:

steps {
   echo 'Scanning...'
   echo "${pwd()}"
   jobDsl(additionalClasspath: 'src/breuer/jenkins/utils', removedJobAction: 'DELETE', removedViewAction: 'DELETE', targets: '../src/breuer/jenkins/utils/DotNetJob.groovy', unstableOnDeprecation: true)
}

now the problem is, the DotNetJob.groovy file is located in a different repository, than the Jenkinsfile which contains the pipeline definition. So pwd will print the workspace of the repository containing the Jenkinsfile which is what I need, as DotNetJob.groovy needs to scan this repo for .sln files. But how can I specify the DotNetJob.groovy script in the targets parameter of the jobDsl command when this is actually located in a different repo??

Ravitej Aluru

unread,
Nov 17, 2017, 4:28:04 AM11/17/17
to job-dsl-plugin
Hi Tobias,

You should be able to checkout both the repos in your Jenkinsfile and then cp (copy) all the required files into one directory and you can then use any DSL script file from both the repositories to create jobs. See this answer on stackoverflow: https://stackoverflow.com/questions/37395860/jenkinsfile-with-two-git-repositories

Another point to note is that the additionalClasspath that you use to import libraries works only when script security is turned off for DSL scripts (if you have Script Security plugin installed).

Hope this helps.
Ravitej
Reply all
Reply to author
Forward
0 new messages