繁体   English   中英

用Groovy设置StashNotifier

[英]Set StashNotifier with Groovy

我正在尝试通过Groovy将StashNotifier配置添加到Jenkins

import jenkins.model.*;
import org.jenkinsci.plugins.stashNotifier.*

def instance = Jenkins.getInstance()
def bitbucket = instance.getDescriptor(StashNotifier)

println "--> configure Stash Notifier..."

def bitBucketNotifier = new StashNotifier (
    "https://servername:8443", //stashServerBaseUrl
    "user", //credentialsId
    false,  //ignoreUnverifiedSSLPeer
    "",  //commitSha1
    false,  //includeBuildNumberInKey
    "",  //projectKey
    false, //prependParentProjectKey
    false //disableInprogressNotification
)

bitbucket.save()
println "--> configure Stash Notifier... done"

我尝试实现的xml配置是

<?xml version='1.0' encoding='UTF-8'?>
<org.jenkinsci.plugins.stashNotifier.StashNotifier_-DescriptorImpl plugin="stashNotifier@1.11.6">
  <credentialsId>user</credentialsId>
  <stashRootUrl>https://servername:8443/</stashRootUrl>
  <ignoreUnverifiedSsl>false</ignoreUnverifiedSsl>
  <includeBuildNumberInKey>false</includeBuildNumberInKey>
  <prependParentProjectKey>false</prependParentProjectKey>
  <disableInprogressNotification>false</disableInprogressNotification>
</org.jenkinsci.plugins.stashNotifier.StashNotifier_-DescriptorImpl>

我是Java和groovy的新手,但是我无法使它正常工作。 我觉得我很亲密,可能缺少一两个点。

我试图让Jenkins在启动时进行配置,然后在对核心集成进行任何更改的情况下重新配置自身。 在这种情况下,BitBucket服务器不会更改,但是如果用户确实进行了更改以指向其他内容,则将Jenkins重新配置为指向正确的内容

我已经做了类似的事情:

#!groovy
import jenkins.model.*;
import org.jenkinsci.plugins.*;
import net.sf.json.JSONObject;

String url = 'https://stashblablablabla';
String credentials = '01111111-e222-3333-eeff-4f4444e44bc4';

def j = Jenkins.getInstance();

def stash = j.getExtensionList(
  stashNotifier.StashNotifier.DescriptorImpl.class)[0];

def formData = [
  stashRootUrl: url,
  credentialsId: credentials,
  ignoreUnverifiedSsl: false,
  includeBuildNumberInKey: false,
  prependParentProjectKey: false,
  disableInprogressNotification: false,
  considerUnstableAsSuccess: false
] as JSONObject;

stash.configure(null, formData);
j.save();

我已经找到解决方案,阅读以下内容:

它似乎正在我的测试平台上工作。

@ flue42有一个很好的答案,尽管要遵循JSON和FormData有点困难。 取而代之的是,您可以简单地将值写入对象的相应属性并保存。

如果您碰巧有多个要通知的Stash服务器(不支持全局配置级别),则需要基于每个作业覆盖全局设置,请参阅我的其他答案以及使用Job进行配置的框架DSL插件。

也许看这个实现自己的最烦人的部分是它引用了stashServerBaseUrl的代码(正如您在注释中所指出的那样),但是Groovy / form中所需的名称是stashRootUrl 查找正确名称的地方就在这里

#!groovy
import jenkins.model.*;
import org.jenkinsci.plugins.stashNotifier.*;

String url = 'https://stashblablablabla';
String credentials = '01111111-e222-3333-eeff-4f4444e44bc4';

def j = Jenkins.getInstance();

def stash = instance.getDescriptor(StashNotifier)

stash.stashRootUrl = url //stashServerBaseUrl
stash.credentialsId = credentials //credentialsId
stash.ignoreUnverifiedSsl = false  //ignoreUnverifiedSSLPeer
stash.includeBuildNumberInKey =  false  //includeBuildNumberInKey
stash.projectKey =    ""  //projectKey
stash.prependParentProjectKey =   false //prependParentProjectKey
stash.disableInprogressNotification =  false //disableInprogressNotification

stash.save()

如果您希望通过Job DSL进行操作,则可以从Job DSL存储库中使用此示例。

// notify Stash using the global Jenkins settings
job('example-1') {
    publishers {
        stashNotifier()
    }
}

// notify Stash using the global Jenkins settings and sets keepRepeatedBuilds to true
job('example-2') {
    publishers {
        stashNotifier {
            keepRepeatedBuilds()
        }
    }
}

https://github.com/jenkinsci/job-dsl-plugin/blob/master/job-dsl-core/src/main/docs/examples/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext/stashNotifier.groovy

暂无
暂无

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

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