Brain
Brain

Reputation: 75

create dsl job inside a job

When I run the job I should create a freestyle job with a parameter name and repo.

I already try this but it doesn't work.

 freeStyleJob('seed') {
    parameters {
      stringParam("GITHUB_REPO_NAME", "", "repo_name")
      stringParam("JOB_NAME", "", "name for the job")
    }
    steps {
      dsl {
        job('\$DISPLAY_NAME') {

        }
      }
    }
  }

Upvotes: 0

Views: 1223

Answers (2)

Da2ny
Da2ny

Reputation: 1047

You can create a job inside a job by using 'text': https://jenkinsci.github.io/job-dsl-plugin/#path/freeStyleJob-steps-dsl-text

steps {
  dsl {
    text('job ("name") {}')
  }
}

Upvotes: 1

Sourav
Sourav

Reputation: 3402

Here is the script that will help you out. It will create a Freestyle Job named example with Build Parameters JOB_NAME and GITLAB_REPOSITORY_URL. This job will have Job DSL Scripts.

freeStyleJob('example') {
    logRotator(-1, 10)
    parameters{
        stringParam('JOB_NAME', '', 'Set the Gitlab repository URL')
        stringParam('GITLAB_REPOSITORY_URL', '', 'Set the Gitlab repository URL')
        }
    steps {
        dsl {
           external('**/project.groovy')
           }
        }
    }

In project.groovy, I have written my DSL scripts.

project.groovy

import hudson.model.*
def thr = Thread.currentThread()
def build = thr?.executable

def jobname_param = "JOB_NAME"
def resolver = build.buildVariableResolver
def jobname = 'TEST/'+ resolver.resolve(jobname_param)
println "found jobname: '${jobname}'"

def project_url_param = "GITLAB_REPOSITORY_URL"
def resolver2 = build.buildVariableResolver
def project_url = resolver2.resolve(project_url_param)
println "found project_url: '${project_url}'"

def repoUrl = "https://example.com/gitlab/repoA/jenkinsfile-repo.git"

    pipelineJob(jobname) {
        parameters {
               stringParam('GITLAB_REPOSITORY_URL', '', 'Set the Gitlab repository URL')
           }
        
     logRotator {
           numToKeep(5)
           daysToKeep(5)
      }

        definition {
            cpsScm {
                scm {
                git {
                        remote {
                          url(repoUrl)
                          credentials('gitlab-test')
                    }
                     branches('master')
                     extensions {
                       cleanBeforeCheckout()
                    }
                      }
                    }
                    scriptPath("Jenkinsfile")
                  }
            }
        }

For more reference:

Upvotes: 0

Related Questions