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

Job DSL enable programmatically "Advanced clone behaviours"

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • job-dsl-plugin

      Hi,

      My pipelines are managed with multi branch. I source code from GitHub with the GitHub plugin. Every component use Git tags to compute by themselves their version number. By default, Jenkins doesn't fetch tags during clones. So I need to activate "Advanced clone behaviors" (the option "Fetch tags" is enabled by default) for each of my projects.

      My issue is that I didn't found any solution to enable "Advanced clone behaviors" behavior in "multibranchPipelineJob" with CasC plugin. I have searched across, Stack Overflow, web and documentation without ending.

      Here is my example CasC section relative to `jobs` CasC section:

      jobs: - script: |
          multibranchPipelineJob('my-repo') {
            displayName('my-repo')
            description('my-repo')
      
            branchSources {
              github {
                id('my-repo')
                apiUri('https://github.corp.com/api/v3')
                scanCredentialsId('my-creds')
                repoOwner('my-org')
                repository('my-repo')
              }
            }
            orphanedItemStrategy {
              discardOldItems {
                daysToKeep(31)
              }
            }
            triggers {
              // Schedule every minutes
              cron('* * * * *')
            }
            configure {
              // Re-write Git traits
              def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
              // Discover branches
              // See: https://github.com/jenkinsci/github-branch-source-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github_branch_source/BranchDiscoveryTrait.java
              traits << 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait' {
                // Select only branches (no PRs)
                strategyId(1)
              }
              // Discover tags
              // See: https://github.com/jenkinsci/github-branch-source-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github_branch_source/TagDiscoveryTrait.java
              traits << 'org.jenkinsci.plugins.github_branch_source.TagDiscoveryTrait' { }
              // Discover pull-requests
              // See: https://github.com/jenkinsci/github-branch-source-plugin/blob/master/src/main/java/org/jenkinsci/plugins/github_branch_source/OriginPullRequestDiscoveryTrait.java
              traits << 'org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait' {
                // Merge automatically PRs with destination
                strategyId(1)
              }
            }
          }
      

      Every time I deploy a new Jenkins instance (this can append multiple times per day with Kubernetes), I have to manually update the option in the GUI for every project I have. This is non-practical and time-consuming.

          [JENKINS-64134] Job DSL enable programmatically "Advanced clone behaviours"

          Burkhard Dietterle added a comment - - edited

          Hi clementlesne,

           

          Was wondering about the same issue. I then

          1. checked the diffs in the "Job Config History" of a job I manually configured with "Advanced Clone Behaviours" and then
          2. cross-checked with the "Job DSL Api Viewer" (you should finds yours unders (your_jenkins_base_url)/jenkins/plugin/job-dsl/api-viewer

           and inferred it has to be

          git {
              traits {
                  cloneOptionTrait {
                      extension {
                          noTags(false) // equals "Fetch tags" checked in the GUI
                          honorRefspec(...)
                          reference(...)
                          shallow(...)
                          timeout(...)
                      }
                  }
              }
          }
          

          But, I agree, proper documentation for this feature would be cool.

          Burkhard Dietterle added a comment - - edited Hi clementlesne ,   Was wondering about the same issue. I then checked the diffs in the "Job Config History" of a job I manually configured with "Advanced Clone Behaviours" and then cross-checked with the "Job DSL Api Viewer" (you should finds yours unders (your_jenkins_base_url)/jenkins/plugin/job-dsl/api-viewer  and inferred it has to be git { traits { cloneOptionTrait { extension { noTags(false) // equals "Fetch tags" checked in the GUI honorRefspec(...) reference(...) shallow(...) timeout(...) } } } } But, I agree, proper documentation for this feature would be cool.

          Maayan added a comment -

          Hi burkhard,

          I know this is an old thread, but very relevant for me as I couldn't find a way to change default git checkout behavior in code.

          Is the code you mentioned can be implemented in the pipeline? If so, do you remember how?
          Couldn't figure it out and can't find any documentation on this.
          Also this link "jenkins/plugin/job-dsl/api-viewer" doesn't work for so can't check it out.

          Appreciate any assistance. 

           

          Thanks

          Maayan added a comment - Hi burkhard , I know this is an old thread, but very relevant for me as I couldn't find a way to change default git checkout behavior in code. Is the code you mentioned can be implemented in the pipeline? If so, do you remember how? Couldn't figure it out and can't find any documentation on this. Also this link "jenkins/plugin/job-dsl/api-viewer" doesn't work for so can't check it out. Appreciate any assistance.    Thanks

          Hi mapel88 ,

           

          try with "index.html" at the end of the API viewer URL, i.e. "{jenkins}/plugin/job-dsl/api-viewer/index.html"

           

          For how to run job DSL scripts in your pipeline, check https://www.jenkins.io/doc/pipeline/steps/job-dsl/

           

          Kind regards,

          Burkhard

          Burkhard Dietterle added a comment - Hi mapel88 ,   try with "index.html" at the end of the API viewer URL, i.e. "{jenkins}/plugin/job-dsl/api-viewer/ index.html "   For how to run job DSL scripts in your pipeline, check https://www.jenkins.io/doc/pipeline/steps/job-dsl/   Kind regards, Burkhard

            jamietanna Jamie Tanna
            clementlesne Clément Lesné
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: