首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jenkins Job DSL:如何从文件中读取管道DSL脚本?

Jenkins Job DSL:如何从文件中读取管道DSL脚本?
EN

Stack Overflow用户
提问于 2017-05-16 18:55:33
回答 2查看 2.6K关注 0票数 2

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

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

代码语言:javascript
复制
definition {
   cps {
      sandbox()
         script( new File('Pipeline.groovy').text )
      }
   }
}

我必须将Pipeline.groovy放在哪里才能正常工作?我试着把它放在我的DSL脚本旁边,也放在我的DSL源代码的resources/文件夹中。但是Jenkins总是抛出一个"file not found“。

EN

回答 2

Stack Overflow用户

发布于 2017-08-16 01:43:48

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

票数 1
EN

Stack Overflow用户

发布于 2017-08-29 10:52:20

引用Job DSL pipelineJob:https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob,并在http://job-dsl.herokuapp.com/上对其进行修改以查看生成的配置。

下面的job DSL创建了一个管道作业,它从Jenkinsfile中拉出实际的作业:

代码语言:javascript
复制
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作业并不是一个坏主意。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43999508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档