本文介绍了在Jenkinsfile中设置可重用属性的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jenkins管道共享库可以按需发送通知.用户基本上必须为他要通知的每个阶段发送通知信道详细信息,例如松弛的信道名称或电子邮件ID.

My Jenkins pipeline shared library can send notification on demand. The user basically has to send the the notifcation channel details like slack channel name or email id for each stage that he wants notifcation.

我不希望用户在每个阶段都重复此属性,而只是在Jenkinsfile中定义一次即可,我可以使用它.设置此变量的最佳方法是什么?

I do not want user to repeat this property in every stage but rather define it once in Jenkinsfile and I can use that. What would be the best way to set this variable?

示例:

// this properties needs to be accessed by my groovy files
def properties = "channelNm,token"
node('myNode'){

stage('checkout'){
   slackNotify = "yes"
   .....
 }
 stage('compile'){
   slackNotify = "yes"
   .....
 }
}

推荐答案

使用Jenkins共享库时,可以创建配置类,还可以公开允许修改配置对象的DSL脚本.看下面的例子.假设您在src文件夹中有一个名为NotificationConfig的类:

When you use Jenkins shared library you can create a configuration class and you can expose a DSL script that allows you modifying your configuration object. Take a look at following example. Let's say you have a class called NotificationConfig located in src folder:

src/NotificationConfig.groovy

@Singleton
class NotificationConfig {
    String slackChannelName
    String email
    String otherStuff
}

此类是一个单例,这意味着您可以使用NotificationConfig.instance获得该实例(单个实例).现在,假设您在vars文件夹中有一个名为notificationConfig.groovy的DSL脚本:

This class is a singleton which means that you can get instance (a single one) of this with NotificationConfig.instance. Now, let's say you have a DSL script called notificationConfig.groovy located in vars folder:

vars/notificationConfig.groovy

#!groovy

def call(Closure body) {
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = NotificationConfig.instance
    body()
}

这是一个非常简单的脚本,委托封闭体在NotificationConfig对象的上下文中执行.现在,让我们看一下使用notificationConfig DSL设置一些配置值的非常简单的脚本化管道:

This is a very simple script that delegates closure body to be executed in context of NotificationConfig object. Now let's take a look at very simple scripted pipeline that uses notificationConfig DSL to set some configuration values:

Jenkinsfile

notificationConfig {
    email = '[email protected]'
    slackChannelName = 'test'
}

node {
    stage('Test') {
        echo NotificationConfig.instance.email
    }
}

当我运行这个简单的管道时,我看到:

When I run this simple pipeline I see:

[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
[email protected]
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

如您所见,您可以使用此类DSL NotificationConfig对象进行公开,并让管道定义其自己的值,然后使用NotificationConfig.instance对象在共享库中访问这些值.

As you can see you can expose with such DSL NotificationConfig object and let pipeline to define its own values and then access these values in the shared library with NotificationConfig.instance object.

这是Jenkins Pipeline共享库中非常流行的模式.您可以在此Jenkins博客文章中了解更多相关信息- https://jenkins.io/blog/2017/10/02/pipeline-templates-with-shared-libraries/

This is pretty popular pattern in Jenkins Pipeline shared libraries. You can read more about it in this Jenkins blog post - https://jenkins.io/blog/2017/10/02/pipeline-templates-with-shared-libraries/

这篇关于在Jenkinsfile中设置可重用属性的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 02:53