Skip to main content

Clone a subdirectory

Sparse Checkout support with 'Clone Codebase' or 'Git Clone' step.

When you configure a codebase, you can specify a sparse checkout to clone a subdirectory instead of the entire repo.

Similarly, you can use the built-in git clone step to clone a subdirectory instead of the entire repo.

note

The feature is behind feature flag CI_GIT_CLONE_ENHANCED. If it is not available in your account, contact Harness Support to enable the feature.

The following example shows how to use the sparseCheckout option to clone the docs/continuous-integration and docs/code-repository subdirectories from the Harness Developer Hub code repository.

pipeline:
name: git_clone_pipeline
identifier: git_clone_pipeline
projectIdentifier: default_project
orgIdentifier: default
tags: {}
stages:
- stage:
name: clone_subdirectory_example
identifier: clone_subdirectory_example
description: ""
type: CI
spec:
cloneCodebase: true
caching:
enabled: true
paths: []
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
execution:
steps:
- step:
type: Run
name: run_step
identifier: run_step
spec:
shell: Sh
command: ls -la docs
properties:
ci:
codebase:
connectorRef: github_connector
repoName: harness/developer-hub
build: <+input>
sparseCheckout:
- docs/continuous-integration
- docs/code-repository

Clone a subdirectory using a Run step

You can disable the pipeline's default codebase and then run your git commands in a Run step.

You can also use this pattern if you need to use specific git clone arguments to clone your codebase.

warning

The following section explains how to use a Run step to run specific git clone arguments (such as --recursive or sparse-checkout) instead of using the built-in clone codebase or git clone step functionality.

Disabling the built-in clone step removes access to some associated functionality, such as PR status updates or resolution of <+codebase.*> expressions.

Determine if you need to disable Clone Codebase

  • Clone with my git commands instead of using the built-in clone step: If Clone Codebase is enabled, then the build clones the pipeline's default codebase automatically. If you don't want to clone the default codebase, you must disable Clone Codebase so that you can clone the repo with your desired git commands.
  • Clone with my git commands and use the built-in clone step: If you want to clone files from another repo in addition to the default codebase, then do not disable Clone Codebase. For this pattern, follow the instructions in Clone multiple code repos in one pipeline and use the Run step option.

Add a Run step with git commands

Add a Run step containing your desired git commands, such as git sparse-checkout to clone a subdirectory instead of an entire repo.

              - step:
type: Run
identifier: clone
name: clone
spec:
shell: Sh
command: |-
git sparse-checkout ...
tip

Store tokens and passwords as Harness secrets and use variable expressions, such as <+secrets.getValue("YOUR_TOKEN_SECRET")>, to call them in your commands.