Jenkinsfile管道中的SparseCheckout

时间:2017-04-08 11:13:22

标签: jenkins groovy jenkins-pipeline

在jenkins文件中,我通过 SparseCheckoutPaths 指定了我想要签出的folderName。但我得到了一个完整的分支结账。

   checkout([$class: 'GitSCM', 
       branches: [[name: '*/branchName']],
       extensions: [[$class: 'SparseCheckoutPaths', path: 'FolderName']],
       userRemoteConfigs: [[credentialsId: 'someID',
       url: 'git@link.git']]])

3 个答案:

答案 0 :(得分:8)

这是我自己的问题的答案。对于一些背景知识它是如何工作的,git客户端有一个名为 sparsecheckout 的标志/配置,它负责这种结账。此外,还需要稀疏结帐命名文件。有关详细信息,请查看here

我的问题是 Jenkinsfile 的语法,正确的如下:

checkout([$class: 'GitSCM', 
    branches: [[name: '*/branchName']],
    doGenerateSubmoduleConfigurations: false,
    extensions: [
        [$class: 'SparseCheckoutPaths',  sparseCheckoutPaths:[[$class:'SparseCheckoutPath', path:'folderName/']]]
                ],
    submoduleCfg: [],
    userRemoteConfigs: [[credentialsId: 'someID',
    url: 'git@link.git']]])

了解更多信息,github-link

答案 1 :(得分:1)

您的语法看起来不错,但是,如“jenkinsci/plugins/gitclient/CliGitAPIImpl.java”中所示,您是否指定了正确的配置?

private void sparseCheckout(@NonNull List<String> paths) throws GitException, InterruptedException {

    boolean coreSparseCheckoutConfigEnable;
    try {
        coreSparseCheckoutConfigEnable = launchCommand("config", "core.sparsecheckout").contains("true");
    } catch (GitException ge) {
        coreSparseCheckoutConfigEnable = false;
    }

换句话说,在您要结账的仓库中,git config core.sparsecheckout等于是吗?

答案 2 :(得分:1)

您可以在共享库中定义自定义步骤checkout scm,该共享库将添加到现有vars/sparseCheckout.groovy的顶部。

def call(scm, files) { if (scm.class.simpleName == 'GitSCM') { def filesAsPaths = files.collect { [path: it] } return checkout([$class : 'GitSCM', branches : scm.branches, doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations, extensions : scm.extensions + [[$class: 'SparseCheckoutPaths', sparseCheckoutPaths: filesAsPaths]], submoduleCfg : scm.submoduleCfg, userRemoteConfigs : scm.userRemoteConfigs ]) } else { // fallback to checkout everything by default return checkout(scm) } }

sparseCheckout(scm, ['path/to/file.xml', 'path2])

然后用以下方式调用它:

export default class ButtonGroup extends Component {
constructor(props) {
super(props);
this.state = {
  selected: null
};
}
handleClick = e => {
this.setState({
  selected: e.target.name
});
};
render() {
const selected = this.state.selected;
return (
  <>
    <button
      name="1"
      style={{ backgroundColor: selected == 1 ? "red" : "blue" }}
      onClick={this.handleClick}
    />
    <button
      name="2"
      style={{ backgroundColor: selected == 2 ? "red" : "blue" }}
      onClick={this.handleClick}
    />
    <button
      name="1"
      style={{ backgroundColor: selected == 3 ? "red" : "blue" }}
      onClick={this.handleClick}
    />
  </>
 );
 }
 }