Jenkins - 更改Stash repo浏览器URL

时间:2015-12-30 19:45:10

标签: git jenkins groovy jenkins-plugins jenkins-cli

我尝试更改所有作业的Stash repo浏览器网址(约200个作业),我设法更改了ssh网址。

我使用groovy脚本执行此操作,并且我通过Jenkins中的脚本控制台执行它。

这是脚本的简短片段:

def OLD_GIT_URL = "old_url"
def NEW_GIT_URL = "new_url"
def OLD_STASH_REPO_URL_BROWSER = "oldStash/stash"

def allJobs = Jenkins.instance.getItems()

allJobs.each { job ->
    def jobScm = job.getScm()
    if (jobScm instanceof GitSCM) {
        def oldScm  = jobScm
//        println "${job.getName()} ->  It has git in it"
        def git_url = jobScm.userRemoteConfigs[0].url
        println "${job.getName()} -> ${git_url}"
        def new_git_url = git_url.replaceAll(OLD_GIT_URL, NEW_GIT_URL)
        println "The replace url is: ${new_git_url}"
        // Uncomment
        //jobScm.userRemoteConfigs[0].url = new_git_url
        // We have a lot of jobs which contains the repo url
        if (jobScmBrowser instanceof Stash){
            def repo_url = jobScmBrowser.getRepoUrl()
            println "${job.getName()} --> ${repo_url}"
            new_repo_url = repo_url.replaceAll(OLD_STASH_REPO_URL_BROWSER, NEW_GIT_URL)
            println "The replace repo url is: ${new_repo_url}"
            // TODO: Replace the old url with new one
            // I can't change the repo url using this property, it's private!! 
            //jobScmBrowser.repoUrl = new_repo_url
        }
        else {
            println "${job.getName()} --> Not a stash browser"
        }
    }
    else {
        println "${job.getName()} -> No git in it"
    }

}

1 个答案:

答案 0 :(得分:1)

所以,我通过创建一个新的Stash对象来解决:

jobScmBrowser.browser = new hudson.plugins.git.browser.Stash(new_repo_url)