繁体   English   中英

Jenkins Job DSL:如何从文件中读取Pipeline DSL脚本?

[英]Jenkins Job DSL: How to read Pipeline DSL script from file?

我想通过Job DSL生成基于管道插件的作业,Job DSL包含在由Jenkins检出的git存储库中。

但是,我认为将管道脚本作为Job DSL脚本中的引用字符串并不是很好。 所以我想把它们读成一个字符串并将其传递给script()函数:

definition {
   cps {
      sandbox()
         script( new File('Pipeline.groovy').text )
      }
   }
}

我在哪里可以使用Pipeline.groovy来实现这个目的? 我尝试将它放在我的DSL脚本旁边,也放在DSL源的resources/文件夹中。 但詹金斯总是抛出一个“找不到档案”。

你试过readFileFromWorkspace()吗? 它应该能够找到你从git结账的文件。

参考Job DSL管道工作: https//jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob ,并在http://job-dsl.herokuapp.com/上查看生成的配置。

下面的Job DSL创建一个管道作业,它从Jenkinsfile中提取实际作业:

pipelineJob('DSL_Pipeline') {

  def repo = 'https://github.com/path/to/your/repo.git'

  triggers {
    scm('H/5 * * * *')
  }
  description("Pipeline for $repo")

  definition {
    cpsScm {
      scm {
        git {
          remote { url(repo) }
          branches('master', '**/feature*')
          scriptPath('misc/Jenkinsfile.v2')
          extensions { }  // required as otherwise it may try to tag the repo, which you may not want
        }

        // the single line below also works, but it
        // only covers the 'master' branch and may not give you
        // enough control.
        // git(repo, 'master', { node -> node / 'extensions' << '' } )
      }
    }
  }
}

您/您的Jenkins管理员可能希望将Jenkins作业创建与实际作业定义分开。 这对我来说似乎是明智的......集中脚本来在单个Jenkins DSL仓库中创建Jenkins作业并不是一个坏主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM