简体   繁体   English

在 Jenkins 中,如何将项目检出到特定目录(使用 GIT)

[英]In Jenkins, how to checkout a project into a specific directory (using GIT)

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.我们需要的是能够让 Jenkins 检出(或者我应该说是克隆?)GIT 项目(存储库?)到特定目录中。 We've tried some refspecs magic but it wasn't too obvious to understand and use successfully.我们已经尝试了一些 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.我们已经安装了 GitHub 插件。 Hope we've phrased the things right.希望我们的措辞是正确的。

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 Configure screen 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 Syntax generator to automatically generate a Groovy code snippet.如果您想查看更多存储库,请使用流水线语法生成器自动生成 Groovy 代码片段。

In the Configure screen for your pipeline project:在管道项目的配置屏幕中:

  1. Select Pipeline Syntax .选择管道语法 In the Sample Step drop down menu, choose checkout: General SCM .Sample Step下拉菜单中,选择checkout: General SCM
  2. Select your SCM system, such as Git .选择您的 SCM 系统,例如Git Fill in the usual information about your repository or depot.填写有关您的存储库或软件仓库的常用信息。
  3. Note that in the Multibranch Pipeline, environment variable env.BRANCH_NAME contains the branch name of the main repository.请注意,在 Multibranch Pipeline 中,环境变量env.BRANCH_NAME包含主存储库的分支名称。
  4. In the Additional Behaviors drop down menu, select Check out to a sub-directory在“附加行为”下拉菜单中,选择“签出到子目录”
  5. Click Generate Groovy .单击生成 Groovy Jenkins will display the Groovy code snippet corresponding to the SCM checkout that you specified. Jenkins 将显示与您指定的 SCM 结账对应的 Groovy 代码片段。
  6. Copy this code into your pipeline script or Jenkinsfile .将此代码复制到您的管道脚本或Jenkinsfile

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:但是,我使用 option: checkout to a sub-direction 启用如下:
In Source Code Management , tick GitSource Code Management 中,勾选 Git
click add button, choose checkout to a sub-directory单击添加按钮,选择结帐到子目录

在此处输入图片说明

The default git plugin for 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 browser region.在向项目添加新的 git 存储库(项目配置 >源代码管理> 检查 GIT 选项)后,导航到插件设置的底部,就在存储库浏览器区域的上方。 There should be an Advanced button.应该有一个高级按钮。 After clicking it a new form should appear, with a value described as Local subdirectory for repo (optional) .单击它后,应出现一个新表单,其值描述为本地子目录 repo (optional) Setting this to folder will make the plugin to check out the repository into the folder relative to your workspace.将此设置为folder将使插件将存储库检出到相对于您的工作区的文件夹中。 This way you can have as many repositories in your project as you need, all in separate locations.通过这种方式,您可以根据需要在项目中拥有任意数量的存储库,所有存储库都位于不同的位置。

Alternatively, if the project you're using will allow that, you can use GIT sub modules, which are similar to external paths in SVN.或者,如果您使用的项目允许这样做,您可以使用 GIT 子模块,它们类似于 SVN 中的外部路径。 In the GIT Book there is a section on that very topic.GIT Book 中有一个关于这个主题的部分。 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.如果这不会违反某些政策,子模块使用起来相当简单,为您提供了强大的方式来控制将要导入的位置、版本/标签/分支,并且它将在您的本地存储库中可用,并为您提供更好的可移植性。

Obviously the GIT plugin supports checking out submodules, so Jenkins can work with them quite effectively.显然,GIT 插件支持检出子模块,因此 Jenkins 可以非常有效地使用它们。

It's worth investigating the Pipeline plugin.值得研究 Pipeline 插件。 With the plugin you can checkout multiple VCS projects into relative directory paths.使用该插件,您可以将多个 VCS 项目检出到相对目录路径中。 Beforehand creating a directory per VCS checkout.预先为每个 VCS 签出创建一个目录。 Then issue commands to the newly checked out VCS workspace.然后向新检出的 VCS 工作区发出命令。 In my case I am using git.就我而言,我使用的是 git。 But you should get the idea.但你应该明白。

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>'
        ......
    }
}

Furthermore, if in the same Jenkins project we need to checkout several private GitHub repositories into several separate dirs under a project root.此外,如果在同一个 Jenkins 项目中,我们需要将几个私有的 GitHub 存储库签出到项目根目录下的几个单独的目录中。 How can we do it please?请问我们怎么办?

The Jenkin's Multiple SCMs Plugin has solved the several repositories problem for me very nicely. Jenkin 的多个 SCM 插件很好地为我解决了几个存储库问题。 I have just got working a project build that checks out four different git repos under a common folder.我刚刚开始了一个项目构建,它在一个公共文件夹下检查了四个不同的 git 存储库。 (I'm a bit reluctant to use git super-projects as suggested previously by Łukasz Rżanek, as git is complex enough without submodules.) (我有点不愿意使用 Łukasz Rżanek 之前建议的git 超级项目,因为 git 没有子模块就足够复杂了。)

Find repoName from the url, and then checkout to the specified directory.从url中找到repoName,然后checkout到指定目录。

String url = 'https://github.com/foo/bar.git';
String[] res = url.split('/');
String repoName = res[res.length-1];
if (repoName.endsWith('.git')) repoName=repoName.substring(0, repoName.length()-4);

checkout([
  $class: 'GitSCM', 
  branches: [[name: 'refs/heads/'+env.BRANCH_NAME]],
  doGenerateSubmoduleConfigurations: false,
  extensions: [
    [$class: 'RelativeTargetDirectory', relativeTargetDir: repoName],
    [$class: 'GitLFSPull'],
    [$class: 'CheckoutOption', timeout: 20],
    [$class: 'CloneOption',
        depth: 3,
        noTags: false,
        reference: '/other/optional/local/reference/clone',
        shallow: true,
        timeout: 120],
    [$class: 'SubmoduleOption', depth: 5, disableSubmodules: false, parentCredentials: true, recursiveSubmodules: true, reference: '', shallow: true, trackingSubmodules: true]
  ],
  submoduleCfg: [],
  userRemoteConfigs: [
    [credentialsId: 'foobar',
    url: url]
  ]
])

just add "dir wrapper" to the git operation:只需在 git 操作中添加“dir wrapper”:

dir('subDir') {
    checkout scm
}

In the new Pipeline flow, following image may help ..在新的流水线流程中,下图可能会有所帮助..

在此处输入图片说明

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 添加到您的作业中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM