For the complete documentation index, see llms.txt. This page is also available as Markdown.

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_NAME with your GitHub account name.

  • REPO_NAME with the name of the GitHub repo to clone.

  • GH_PERSONAL_ACCESS_TOKEN with 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.

When you clone additional codebases that use the same credentials as your default codebase, you can use your default codebase connector's credentials in your Run step.

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.

How do I handle authentication for git commands in a Run step?

Store authentication credentials as secrets, and use expressions, such as <+secrets.getValue("YOUR_TOKEN_SECRET")>, to call them in your git commands.

You can also pull credentials from a git connector that you use elsewhere in the pipeline.

Can I use codebase variables when I clone a repo in a Run step or Git Clone step?

No. Harness resolves codebase variables only for the built-in Clone Codebase functionality. Harness does not resolve these variables for git commands in Run steps or for repos that Git Clone steps clone. Go to Unresolved variables for details.

Parallel container-less Git Clone steps cause intermittent build failures

Harness CI does not support container-less git steps in parallel. Running them in parallel can cause intermittent build failures. Keep your Git Clone steps in sequence when you use container-less build infrastructure. Go to Parallel container-less Git Clone steps are not supported for details.

Git connector fails to connect to the SCM service. SCM request fails with UNKNOWN

This error can occur if your code repo connector uses SSH authentication. To resolve this error, make sure HTTPS is enabled on port 443. The Harness connection test for Git connectors uses this protocol and port.

SCM service connection failures can also occur when you use self-signed certificates. Go to the Harness CI Knowledge Base for details.

Next steps

Last updated

Was this helpful?