Clone multiple code repos in one pipeline
Use Git Clone, Run, or Plugin steps to clone additional code repos into a pipeline's workspace.
In addition to the pipeline's default codebase, you can use the Git Clone step to clone additional code repos into the pipeline's workspace.
For example, you can use this capability to:
Build multiple artifacts in the same pipeline. Suppose you use Packer and Ansible to build artifacts automatically, and you have separate repositories for Packer, Ansible, and code. You can clone all three repositories into the pipeline's workspace.
Pull code from separate code and build repositories. For instance, if the Engineering team manages your code files in one repository and the Security team manages your Dockerfiles in a different repository, you can clone both repositories into the pipeline's workspace.
Clone codebases without using the built-in clone codebase function.
What you will learn in this topic
By the end of this topic, you will learn how to:
Add a Git Clone step or a Run step to clone a second repo into the pipeline's workspace.
Build an artifact from files in both repos.
Review YAML examples for each approach.
Use a Plugin step as an alternative.
Troubleshoot common issues when you clone multiple repos.
Before you begin
Before you clone multiple code repositories in one pipeline, make sure you have a CI pipeline with a Build stage and a default codebase.
Add a Git clone step
Assume the default codebase is a repo that contains app code files, and the Dockerfile that builds the app image is in a different repo. You can use a Git Clone step to clone the second repo into the workspace, or to fetch specific files or directories. Then, you can use a Build and Push step to build and push an image with files from both repos.
Add a Git Clone step to clone a second repo into the pipeline's workspace.
Go to Git Clone step for details.
Add a run step
Use git commands in Run steps to clone multiple repos into a stage. You can also provide arguments to clone subdirectories, clone recursively, and so on.
For example, this step clones a GitHub repository:
To use this command, replace:
ACCOUNT_NAMEwith your GitHub account name.REPO_NAMEwith the name of the GitHub repo to clone.GH_PERSONAL_ACCESS_TOKENwith a GitHub personal access token that has pull permissions to the target repository. Additional permissions might be necessary depending on the purpose of your step. Store the token as a Harness secret and use a variable expression, such as<+secrets.getValue("YOUR_TOKEN_SECRET")>, to call it.
Go to the Run scripts tab on the Run step settings page for details about the Run step settings.
Build an artifact from both code repos
After Harness clones the files from both repos into a common workspace, add a step that builds an image with code from both repos, such as a Build and Push to Docker step.
Pay attention to settings, such as the Dockerfile setting, that assume files are in the codebase's root directory unless you specify otherwise. The pipeline's default codebase files are cloned into the root folder (/harness), while other codebase files are cloned into subfolders.
Depending on the default codebase, you might need to specify a non-root path for build files. You can also use commands, such as cp, in Run steps to move cloned files around the workspace before you build the image.
YAML examples
The following YAML example describes a pipeline that clones two code repos. The pipeline clones one as the default codebase (cloneCodebase: true) and the second in the GitClone step.
This example clones app code as the default codebase and then uses a Run step to clone a separate repo that has the Dockerfile that builds the app.
Because of the build infrastructure and the image that the Run step uses, you must install Git before you clone the additional repo. The Run step uses the following commands to install Git, verify that it works, and then clone the repo that has the Dockerfile that builds the app.
This example also uses stage variables, such as $GITHUB_USERNAME, to reference account and repo names. All steps in the stage can access these variables.
Use a plugin step
As an alternative to the Git Clone and Run steps, you can clone a codebase by running the Git Drone plugin in a Plugin step.
Troubleshooting
Go to the Harness CI Knowledge Base for more common questions and issues related to codebases.
Next steps
Git Clone step in CI: Review all Git Clone step settings.
Clone a subdirectory: Clone a subdirectory instead of an entire repo.
CI codebase variables reference: Reference Git codebase attributes with built-in expressions.
Last updated
Was this helpful?