Search code examples
jenkinsjenkins-pipelinejenkins-job-dsl

Configure block in Jenkins pipelinejob using DSL?


Trying to write a DSL Jenkins pipeline job using jobs-dsl and not sure if I'm hitting a couple of pipeline job limitations or am missing something more fundamental.

1 - Configuring "Polling ignores commits in certain paths" under "Additional Behaviours" using the configure block does not seem to be working as expected in the pipeline job; I have tested and this configure block works as expected in a freestyle job dsl. Searched and couldn't find anything relevant - can someone confirm if the below is supported/not supported within the below pipeline job?

    pipelineJob("ProjA/pipeline") 
    {
        logRotator
        {
          daysToKeep 10
          numToKeep 30
        }
        definition 
        {
          cpsScm 
          {
            scm
            {
                git('git@github.com:sample-org/pipeline.git', '*/develop')
            }
            configure { gitScm -> 
                gitScm / 'extensions' << 'hudson.plugins.git.extensions.impl.PathRestriction' {
                    excludedRegions('sample/dirs')
                }
              }
           }
        }
     }

2 - How do we pass credentials to the git under scm block under pipeline? Works for freestyle jobs but having trouble getting it to work here

Thanks in advance.


Solution

  • The built-in DSL only supports basic options. But the Dynamic DSL supports almost any option.

    pipelineJob('example') {
      definition {
        cpsScmFlowDefinition {
          scm {
            gitSCM {
              userRemoteConfigs {
                userRemoteConfig {
                  url('git@github.com:sample-org/pipeline.git')
                  name('master')
                  refspec(null)
                  credentialsId('example')
                }
              }
              branches {
                branchSpec {
                  name('*/develop')
                }
              }
              extensions {
                pathRestriction {
                  includedRegions(null)
                  excludedRegions('sample/dirs')
                } 
              }
              doGenerateSubmoduleConfigurations(false)
              browser {}
              gitTool(null)
            }
          }
          scriptPath('Jenkinsfile')
        }
      }
    }