带有两个 git 存储库的 Jenkinsfile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37395860/
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-09 04:11:45  来源:igfitidea点击:

Jenkinsfile with two git repositories

gitjenkinsjenkins-pipelinejenkinsfile

提问by Mark Chorley

I'm using the Jenkins pipeline plugin with a Jenkinsfile.

我正在使用带有 Jenkinsfile 的 Jenkins 管道插件。

In one repository, called vms.git, I have the Jenkinsfile and an application it builds.

在一个名为 vms.git 的存储库中,我有 Jenkinsfile 和它构建的应用程序。

I have another repository called deploy.git, which contains scripts I want to use to deploy the application in vms.git.

我有另一个名为 deploy.git 的存储库,其中包含我想用来在 vms.git 中部署应用程序的脚本。

At the moment my Jenkinsfile just looks like this

目前我的 Jenkinsfile 看起来像这样

node {
  stage 'build'
  checkout scm

and I am defining the vms.git repo in the job configuration.

我在作业配置中定义了 vms.git 存储库。

So what I would like to do is check out both repositories, then use the Jenkinsfile in vms.git to define the rest of the build. I want to reuse the deploy.git scripts in other pipelines so I don't want to put a Jenkinsfile in there.

所以我想做的是检查两个存储库,然后使用 vms.git 中的 Jenkinsfile 来定义构建的其余部分。我想在其他管道中重用 deploy.git 脚本,所以我不想在那里放一个 Jenkinsfile。

回答by krynio

You can checkout multiple directories using checkout, but you have to specify directory where you want checkout this. You can generate snippets using jenkins (Snippet generator bellow script field). Choose checkout, next git repository and in Additional Behaviours choose: checkout into sub directory.

您可以使用 签出多个目录checkout,但必须指定要签出此的目录。您可以使用 jenkins(代码段生成器波纹管脚本字段)生成代码段。选择 checkout,next git repository 并在 Additional Behaviors 中选择:checkout into sub directory。

When you will have 2 repositories you can load script from repository you want usin load. Example:

当您拥有 2 个存储库时,您可以从您想要使用的存储库加载脚本load。例子:

node {
    // first repository
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory1']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo1.git']]])
    // second repository
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'subdirectory2']], submoduleCfg: [], userRemoteConfigs: [[url: 'repo2.git']]])
    // run first script
    load 'subdirectory1/Jenkinsfile'
    // run second script
    load 'subdirectory2/Jenkinsfile'
}

回答by czerwin

Another elegant solution for handling multiple Git repositories within single pipeline can be found at this thread.

在这个线程中可以找到单个管道中处理多个 Git 存储库的另一个优雅的解决方案。

node {
    dir('RepoOne') {
        git url: 'https://github.com/somewhere/RepoOne.git'
    }
    dir('RepoTwo') {
        git url: 'https://github.com/somewhere/RepoTwo.git'
    }

    sh('. RepoOne/build.sh')
    sh('. RepoTwo/build.sh')
}