简体   繁体   English

用Groovy设置StashNotifier

[英]Set StashNotifier with Groovy

I am trying to add the StashNotifier configuration to Jenkins via 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"

The xml configuration I am trying to implement is 我尝试实现的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>

I am new to Java and groovy, but I cannot get this to work. 我是Java和groovy的新手,但是我无法使它正常工作。 I feel I am close, probably missing one or two little bits. 我觉得我很亲密,可能缺少一两个点。

I am trying to get Jenkins to configure upon start-up and then reconfigure itself if any changes are made to core integrations. 我试图让Jenkins在启动时进行配置,然后在对核心集成进行任何更改的情况下重新配置自身。 In this case, the BitBucket server won't change but if users do make the change to point at something else, Jenkins is reconfigured to point at the correct thing 在这种情况下,BitBucket服务器不会更改,但是如果用户确实进行了更改以指向其他内容,则将Jenkins重新配置为指向正确的内容

I've done something like that : 我已经做了类似的事情:

#!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();

I've figured that solution out reading that: 我已经找到解决方案,阅读以下内容:

It appears to be working in my test platform. 它似乎正在我的测试平台上工作。

@flue42 has a great answer, though going through the JSON and FormData is a bit tough to follow. @ flue42有一个很好的答案,尽管要遵循JSON和FormData有点困难。 Instead you can simply write the values to their respective properties on the object and save it. 取而代之的是,您可以简单地将值写入对象的相应属性并保存。

If you happened to have multiple Stash servers you wanted to notify (not suppported at the global configuration level), you'll need to override the global setting on a per job basis, see my other answer with the skeleton of doing it using the Job DSL plugin. 如果您碰巧有多个要通知的Stash服务器(不支持全局配置级别),则需要基于每个作业覆盖全局设置,请参阅我的其他答案以及使用Job进行配置的框架DSL插件。

Perhaps the most annoying part of looking at this to implement myself was in the code it references stashServerBaseUrl (as you noted in your comment) but then the required name in the Groovy/form is stashRootUrl . 也许看这个实现自己的最烦人的部分是它引用了stashServerBaseUrl的代码(正如您在注释中所指出的那样),但是Groovy / form中所需的名称是stashRootUrl The place to find the right names is right around here 查找正确名称的地方就在这里

#!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()

If you are looking to do it via Job DSL you can use this example from the Job DSL repo. 如果您希望通过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 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