0

I have a lot of jenkin pipelines that load the scripts with scm from Bitbucket. We are migrating all the repositories to Git and I want to know if there is a way to change the scm of the pipeline script from hg to Git not using a script.

I was trying to figure out a script to execute from the console?

Thanks!

tijko
  • 7,599
  • 11
  • 44
  • 64
  • I founs somet idea here https://stackoverflow.com/questions/21640907/how-to-change-a-git-url-in-all-jenkins-jobs/27646182#27646182 where it change the scm but i this post is a job. a pipeline is an object of class org.jenkinsci.plugins.workflow.job.WorkflowJob and it hasn't the scm attribute – Andrea Vavassori Mar 11 '20 at 14:28

1 Answers1

2

after investigate I found this solution to change the Mercurialscm to GitScm mantaining the information of the repository url, and the credentials of 1 pipeline, and i could extend the same to a list of piplines

    import hudson.plugins.git.*;

def it = Jenkins.instance.getItemByFullName("test-pipeline-scm")
def definition = it.getDefinition() 
String scriptPath = definition.getScriptPath()
String url =  definition.getScm().getSource()
String credentials = definition.getScm().getCredentialsId()
def scm = new GitSCM(GitSCM.createRepoList(url, credentials),
                Collections.singletonList(new BranchSpec("*/master")),
                false, Collections.<SubmoduleConfig>emptyList(),
                null, null, Collections.emptyList())
def newDefinition = new org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition(scm, scriptPath)
it.definition = newDefinition
it.save()