将多个 git repos 签出到同一个 Jenkins 工作区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14843696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:29:43  来源:igfitidea点击:

Checkout multiple git repos into same Jenkins workspace

gitjenkins

提问by u123

Using Jenkins 1.501 and Jenkins Git plugin 1.1.26

使用 Jenkins 1.501 和 Jenkins Git 插件 1.1.26

I have 3 different git repos each with multiple projects.

我有 3 个不同的 git 仓库,每个仓库都有多个项目。

Now I need to checkout all projects from the 3 git repos into the same workspace on a Jenkins slave. I have defined each git repo in: Source code Management: Multiple SCMs. But each time a repo is checked out the previous repo (and its associated projects) is deleted.

现在我需要将 3 个 git 存储库中的所有项目签出到 Jenkins 从站上的同一个工作区中。我在:Source code Management: Multiple SCMs 中定义了每个 git repo 。但是每次检出一个 repo 时,前一个 repo(及其相关项目)都会被删除。

I have read this:

我读过这个:

http://jenkins.361315.n4.nabble.com/multiple-git-repos-in-one-job-td4633300.html

http://jenkins.361315.n4.nabble.com/multiple-git-repos-in-one-job-td4633300.html

but its does not really help. I have tried to specify the same folder under Local subdirectory for repo (optional)for all repos but it gives the same result.

但它并没有真正的帮助。我试图在本地子目录下为所有存储库指定相同的文件夹(可选),但它给出了相同的结果。

If this is simply impossible using Jenkins I guess some pre-build step/scripting could be used to move the projects into the right location. Its not an option to modify the build configuration of the projects.

如果使用 Jenkins 这根本不可能,我想可以使用一些预构建步骤/脚本将项目移动到正确的位置。它不是修改项目构建配置的选项。

采纳答案by CIGuy

Checking out more than one repo at a time in a single workspace is not possible with Jenkins + Git Plugin.

使用 Jenkins + Git 插件不可能在一个工作区中一次检出多个存储库。

As a workaround, you can either have multiple upstream jobs which checkout a single repo each and then copy to your final project workspace (Problematic on a number of levels), or you can set up a shell scripting step which checks out each needed repo to the job workspace at build time.

作为一种解决方法,您可以有多个上游作业,每个作业检出一个存储库,然后复制到您的最终项目工作区(在多个级别上有问题),或者您可以设置一个 shell 脚本步骤,将每个需要的存储库检出到构建时的作业工作区。

Previously the Multiple SCM plugin could help with this issue but it is now deprecated. From the Multiple SCM plugin page: "Users should migrate to https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin. Pipeline offers a better way of checking out of multiple SCMs, and is supported by the Jenkins core development team."

以前 Multiple SCM 插件可以帮助解决此问题,但现在已弃用。来自 Multiple SCM 插件页面:“用户应该迁移到https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin。Pipeline 提供了一种更好的检查多个 SCM 的方法,并且得到了 Jenkins 的支持核心开发团队。”

回答by GaRRaPeTa

With the Multiple SCMs Plugin:

使用多个 SCM 插件:

  • create a different repository entry for each repository you need to checkout (main project or dependancy project.

  • for each project, in the "advanced" menu (the second "advanced" menu, there are two buttons labeled "advanced" for each repository), find the "Local subdirectory for repo (optional)" textfield. You can specify there the subdirectory in the "workspace" directory where you want to copy the project to. You could map the filesystem of my development computer.

  • 为您需要检出的每个存储库(主项目或依赖项目)创建不同的存储库条目。

  • 对于每个项目,在“高级”菜单(第二个“高级”菜单,每个存储库有两个标记为“高级”的按钮)中,找到“本地子目录 repo(可选)”文本字段。您可以在“工作区”目录中指定要将项目复制到的子目录。您可以映射我的开发计算机的文件系统。

The "second advanced menu" doesn't exist anymore, instead what needs to be done is use the "Add" button (on the "Additional Behaviours" section), and choose "Check out to a sub-directory"

“第二个高级菜单”不再存在,需要做的是使用“添加”按钮(在“其他行为”部分),然后选择“签出到子目录”

  • if you are using ant, as now the build.xml file with the build targets in not in the root directory of the workspace but in a subdirectory, you have to reflect that in the "Invoke Ant" configuration. To do that, in "Invoke ant", press "Advanced" and fill the "Build file" input text, including the name of the subdirectory where the build.xml is located.
  • 如果您正在使用 ant,因为现在构建目标的 build.xml 文件不在工作区的根目录中,而是在子目录中,您必须在“调用 Ant”配置中反映这一点。为此,在“调用 ant”中,按“高级”并填写“构建文件”输入文本,包括 build.xml 所在子目录的名称。

Hope that helps.

希望有帮助。

回答by Joker

Since Multiple SCMs Pluginis deprecated.

由于多个 SCM 插件已被弃用。

With Jenkins Pipelineits possible to checkout multiple git repos and after building it using gradle

使用Jenkins Pipeline可以检查多个 git repos 并在使用 gradle 构建它之后

node {   
def gradleHome

stage('Prepare/Checkout') { // for display purposes
    git branch: 'develop', url: 'https://github.com/WtfJoke/Any.git'

    dir('a-child-repo') {
       git branch: 'develop', url: 'https://github.com/WtfJoke/AnyChild.git'
    }

    env.JAVA_HOME="${tool 'JDK8'}"
    env.PATH="${env.JAVA_HOME}/bin:${env.PATH}" // set java home in jdk environment
    gradleHome = tool '3.4.1' 
}

stage('Build') {
  // Run the gradle build
  if (isUnix()) {
     sh "'${gradleHome}/bin/gradle' clean build"
  } else {
     bat(/"${gradleHome}\bin\gradle" clean build/)
  }
}
}

You might want to consider using git submodulesinstead of a custom pipeline like this.

您可能需要考虑使用git 子模块而不是像这样的自定义管道。

回答by Frederik Deweerdt

I used the Multiple SCMs Pluginin conjunction with the Git Plugin successfully with Jenkins.

我在 Jenkins 上成功地将Multiple SCM 插件与 Git 插件结合使用。

回答by AKS

Jenkins:Multiple SCM - deprecated. GIT Plugin - doesn't work for multiple repos.

詹金斯:多个 SCM - 已弃用。GIT 插件 - 不适用于多个存储库。

Scripting / pipeline as code - is the way to go.

脚本/管道即代码 - 是要走的路。

回答by mikehwang

Depending upon the relationships of the repositories, another approach is to add the other repository (repositories) as a git submodulesto one of the repositories. A git submodule is creates a reference to the other repos. Those submodule repos are not cloned unless the you specify the --recursiveflag when cloning the "superproject"(official term).

根据存储库的关系,另一种方法是将另一个存储库(存储库)作为git 子模块添加到其中一个存储库中。git 子模块创建对其他存储库的引用。除非您--recursive在克隆“超级项目”(官方术语)时指定标志,否则不会克隆这些子模块存储库。

Here's the command to add a submodule into the current project:

这是将子模块添加到当前项目的命令:

git submodule add <repository URI path to clone>

git submodule add <repository URI path to clone>

We are using Jenkins v1.645 and the git SCM will out-of-the-box do a recursive clone for superprojects. Voila you get the superproject files and all the dependent (submodule) repo files in their own respective directories in the same Jenkins job workspace.

我们正在使用 Jenkins v1.645,并且 git SCM 将开箱即用地为超级项目执行递归克隆。瞧,您将在同一个 Jenkins 作业工作区的各自目录中获得超级项目文件和所有依赖(子模块)repo 文件。

Not vouching that this is the correctapproach rather it's an approach.

不保证这是正确的方法,而是一种方法。

回答by Torbj?rn Gard

I also had this problem. I solved it using Trigger/call builds on other projects. For each repository I call the downstream project using parameters.

我也有这个问题。我在其他项目上使用触发器/调用构建解决了它。对于每个存储库,我使用参数调用下游项目。

Main project:

主要项目:

This project is parameterized
String Parameters: PREFIX, MARKETNAME, BRANCH, TAG
Use Custom workspace: ${PREFIX}/${MARKETNAME}
Source code management: None

Then for each repository I call a downstream project like this:

然后对于每个存储库,我调用一个像这样的下游项目:

Trigger/call builds on other projects: 
Projects to build: Linux-Tag-Checkout
Current Build Parameters
Predefined Parameters: REPOSITORY=<name>

Downstream project: Linux-Tag-Checkout:

下游项目:Linux-Tag-Checkout:

This project is parameterized
String Parameters: PREFIX, MARKETNAME, REPOSITORY, BRANCH, TAG
Use Custom workspace:${PREFIX}/${MARKETNAME}/${REPOSITORY}-${BRANCH}
Source code management: Git
git@<host>:${REPOSITORY}
refspec: +refs/tags/${TAG}:refs/remotes/origin/tags/${TAG}
Branch Specifier: */tags/${TAG} 

回答by JRA_TLL

Checking out more than one repo at a time in a single workspace ispossible with Jenkins + Git Plugin (maybe only in more recent versions?).

在一个工作区检查了在同一时间超过一个回购协议可能的詹金斯+ Git的插件(也许只有在最近的版本?)。

In section "Source-Code-Management", do not select "Git", but "Multiple SCMs" and add several git repositories.

在“Source-Code-Management”部分,不要选择“Git”,而是选择“Multiple SCMs”并添加多个git存储库。

Be sure that in all but one you add as an "Additional behavior" the action "Check out to a sub-directory" and specify an individual subdirectory.

请确保在除一个之外的所有其他行为中添加“签出到子目录”操作并指定单个子目录作为“附加行为”。

回答by vladisld

We are using git-repoto manage our multiple GIT repositories. There is also a Jenkins Repo pluginthat allows to checkout all or part of the repositories managed by git-repo to the same Jenkins job workspace.

我们正在使用git-repo来管理我们的多个 GIT 存储库。还有一个Jenkins Repo 插件,它允许将 git-repo 管理的全部或部分存储库签出到同一个 Jenkins 作业工作区。