如何在所有Jenkins作业中更改Git URL [英] How to change a Git URL in all Jenkins jobs

查看:612
本文介绍了如何在所有Jenkins作业中更改Git URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我们更改git服务器以来,我在詹金斯(Jenkins)有100多个工作,并且每项工作都必须更改一个Git URL. 我必须遍历每个作业并更改Git URL.有人可以帮我编写一些时髦的脚本吗?

I have more than 100 jobs in Jenkins and I have to change a Git URL in each and every job since we changed the git server. I must traverse each job and change the Git URL. Can anyone help me with a groovy script?

我能够遍历每项工作,但无法获取或更改Git URL:

I was able to traverse each job, but not able to get the Git URL or change it:

import hudson.plugins.emailext.*
import hudson.model.*
import hudson.maven.*
import hudson.maven.reporters.*
import hudson.tasks.*

// For each project
for(item in Hudson.instance.items) {
 println("JOB : " + item.name);
}

我非常需要帮助,请有人帮助我.

I badly need help in this, please someone help me.

推荐答案

以下脚本将修改所有Git URL.您将需要填充ModifyGitUrl方法.脚本是为Git插件版本2.3.2编写的.检查 git插件源代码以将其调整为所需的版本,例如构造函数参数可能已更改.

The script below will modify all Git URL. You will need to fill the modifyGitUrl method. Script is written for Git plugin version 2.3.2. Check the git plugin source code to adjust it to the version you need e.g. the constructor parameters might have changed.

import hudson.plugins.git.*
import jenkins.*
import jenkins.model.*

def modifyGitUrl(url) {
  // Your script here
  return url + "modified"
}

Jenkins.instance.items.each {
  if (it.scm instanceof GitSCM) {
    def oldScm = it.scm
    def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
      new UserRemoteConfig(modifyGitUrl(it.url), it.name, it.refspec, it.credentialsId)
    }
    def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
                            oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
    it.scm = newScm 
    it.save()
  }
}

这篇关于如何在所有Jenkins作业中更改Git URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆