[JIRA] (JENKINS-61399) Improve How to use RefSpecsSCMSourceTrait

13 views
Skip to first unread message

mateusmiranda@gmail.com (JIRA)

unread,
Mar 9, 2020, 10:20:03 AM3/9/20
to jenkinsc...@googlegroups.com
Mateus Miranda created an issue
 
Jenkins / Improvement JENKINS-61399
Improve How to use RefSpecsSCMSourceTrait
Issue Type: Improvement Improvement
Assignee: Daniel Spilker
Components: job-dsl-plugin
Created: 2020-03-09 14:19
Priority: Minor Minor
Reporter: Mateus Miranda

 

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)
                }
            }
        }

    }
}

 

Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.13.12#713012-sha1:6e07c38)
Atlassian logo

mateusmiranda@gmail.com (JIRA)

unread,
Mar 9, 2020, 10:54:03 AM3/9/20
to jenkinsc...@googlegroups.com
Mateus Miranda updated an issue
Change By: Mateus Miranda
 

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?
{code:java}
{code}
 
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')
                     }
                    }

mateusmiranda@gmail.com (JIRA)

unread,
Mar 9, 2020, 10:55:02 AM3/9/20
to jenkinsc...@googlegroups.com
{code:java}

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

guilherme@souza.tech (JIRA)

unread,
Mar 10, 2020, 5:21:03 AM3/10/20
to jenkinsc...@googlegroups.com

mail@daniel-spilker.com (JIRA)

unread,
Mar 16, 2020, 4:00:04 PM3/16/20
to jenkinsc...@googlegroups.com
Daniel Spilker updated Improvement JENKINS-61399
 

You can try to use Dynamic DSL:

multibranchPipelineJob('example-mbp') {
  branchSources {
    branchSource {
      source {
        github {
          id('4433232')
          repoOwner('comp')
          repository('test')
          repositoryUrl('http://acme.org')
          configuredByUrl(false)
          traits {
            refSpecsSCMSourceTrait {
              templates {
                refSpecTemplate {
                  value('foo')
                }
              }
            }
          }
        }
      }
    }
  }
}

Or use the following configure block. Make sure to use the correct element names. Configure the job manually and use the element names from that config.

multibranchPipelineJob('example-mbp') {
  branchSources {
    github {
      id('1234')
      repoOwner('comp')
    }
  }
  configure {
    def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
    traits << 'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait' {
      templates {
        'jenkins.plugins.git.traits.RefSpecsSCMSourceTrait_-RefSpecTemplate' {
          value('+refs/heads/master:refs/remotes/@{remote}/master')
        }
      }
    }
  }
}
Change By: Daniel Spilker
Status: Open Fixed but Unreleased
Resolution: Fixed
Reply all
Reply to author
Forward
0 new messages