在 Jenkins 中,如何将项目检出到特定目录(使用 GIT)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9767919/
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
In Jenkins, how to checkout a project into a specific directory (using GIT)
提问by viebel
Sorry for the 'svn' style - we are in a process of migration from SVN to GIT (including our CI Jenkins environment).
很抱歉'svn' 风格 - 我们正在从 SVN 迁移到 GIT(包括我们的 CI Jenkins 环境)。
What we need is to be able to make Jenkins to checkout (or should I say clone?) the GIT project (repository?) into a specific directory. We've tried some refspecs magic but it wasn't too obvious to understand and use successfully.
我们需要的是能够让 Jenkins 检出(或者我应该说是克隆?)GIT 项目(存储库?)到特定目录中。我们已经尝试了一些 refspecs 魔法,但理解和成功使用并不是很明显。
Furthermore, if in the same Jenkins project we need to checkout several private GitHub repositories into several separate dirs under a project root, how can we do it please?
此外,如果在同一个 Jenkins 项目中我们需要将多个私有 GitHub 存储库签出到项目根目录下的几个单独的目录中,我们该怎么做?
We have GitHub plugin installed. Hope we've phrased the things right.
我们已经安装了 GitHub 插件。希望我们的措辞是正确的。
采纳答案by ?ukasz R?anek
The default git pluginfor Jenkins does the job quite nicely.
Jenkins的默认git 插件可以很好地完成这项工作。
After adding a new git repository (project configuration > Source Code Management> check the GIT option) to the project navigate to the bottom of the plugin settings, just above Repository browserregion. There should be an Advancedbutton. After clicking it a new form should appear, with a value described as Local subdirectory for repo (optional). Setting this to folder
will make the plugin to check out the repository into the folder relative to your workspace. This way you can have as many repositories in your project as you need, all in separate locations.
在向项目添加新的 git 存储库(项目配置 >源代码管理> 检查 GIT 选项)后,导航到插件设置的底部,就在存储库浏览器区域的上方。应该有一个高级按钮。单击它后,应出现一个新表单,其值描述为本地子目录 repo (optional)。将此设置为folder
将使插件将存储库检出到相对于您的工作区的文件夹中。通过这种方式,您可以根据需要在项目中拥有任意数量的存储库,所有存储库都位于不同的位置。
Alternatively, if the project you're using will allow that, you can use GIT sub modules, which are similar to external paths in SVN. In the GIT Bookthere is a section on that very topic. If that will not be against some policy, submodules are fairly simple to use, giving you powerful way to control the locations, versions/tags/branches that will be imported AND it will be available on your local repository as well giving you better portability.
或者,如果您使用的项目允许这样做,您可以使用 GIT 子模块,它们类似于 SVN 中的外部路径。在GIT Book 中有一个关于这个主题的部分。如果这不会违反某些政策,子模块使用起来相当简单,为您提供了强大的方式来控制将要导入的位置、版本/标签/分支,并且它将在您的本地存储库中可用,并为您提供更好的可移植性。
Obviously the GIT plugin supports checking out submodules, so Jenkins can work with them quite effectively.
显然,GIT 插件支持检出子模块,因此 Jenkins 可以非常有效地使用它们。
回答by biolinh
I agree with @?ukasz R?anek that we can use git plugin
我同意 @?ukasz R?anek 我们可以使用git 插件
But, I use option: checkout to a sub-direction what is enable as follow:
In Source Code Management, tick Git
click add button, choose checkout to a sub-directory
但是,我使用 option: checkout to a sub-direction 启用如下:
在Source Code Management 中,勾选 Git
单击添加按钮,选择checkout to a sub-directory
回答by John McGehee
In the new Jenkins 2.0 pipeline (previously named the Workflow Plugin), this is done differently for:
在新的 Jenkins 2.0 管道(以前称为工作流插件)中,这与以下方面不同:
- The main repository
- Other additional repositories
- 主存储库
- 其他附加存储库
Here I am specifically referring to the Multibranch Pipeline version 2.9.
这里我特指的是 Multibranch Pipeline 2.9 版。
Main repository
主存储库
This is the repository that contains your Jenkinsfile
.
这是包含您的Jenkinsfile
.
In the Configurescreen for your pipeline project, enter your repository name, etc.
在管道项目的配置屏幕中,输入您的存储库名称等。
Do not use Additional Behaviors > Check out to a sub-directory. This will put your Jenkinsfile
in the sub-directory where Jenkins cannot find it.
不要使用“附加行为”>“签出到子目录”。这会将您Jenkinsfile
放在Jenkins 找不到的子目录中。
In Jenkinsfile
, check out the main repository in the subdirectory using dir()
:
在 中Jenkinsfile
,使用以下命令检查子目录中的主存储库dir()
:
dir('subDir') {
checkout scm
}
Additional repositories
附加存储库
If you want to check out more repositories, use the Pipeline Syntaxgenerator to automatically generate a Groovy code snippet.
如果您想查看更多存储库,请使用流水线语法生成器自动生成 Groovy 代码片段。
In the Configurescreen for your pipeline project:
在管道项目的配置屏幕中:
- Select Pipeline Syntax. In the Sample Stepdrop down menu, choose checkout: General SCM.
- Select your SCM system, such as Git. Fill in the usual information about your repository or depot.
- Note that in the Multibranch Pipeline, environment variable
env.BRANCH_NAME
contains the branch name of the main repository. - In the Additional Behaviorsdrop down menu, select Check out to a sub-directory
- Click Generate Groovy. Jenkins will display the Groovy code snippet corresponding to the SCM checkout that you specified.
- Copy this code into your pipeline script or
Jenkinsfile
.
- 选择管道语法。在Sample Step下拉菜单中,选择checkout: General SCM。
- 选择您的 SCM 系统,例如Git。填写有关您的存储库或仓库的常用信息。
- 请注意,在 Multibranch Pipeline 中,环境变量
env.BRANCH_NAME
包含主存储库的分支名称。 - 在“附加行为”下拉菜单中,选择“ 签出到子目录”
- 单击生成 Groovy。Jenkins 将显示与您指定的 SCM 结账对应的 Groovy 代码片段。
- 将此代码复制到您的管道脚本或
Jenkinsfile
.
回答by Jeremy Whiting
It's worth investigating the Pipeline plugin. With the plugin you can checkout multiple VCS projects into relative directory paths. Beforehand creating a directory per VCS checkout. Then issue commands to the newly checked out VCS workspace. In my case I am using git. But you should get the idea.
值得研究 Pipeline 插件。使用该插件,您可以将多个 VCS 项目检出到相对目录路径中。预先为每个 VCS 签出创建一个目录。然后向新检出的 VCS 工作区发出命令。就我而言,我使用的是 git。但你应该明白。
node{
def exists = fileExists 'foo'
if (!exists){
new File('foo').mkdir()
}
dir ('foo') {
git branch: "<ref spec>", changelog: false, poll: false, url: '<clone url>'
......
}
def exists = fileExists 'bar'
if (!exists){
new File('bar').mkdir()
}
dir ('bar') {
git branch: "<ref spec>", changelog: false, poll: false, url: '<clone url>'
......
}
def exists = fileExists 'baz'
if (!exists){
new File('baz').mkdir()
}
dir ('baz') {
git branch: "<ref spec>", changelog: false, poll: false, url: '<clone url>'
......
}
}
回答by Alberto
Furthermore, if in the same Jenkins project we need to checkout several private GitHub repositories into several separate dirs under a project root. How can we do it please?
此外,如果在同一个 Jenkins 项目中,我们需要将几个私有的 GitHub 存储库签出到项目根目录下的几个单独的目录中。请问我们怎么办?
The Jenkin's Multiple SCMs Pluginhas solved the several repositories problem for me very nicely. I have just got working a project build that checks out four different git repos under a common folder. (I'm a bit reluctant to use git super-projects as suggested previouslyby ?ukasz R?anek, as git is complex enough without submodules.)
在詹的多的SCM插件已经解决了几个问题库对我来说非常漂亮。我刚刚开始了一个项目构建,它在一个公共文件夹下检查了四个不同的 git 存储库。(我有点不愿意使用?ukasz R?anek之前建议的git 超级项目,因为 git 没有子模块就足够复杂了。)
回答by Tim
I do not use github plugin, but from the introduction page, it is more or less like gerrit-trigger plugin.
我没有使用github插件,但从介绍页面来看,它或多或少有点像gerrit-trigger插件。
You can install git plugin, which can help you checkout your projects, if you want to include multi-projects in one jenkins job, just add Repository into your job.
您可以安装 git 插件,它可以帮助您检查您的项目,如果您想在一个 jenkins 作业中包含多个项目,只需将 Repository 添加到您的作业中。