Search code examples
jenkinsjenkins-job-dsl

Get list of jobs which created via job-dsl and list of manual jobs


Part of the job was generated through job-dsl, but there is still part of the job that was created by manual. If Jenkins has a mechanism or api to get a list of all manual jobs.

I see that all jobs through job-dsl have a mark which job created them(seed job). Perhaps having received a list of all generated ones, you can take a list of all jobs and weed out manual ...


Solution

  • JobDSL saves information about generated jobs in the javaposse.jobdsl.plugin.ExecuteDslScripts.xml file, example:

    <?xml version='1.1' encoding='UTF-8'?>
    <javaposse.jobdsl.plugin.DescriptorImpl plugin="job-dsl@1.77">
      <templateJobMap class="com.google.common.collect.HashMultimap" serialization="custom">
        <com.google.common.collect.HashMultimap>
          <default/>
          <int>8</int>
          <int>0</int>
        </com.google.common.collect.HashMultimap>
      </templateJobMap>
      <generatedJobMap class="concurrent-hash-map">
        <entry>
          <string>generated-job-name1</string>
          <javaposse.jobdsl.plugin.SeedReference>
            <seedJobName>job-which-created-it</seedJobName>
            <digest>hash1</digest>
          </javaposse.jobdsl.plugin.SeedReference>
        </entry>
        <entry>
          <string>generated-job-name2</string>
          <javaposse.jobdsl.plugin.SeedReference>
            <seedJobName>job-which-created-it</seedJobName>
            <digest>hash2</digest>
          </javaposse.jobdsl.plugin.SeedReference>
        </entry>
        ...
      </generatedJobMap>
    </javaposse.jobdsl.plugin.DescriptorImpl>
    

    If you parse the file and collect all javaposse.jobdsl.plugin.DescriptorImpl/generatedJobMap/entry/string values then you get a list of all generated jobs.

    You can next get all jobs by using Jenkins.get().getItems(), collect all names and at the end remove those which were found in the javaposse.jobdsl.plugin.ExecuteDslScripts.xml file.


    EDIT:

    The file keeps an XML representation of the javaposse.jobdsl.plugin.DescriptorImpl class. You can also get it programmatically: Jenkins.get().getDescriptorByType(Class<?> type).

    def jenkins = Jenkins.get()
    def clazz = Class.forName('javaposse.jobdsl.plugin.DescriptorImpl', true, jenkins.pluginManager.uberClassLoader)
    def descriptor = jenkins.getDescriptorByType(clazz)
    descriptor.generatedJobMap // map with listed previously entries