简体   繁体   English

来自 YAML 文件的 Jenkins 管道

[英]Jenkins pipeline from YAML file

Jenkins declarative pipeline is too powerful for us, often users can abuse it. Jenkins 声明式管道对我们来说太强大了,经常被用户滥用。 We are thinking to use an opinionated YAML to describe CI/CD pipeline.我们正在考虑使用一个自以为是的 YAML 来描述 CI/CD 管道。 And it seems there are two choices.似乎有两种选择。

  1. Write a plugin and consume YAML and dynamically create stage / steps.写一个插件消费 YAML 动态创建阶段/步骤。
  2. Write a plugin to convert a YAML to Jenkins pipeline.编写一个插件将 YAML 转换为 Jenkins 管道。

I am not expert on Jenkins, so I hope some expert can give some guidance and maybe an example.我不是Jenkins方面的专家,所以我希望高手能给一些指导,也许是一个例子。

  1. using official plugin pipeline-as-yaml , but it has a fixed grammar.使用官方插件pipeline-as-yaml ,但它有固定的语法。

  2. using or customization wolox-ci使用或定制wolox-ci

  3. create your own shared libaray.创建您自己的共享库。 However, they are easy from beginning but full grammer design is required when used widely.然而,它们从一开始就很容易,但在广泛使用时需要完整的语法设计。 Here is a psudo code based on curry .这是一个基于curry的伪代码。

// create a file named yamlCompiler.groovy in shared library,
def call(str){
  def rawMap = readYaml(text: str)
  // consume yaml and get a lambda function
  return {
    stage{
      steps.each{it ->
        it."$type"(it)
      }
    }
  }
}

Use yamlCompiler in your jenkinsfile code block.在您的 jenkinsfile 代码块中使用yamlCompiler

@Library('your libs name')
def str = 
'''
steps:
 - type: sh
   script: ls -la 
 - type: echo
   message: xxx
'''
Closure closure = yamlCompiler(str)
closure.call()

I'm looking for a similar solution.我正在寻找类似的解决方案。 We run hardened predefined pipelines for every project, but still want to allow dev teams to customise certain steps within the process —without allowing them the full power of a Jenkinsfile.我们为每个项目运行强化的预定义管道,但仍希望允许开发团队自定义流程中的某些步骤——但不允许他们使用 Jenkinsfile 的全部功能。

I'm also exploring the possibility of an —in your words— "opinionated YAML" .我也在探索——用你的话说—— “自以为是的 YAML”的可能性。

I've so far only found one example of such an implementation: Wolox-CI supports their own pre-defined build steps via YAML. You'll be able to see the steps they support here .到目前为止,我只找到了一个这样的实现示例:Wolox-CI 通过 YAML 支持他们自己的预定义构建步骤。您将能够在此处看到他们支持的步骤。

I'm thinking of parsing the YAML using Snake YAML. Here's an SO answer with an example on how to do it.我正在考虑使用 Snake YAML 解析 YAML。这是一个 SO 答案,其中包含有关如何执行此操作的示例。

Two solutions:两种解决方案:

If you're not an expert and don't want/have the time to become one, the second solution might be the best one.如果您不是专家并且不想/没有时间成为专家,那么第二种解决方案可能是最好的。

Jenkins declarative pipeline is too powerful for us, often users can abuse it. Jenkins 声明式管道对我们来说太强大了,经常被用户滥用。 We are thinking to use an opinionated YAML to describe CI/CD pipeline.我们正在考虑使用自以为是的 YAML 来描述 CI/CD 管道。 And it seems there are two choices.而且似乎有两种选择。

  1. Write a plugin and consume YAML and dynamically create stage / steps.编写一个插件并使用 YAML 并动态创建阶段/步骤。
  2. Write a plugin to convert a YAML to Jenkins pipeline.编写一个插件来将 YAML 转换为 Jenkins 管道。

I am not expert on Jenkins, so I hope some expert can give some guidance and maybe an example.我不是 Jenkins 方面的专家,所以我希望一些专家能给一些指导,也许可以举个例子。

Really?真的吗? Is the only difference here when the plugin is executed?:执行插件时唯一的区别是这里吗?:

  1. Write a plugin and consume YAML and dynamically create stage / steps.写一个插件消费 YAML 动态创建阶段/步骤。
  2. Write a plugin to convert a YAML to Jenkins pipeline.编写一个插件将 YAML 转换为 Jenkins 管道。

Forgive me, because I may be a little hardened, but abstracting a layer for the dynamic creation of a declarative, or scripted, Jenkinsfile written in the simple groovy lang syntax so that it can be pretty-printed in yml prevents users from updating your yml exactly how?原谅我,因为我可能有点顽固,但是为动态创建声明式或脚本化的 Jenkinsfile 抽象层,用简单的 groovy lang 语法编写,以便它可以漂亮地打印在 yml 中,防止用户更新你的 yml具体如何? It seems to me your abstraction only adds to the complexity with which you wish to implement usability.在我看来,您的抽象只会增加您希望实现可用性的复杂性。

One, all the current yml plugins for Jenkins do exactly that.第一,所有当前 Jenkins 的 yml 插件都是这样做的。 Two, they don't actually have the full breadth of "features" (yes, I'm using that term loosely here) accessible by implementing the groovy/(java) classes already available in the Jenkins domain (referencing the DSL).第二,它们实际上并没有通过实现 Jenkins 域(引用 DSL)中已经可用的 groovy/(java) 类来访问的全部“功能”(是的,我在这里松散地使用该术语)。 Two solutions exist right now for this, and I've investigated both, and implemented both, extensively.目前有两种解决方案,我已经调查并广泛实施了这两种解决方案。 One is wolox-ci, which is the better of the two, and the other is Pipeline-as-YAML.一个是 wolox-ci,这是两者中较好的一个,另一个是 Pipeline-as-YAML。 In my opinion, it's easy to use, but both lack the full breadth of implementation features simply using groovy provides.在我看来,它很容易使用,但两者都缺乏仅使用 groovy 提供的全部实现功能。 So why force it?那为什么要强制呢? Simply so your users can have a pretty-printed yml file, and not have to be concerned with simple syntax, which you claim hardens your infrastructure-as-code backend so that the same users can't screw it up?只是为了让您的用户可以拥有一个打印精美的 yml 文件,而不必关心简单的语法,您声称这会强化您的基础设施即代码后端,以便相同的用户不会搞砸它? Sorry, I'm calling bull pucky on that assertion.抱歉,我认为这种说法是胡说八道。 What's to stop anyone from totally screwing up your builds by pushing a change to the yml file which breaks the integration with groovy, or worse, completely changes an algorithm you worked hard to customize?是什么阻止任何人通过将更改推送到 yml 文件来完全搞砸您的构建,这会破坏与 groovy 的集成,或者更糟糕的是,完全更改您努力定制的算法?

Sorry, I just don't get it.对不起,我只是不明白。 Sure, making something more human readable is always a good thing.当然,让一些东西更具可读性总是一件好事。 Doing it because of the reasons you've stipulated makes no sense, though.但是,因为您规定的原因而这样做是没有意义的。 Also, unless you have a super simple defined algorithm in your CI/CD process, without any non-continuous-passing-style transform methods being implemented, then using the current iterations of the yml-as-Jenkinsfile-templates plugins is probably not the way you want to go.此外,除非您在 CI/CD 过程中有一个超级简单的定义算法,没有实现任何非连续传递式转换方法,否则使用 yml-as-Jenkinsfile-templates 插件的当前迭代可能不是你想要的方式 go。

Now, you could write your own plugin to do this, but what's the technical debt on that, versus just learning the groovy syntax?现在,您可以编写自己的插件来执行此操作,但与仅学习 groovy 语法相比,技术债务是什么? Also, it still doesn't prevent users from making code changes to your build infrastructure, then integrating those changes in a simple yml file.此外,它仍然不会阻止用户对您的构建基础结构进行代码更改,然后将这些更改集成到一个简单的 yml 文件中。

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

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