首页 > 解决方案 > 如何在 jenkins groovy 中签出 SCM 的子目录?

问题描述

我有一个 git hub 存储库 ABC.git 它有以下目录 src ,config,env,test。我只想将 src 和 config 文件夹检出到我的 Jenkins 工作区中。

我可以使用 SCM 插件将完整的存储库签出到我的 jenkins 工作区中。

用于检查完整分支的命令:

checkout([
        $class: 'GitSCM', 
        branches: [[name: '*/master']], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'CleanCheckout']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
    ])

标签: jenkins-pipelinejenkins-groovy

解决方案


使用它来检查特定目录/子目录:extensions: [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [[path: '/directory/path/here']]]]

因此,您的脚本将如下所示:

checkout([
    $class: 'GitSCM',
    branches: [[name: '*/master']],
    doGenerateSubmoduleConfigurations: false, 
    extensions: [[$class: 'CleanCheckout'],
    [$class: 'SparseCheckoutPaths',
    sparseCheckoutPaths: [[path: '/directory/path/here']]]],
    submoduleCfg: [],
    userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
])

推荐阅读