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

Improve How to use RefSpecsSCMSourceTrait

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • job-dsl-plugin
    • None

       

      I've been trying to populate the RefSpec template on the GitSCM source,
      but I cannot seem to get it to work.
      I've failed to find any examples of how to do it.

      Does anyone have an idea how to use it?

      package utils
      
      import groovy.transform.builder.Builder
      import groovy.transform.builder.SimpleStrategy
      import javaposse.jobdsl.dsl.DslFactory
      import javaposse.jobdsl.dsl.Job
      import javaposse.jobdsl.dsl.jobs.MultibranchWorkflowJob
      
      // Created to make it easy to creade new jobs without duplicating the code
      
      @Builder(builderStrategy = SimpleStrategy, prefix = '')
      class GithubPipeline {
          String viewPrefix
          String repository
          String jobSuffix = ""
          String includes = "*"
          String jenkinsfileLocation = "Jenkinsfile"
          Boolean discoverBranches = true
          Boolean discoverTags = true
          Boolean includeMasterRef = false
      
          MultibranchWorkflowJob build(DslFactory dslFactory) {
              def cleanedRepoString = this.repository.toLowerCase().replaceAll(/_/, '-');
      
              def jobId = "${this.viewPrefix.toLowerCase()}-${cleanedRepoString}"
              if (this.jobSuffix) jobId += "-${jobSuffix}"
      
              dslFactory.multibranchPipelineJob(jobId) {
                  branchSources {
                      github {
                          id(jobId)
                          includes(this.includes)
                          repoOwner('comp')
                          repository(this.repository)
                          scanCredentialsId('github-appscicomp-dev-token')
                          checkoutCredentialsId('github-appscicomp-dev-token')
                      }
                  }
                  factory {
                      workflowBranchProjectFactory {
                          scriptPath(this.jenkinsfileLocation)
                      }
                  }
                  configure {
                      def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
                      if (this.discoverBranches) {
                          traits << 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait' {
                              strategyId(3) // detect all branches -refer the plugin source code for various options
                          }
                      }
                      if (this.discoverTags) {
                          traits << 'org.jenkinsci.plugins.github_branch_source.TagDiscoveryTrait' { }
                      }
                      if (this.includeMasterRef) {
                          traits << 'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait' {
                              tempates << 'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait$RefSpecTemplate' {
                                  value('+refs/heads/master:refs/remotes/@{remote}/master')
                              }
                          }
      
                      }
                  }
                  orphanedItemStrategy {
                      discardOldItems {
                          daysToKeep(20)
                          numToKeep(25)
                      }
                  }
              }
      
          }
      }
      

       The part which I'm struggling is:

      traits << 'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait' {
          tempates << 'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait$RefSpecTemplate' { 
              value('+refs/heads/master:refs/remotes/@\{remote}/master')
          }
      }

       

            jamietanna Jamie Tanna
            mmiranda Mateus Miranda
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: