【问题标题】:How to change a Git URL in all Jenkins jobs如何在所有 Jenkins 作业中更改 Git URL
【发布时间】:2014-02-08 01:53:24
【问题描述】:

我在 Jenkins 中有 100 多个工作,自从我们更改了 git 服务器后,我必须在每个工作中更改一个 Git URL。 我必须遍历每个作业并更改 Git URL。谁能帮我写一个 groovy 脚本?

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

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

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

【问题讨论】:

  • jenkin 使用的 git 实例配置在一个中心位置;詹金斯的配置页面。选择 git 作为 SCM 的项目使用该设置。

标签: jenkins jenkins-plugins jenkins-cli django-jenkins jenkins-scriptler


【解决方案1】:

下面的脚本将修改所有 Git URL。您将需要填写 modifyGitUrl 方法。脚本是为 Git 插件版本 2.3.2 编写的。检查git plugin source code 以将其调整为您需要的版本,例如构造函数参数可能已更改。

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

【讨论】:

  • 以上脚本的使用方法有介绍吗?
【解决方案2】:

我会关闭服务器并使用脚本(sed/awk perl 或其他东西)编辑所有 config.xml 文件,然后重新启动 jenkins 以加载新配置。

如果关闭 jenkins 不是一个选项,则可以使用类似的方式编辑和发布每个 config.xml

GET http://myserver/job/config.xml| sed s/oldurl/newurl/g |POST http://myserver/job/config.xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多