create DSL pipeline job with dynamic source repo and jenkins script

2,380 views
Skip to first unread message

Sakthivel Subramanian

unread,
Oct 1, 2017, 10:54:57 AM10/1/17
to job-dsl-plugin
Hi,

I am trying to create Jenkins DSL pipeline job with dynamic source Repo and jenkins file to add that job.. I am trying this way...

Step 1: Using below script creating DSL job. Created job will use jenkins file which is mentioned in Step 2. Before using jekins file in the job.. i want to modify the dynamic URL which is commended in step1. But if enable it getting some groovy issue.



create_job.groovy
=================

def apps = [

   [project:"myproj",repo:"my-test-repo1"],
   [project:"testproj",repo:"my-test-repo2"],
   [project:"myproj",repo:"my-dev-repo"]
]


apps.each {
       app ->

       //mystashurl = 'mybaseurl+scm/'+app.project+'/' + app.repo + '.git'
       //sh("sed -i.bak -e 's#--GIT_URL--#${mystashurl}#' Jenkinsfile")

       pipelineJob('LIB.' + app.repo.replaceAll('-','')) {
       description('Containerized microservice lib config')


       definition {
        cps
        {
            script(readFileFromWorkspace('Jenkinsfile'))
            sandbox()
        }
    }


   }
}




Step 2:


Jenkinsfile
===========

node {

  checkout scm --GIT_URL--

  // load build functions into a subdirectory
  checkout([
    $class: 'GitSCM',
    branches: [[name: '*/master']],
    doGenerateSubmoduleConfigurations: false,
    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: '.jenkins']],
    submoduleCfg: [],
    userRemoteConfigs: [[
      credentialsId: 'jenkins',
      url: 'mybaseurl/mylibrepo.git'
    ]]
  ])

  // load pipelinejob
  pom = readMavenPom file: 'pom.xml'
  pipeline = load ".jenkins/library/Jenkinsfile"
  pipeline.run(pom)
}




Is there any way to create this job or modify the jenkins file before creating DSL Job.


Thanks in advance

Sakthi

Victor Martinez

unread,
Oct 4, 2017, 5:39:35 PM10/4/17
to job-dsl-plugin
Hey,

As far as I see  the sh statement is not a JobDSL allowed command.

You could read the file outside of the pipelineJob closure and manipulate it:

So, something like the below untested snippet might work:

...

apps.each {  app ->

  mystashurl = 'mybaseurl+scm/'+app.project+'/' + app.repo + '.git'
  def content = readFileFromWorkspace('Jenkinsfile')
    content.replaceAll('--GIT_URL--',mystashurl)
   pipelineJob('LIB.' + app.repo.replaceAll('-','')) {
     description('Containerized microservice lib config')
     definition {
        cps   {
           script(content)
           sandbox()
        }
     }
  }
}


Cheers
Reply all
Reply to author
Forward
0 new messages