jenkins - 多分支 Pipeline 插件每个分支加载多个 jenkinsfile

标签 jenkins jenkins-pipeline

我可以通过多分支管道插件自动加载 Jenkinsfile,但每个分支只能加载一个 jenkinsfile。

我想加载每个分支有多个 Jenkinsfile,我通过创建主 Jenkins 文件并加载特定文件尝试了以下方法。在下面的代码中,它将 1.Jenkinsfile2.Jenkinsfile 合并为一个管道。

node {
  git url: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d4dac7f3d1dac7d1c6d0d8d6c79ddcc1d4" rel="noreferrer noopener nofollow">[email protected]</a>:xxxxxxxxx/pipeline.git', branch: 'B1P1'
      sh "ls -latr"
          load '1.Jenkinsfile'
              load '2.Jenkinsfile'

}

有没有一种方法可以从一个分支单独加载多个 Jenkins 管道代码?

最佳答案

我编写了一个共享库(引用 https://jenkins.io/doc/book/pipeline/shared-libraries/ ),其中包含以下文件(在 vars/generateJobsForJenkinsfiles.groovy 中):

/**
 * Creates jenkins pipeline jobs from pipeline script files
 * @param gitRepoName name of github repo, e.g. <organisation>/<repository>
 * @param filepattern ant style pattern for pipeline script files for which we want to create jobs
 * @param jobPath closure of type (relativePathToPipelineScript -> jobPath) where jobPath is a string of formated as '<foldername>/../<jobname>' (i.e. jenkins job path)
 */
def call(String gitRepoName, String filepattern, def jobPath) {
    def pipelineJobs = []
    def base = env.WORKSPACE
    def pipelineFiles = new FileNameFinder().getFileNames(base, filepattern)

    for (pipelineFil in pipelineFiles) {

        def relativeScriptPath = (pipelineFil - base).substring(1)
        def _jobPath = jobPath(relativeScriptPath).split('/')
        def jobfolderpath = _jobPath[0..-2]
        def jobname = _jobPath[-1]

        echo "Create jenkins job ${jobfolderpath.join('/')}:${jobname} for $pipelineFil"

        def dslScript = []
        //create folders
        for (i=0;  i<jobfolderpath.size(); i++)
            dslScript << "folder('${jobfolderpath[0..i].join('/')}')"

        //create job
        dslScript << """
            pipelineJob('${jobfolderpath.join('/')}/${jobname}') {
                definition {
                    cpsScm {
                        scm {
                            git {
                                remote {
                                    github('$gitRepoName', 'https')
                                    credentials('github-credentials')
                                }
                                branch('master')
                            }
                        }
                        scriptPath("$relativeScriptPath")
                    }
                }
                configure { d ->
                    d / definition / lightweight(true)
                }
            }
            """
        pipelineJobs << dslScript.join('\n')
        //println dslScript
    }

    if (!pipelineJobs.empty)
        jobDsl sandbox: true, scriptText: pipelineJobs.join('\n'), removedJobAction: 'DELETE', removedViewAction: 'DELETE'

}

关于jenkins - 多分支 Pipeline 插件每个分支加载多个 jenkinsfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146952/

相关文章:

jenkins - 在完成当前正在运行的作业后,如何在 Jenkins UI 中禁用节点?

jenkins - Jenkins 中的 RTC 民意调查 scm

Jenkins 管道中的 Git 使用错误的 SSH 私钥推回 Git 存储库

docker - 无法从 Jenksinfile 中安装 ansible-galaxy 角色以构建和部署具有 (git)scm 引用角色的基础设施

Jenkins 中止并行阶段

jenkins - 如何获取jenkins管道文件中的文件?

jenkins - 我可以在Jenkins CI上加快Gradle守护进程的启动速度吗?

jenkins - 如何加快 Sonar 分析工作?

git - Docker Jenkins 和 Sonar 设置

docker - 从 Dockerfile 将配置文件插入到声明性 Jenkins 管道 Docker 容器中