Jenkinsfile 中包含两个 Git 仓库

26

我正在使用Jenkins流水线插件与Jenkinsfile。

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

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

目前我的Jenkinsfile只长成这样

node {
  stage 'build'
  checkout scm

我正在工作配置中定义vms.git存储库。

因此,我想要做的是检出这两个存储库,然后使用vms.git中的Jenkinsfile来定义其余的构建过程。我希望在其他流水线中重用deploy.git脚本,所以我不想在那里放置Jenkinsfile。

2个回答

37

您可以使用checkout检出多个目录,但必须指定要检出到哪个目录。您可以使用Jenkins生成片段(在脚本字段下方的片段生成器中)。

选择checkout,接下来是git仓库,在“附加行为”中选择:检出到子目录中。

当您有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'
}

1
*/master 的含义在 Jenkinsfile dsl 参考文档中有更详细的解释,网址为 http://jenkinshost:8080/workflow-cps-snippetizer/dslReference - Linus Arver

30

在这个帖子中,可以找到处理单个流水线内多个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')
}

1
如果 https://github.com/somewhere/RepoOne.git 受到保护,你如何将凭据传递给命令 "git url: 'https://github.com/somewhere/RepoOne.git'"? - Kappacake
7
我建议您使用Jenkins凭据,并以以下方式传递它们:git url: 'https://github.com/somewhere/RepoOne.git',credentialsId: 'xxxxx-xxxx-xxxx-xxxx-xxxxx' - czerwin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接