Med Safa Ben Saidane
Med Safa Ben Saidane

Reputation: 11

Try to understand a Groovy Syntax

I'm new to the groovy language under Jenkins (using the Job DSL plugin) and I'm trying to understand an old script written by another person, I cannot figure out what is happening on line 3. Any help on this please:

configure { project ->
          /* Configuration Mantis */
          def properties = project / 'properties'
          def mantisProject = properties / 'hudson.plugins.mantis.MantisProjectProperty' {
              siteName(MANTIS_URL)
              projectId('1')
              category('General')
              pattern('"[%ID%]"')
              linkEnabled('true')
              regex("[^0-9]?([0-9]?[0-9]?[0-9][0-9][0-9][0-9][0-9])[^0-9]?[^0-9]+.*")
              regexpPattern {
                  pattern("[^0-9]?([0-9]?[0-9]?[0-9][0-9][0-9][0-9][0-9])[^0-9]?[^0-9]+.*")
                  flags(0)
              }
          }

Upvotes: 1

Views: 163

Answers (1)

yong
yong

Reputation: 13712

It's Jenkins job DSL script, not pipeline as code. His script is to generate a XML snippet which will be a part of the job's config.xml.

Assume you have a job: test and its url is https://myjenkins.abc.com/Dev/test, you can open this url:https://myjenkins.abc.com/Dev/test/config.xml to get the job's config.xml.

Here is a tool to generate Jenkins job XML from job DSL: http://job-dsl.herokuapp.com/, with it you can pre-check your DSL script is correct or not.

Copy below DSL into the left textbox of the tool.

job('test') {

    configure { project ->

          def properties = project / 'properties'
          def mantisProject = properties / 'hudson.plugins.mantis.MantisProjectProperty' {
              siteName(MANTIS_URL)
              projectId('1')
              category('General')
              pattern('"[%ID%]"')
              linkEnabled('true')
              regex("[^0-9]?([0-9]?[0-9]?[0-9][0-9][0-9][0-9][0-9])[^0-9]?[^0-9]+.*")
              regexpPattern {
                  pattern("[^0-9]?([0-9]?[0-9]?[0-9][0-9][0-9][0-9][0-9])[^0-9]?[^0-9]+.*")
                  flags(0)
              }
          }           

   }
}

Click run you will get the corresponding XML:

<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.plugins.mantis.MantisProjectProperty>
            <siteName>MANTIS_URL</siteName>
            <projectId>1</projectId>
            <category>General</category>
            <pattern>"[%ID%]"</pattern>
            <linkEnabled>true</linkEnabled>
            <regex>[^0-9]?([0-9]?[0-9]?[0-9][0-9][0-9][0-9][0-9])[^0-9]?[^0-9]+.*</regex>
            <regexpPattern>
                <pattern>[^0-9]?([0-9]?[0-9]?[0-9][0-9][0-9][0-9][0-9])[^0-9]?[^0-9]+.*</pattern>
                <flags>0</flags>
            </regexpPattern>
        </hudson.plugins.mantis.MantisProjectProperty>
    </properties>
    <scm class='hudson.scm.NullSCM'></scm>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
</project>

More detail you can see post1 and post2

Upvotes: 1

Related Questions