在作业中创建 dsl 作业

create dsl job inside a job

当我 运行 作业时,我应该创建一个带有参数名称和 repo 的自由式作业。

我已经试过了,但没用。

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

        }
      }
    }
  }

这是可以帮助您解决问题的脚本。它将使用构建参数 JOB_NAMEGITLAB_REPOSITORY_URL 创建一个名为 example 的自由式作业。此作业将包含作业 DSL 脚本。

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')
           }
        }
    }

project.groovy,我写了我的DSL脚本。

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")
                  }
            }
        }

更多参考:

您可以使用 'text' 在作业中创建作业:https://jenkinsci.github.io/job-dsl-plugin/#path/freeStyleJob-steps-dsl-text

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