詹金斯管道模板 [英] Jenkins pipeline template

查看:95
本文介绍了詹金斯管道模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几个Java项目.每个项目都有自己的交付管道.

We have several Java projects. Each project has its own delivery pipeline.

所有管道共有以下共同步骤(简化):

All pipelines have the following steps in common (simplified):

  • 构建项目
  • 发布项目
  • 部署到测试环境
  • 部署到生产环境

项目管道的区别仅在于项目特定的属性,例如服务名称或测试和生产环境的IP地址.

The project pipelines only differ in project specific properties such as service names or the IP addresses of test and production environment.

问题是:我们如何才能避免所有项目都有共同之处? Jenkins的将管道作为代码"是否提供类似管道模板的内容?

The questions are: How could we avoid the boilerplate that all projects have in common? Does Jenkins "Pipeline as code" provide something like pipeline templates?

我可以想象一个模板将在我们的项目管道中节省很多冗余代码/步骤.因此,设置新项目,维护管道,保持管道同步会容易得多.

I could imagine that a template would save a lot of redundant code/steps in our project pipelines. Therefore it would be much easier to setup a new project, maintain the pipeline, keep the pipeline in sync...

推荐答案

一种对我们有效的方法是将部分管道(所有项目具有相同的部分)或整个管道放入 Jenkins共享库.

An approach that works well for us is to put parts of the pipeline (those that all projects have in common) or even the whole pipeline into a Jenkins shared library.

示例

以下脚本(template.groovy)在Jenkins共享库中定义为全局变量.该方法创建一个新的声明性管道(它也适用于脚本化管道语法).所有项目特定的属性都通过templateParams映射提供.

The following script (template.groovy) is defined as global variable in a Jenkins shared library. The method creates a new declarative pipeline (it also works for scripted pipeline syntax). All project specific properties are provided via the templateParams map.

/**
 * Defines a pipeline template (as a sample with one job parameter 
 * that should be common for all pipelines)
 */
def createMyStandardDeclarativePipeline(Map templateParams) {   

    pipeline {
        agent any
        parameters {
            string(name: 'myInput', description: 'Some pipeline parameters')
        }
        stages {
            stage('Stage one') {
                steps {
                    script {
                        echo "Parameter from template creation: " + templateParams.someParam
                    }
                }
            }
            stage('Stage two') {
                steps {
                    script {
                        echo "Job input parameter: " + params.myInput
                    }
                }
            }
        }
    }
}

使用此全局变量,以下行从我们的模板创建管道:

Using this global variable, the following line creates a pipeline from our template:

template.createMyStandardDeclarativePipeline(someParam: 'myParam')

结论

通过此概念,可以轻松定义管道模板并将其在多个项目中重复使用.

This concept makes it easy to define pipeline templates and reuse them in several projects.

根据问题中给出的示例,您可以使用简单的单行代码为项目创建交付管道:

Applied on the example given in the question, you can create a delivery pipeline for a project with a simple one-liner:

template.createStandardDeliveryPipeline(serviceName: 'myService', 
                                        testEnv: '192.168.99.104', 
                                        productionEnv: '192.168.99.105')


更新(30-09-2017):Declarative Pipelines 1.2版现在正式支持在共享库中声明管道块.请参阅: https://jenkins.io/doc/book/管道/共享库/#defining-declarative-pipelines


Update (30-09-2017): Declaring a pipeline block in a shared library is now officially supported with Declarative Pipelines version 1.2 . See: https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-declarative-pipelines

更新(06-10-2017):现在可以在此处找到扩展示例:

Update (06-10-2017): An extended example can now be found here: https://jenkins.io/blog/2017/10/02/pipeline-templates-with-shared-libraries/

这篇关于詹金斯管道模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆