Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-12785

Groovy script console SCMTrigger example causes invalid configuration

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • other
    • None

      The wiki page at https://wiki.jenkins-ci.org/display/JENKINS/Change+SCMTrigger+for+each+project+to+disable+during+the+night+and+the+week-end has an sample script which modifies the SCM trigger schedule. The script does not set the job field of the newly created trigger. I recently used a modified version of this script and encountered many NullPointerExceptions. I would recommend changing the sample to the following:

      import hudson.model.*
      import hudson.triggers.*
      
      
      TriggerDescriptor SCM_TRIGGER_DESCRIPTOR = Hudson.instance.getDescriptorOrDie(SCMTrigger.class)
      assert SCM_TRIGGER_DESCRIPTOR != null;
      
      for(item in Hudson.instance.items)
      {
        println("Working on project <$item.name>")
      		
        def trigger = item.getTriggers().get(SCM_TRIGGER_DESCRIPTOR)
        if(trigger != null && trigger instanceof SCMTrigger)
        {
          print("> $trigger.spec")
          String[] parts = trigger.spec.split(" ");
        		
          //Do wanted modifs
          if(parts[1] == "*" )
          {
            parts[1] = "7-21"
          }
          if(parts[4] == "*")
          {
            parts[4] = "1-5"
          }
          //end modifs
        
          StringBuilder newSpec = new StringBuilder();
          for(p in parts)
          {
            newSpec.append(p+" ");
          }
        
          println(" => $newSpec");
      
          def newTrigger = new SCMTrigger(newSpec.toString())
          newTrigger.job = item
      
          item.removeTrigger(SCM_TRIGGER_DESCRIPTOR)
          item.addTrigger(newTrigger)
        }
        else
        {
          println "> Nothing to do"
        }
      }
      

            batmat Baptiste Mathus
            wfhartford Wesley Hartford
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: