How to configure server in Build Triggers → Gerrit event in job created by Job DSL

457 Views Asked by At

I'm creating a Jenkins job which gets triggered by a commit to Gerrit.

pipelineJob(jobName) {
  displayName(displayString)
  triggers {
    gerrit {
      events {
        patchsetCreated()
      }
      project('plain:Verify', ['ant:**'])
      configure { project ->
        project / triggers << 'com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject' {
          serverName('aj-Gerrit')
        }
      }
    }
  }
}

This works fine and when I look in the UI, I see this.

enter image description here

However, what I would like is for the Gerrit server aj-Gerrit to be selected by default and not Any Server

enter image description here

I've tried to achieve this by adding in the configure block in my Groovy code,

configure { project ->
  project / triggers << 'com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject' {
     serverName('aj-Gerrit')
  }
}

but it doesn't seem to be working.

Is there a way to select a server by default?

1

There are 1 best solutions below

0
agabrys On

The configure function doesn't exist in the gerrit closure. Usually, all available Job DSL options are visible on the page:

<your-jenkins-url>/plugin/job-dsl/api-viewer/index.html

However, the Gerrit plugin breaks the page (cause: JENKINS-53250).

This is generated XML when a server is added manually to the job:

<triggers>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger plugin="[email protected]">
  <spec></spec>
  <gerritProjects>
    <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
      <compareType>PLAIN</compareType>
      <pattern></pattern>
      <branches>
        <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
          <compareType>PLAIN</compareType>
          <pattern></pattern>
        </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
      </branches>
      <disableStrictForbiddenFileVerification>false</disableStrictForbiddenFileVerification>
    </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
  </gerritProjects>
  <dynamicGerritProjects class="empty-list"/>
  <skipVote>
    <onSuccessful>false</onSuccessful>
    <onFailed>false</onFailed>
    <onUnstable>false</onUnstable>
    <onNotBuilt>false</onNotBuilt>
    <onAborted>false</onAborted>
  </skipVote>
  <silentMode>false</silentMode>
  <enableTopicAssociation>false</enableTopicAssociation>
  <notificationLevel></notificationLevel>
  <silentStartMode>false</silentStartMode>
  <escapeQuotes>true</escapeQuotes>
  <nameAndEmailParameterMode>PLAIN</nameAndEmailParameterMode>
  <dependencyJobsNames></dependencyJobsNames>
  <commitMessageParameterMode>BASE64</commitMessageParameterMode>
  <changeSubjectParameterMode>PLAIN</changeSubjectParameterMode>
  <commentTextParameterMode>BASE64</commentTextParameterMode>
  <buildStartMessage></buildStartMessage>
  <buildFailureMessage></buildFailureMessage>
  <buildSuccessfulMessage></buildSuccessfulMessage>
  <buildUnstableMessage></buildUnstableMessage>
  <buildNotBuiltMessage></buildNotBuiltMessage>
  <buildAbortedMessage></buildAbortedMessage>
  <buildUnsuccessfulFilepath></buildUnsuccessfulFilepath>
  <customUrl></customUrl>
  <serverName>__ANY__</serverName>
  <triggerOnEvents class="linked-list">
    <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
      <excludeDrafts>false</excludeDrafts>
      <excludeTrivialRebase>false</excludeTrivialRebase>
      <excludeNoCodeChange>false</excludeNoCodeChange>
      <excludePrivateState>false</excludePrivateState>
      <excludeWipState>false</excludeWipState>
      <commitMessageContainsRegEx></commitMessageContainsRegEx>
    </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent>
    <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginDraftPublishedEvent/>
  </triggerOnEvents>
  <dynamicTriggerConfiguration>false</dynamicTriggerConfiguration>
  <triggerConfigURL></triggerConfigURL>
  <triggerInformationAction/>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
</triggers>

You could try to use (different class than the one you used):

configure { project ->
  project / triggers << 'com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger' {
     serverName('aj-Gerrit')
  }
}

If it doesn't work, then more elements from the generated XML must be provisioned too.