簡體   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