简体   繁体   中英

Set StashNotifier with Groovy

I am trying to add the StashNotifier configuration to Jenkins via Groovy

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 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. 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. 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

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. 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.

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 . 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.

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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