Using Git submodules with GitLab CI/CD (FREE ALL)
Use Git submodules to keep a Git repository as a subdirectory of another Git repository. You can clone another repository into your project and keep your commits separate.
When you use Git submodules, your project should have a file named
You have multiple options to configure it to work in a GitLab CI/CD job.
Using absolute URLs
Introduced in GitLab Runner 15.11.
For example, your generated
.gitmodules configuration might look like the following if:
- Your project is located at
- Your project depends on
https://gitlab.com/group/project, which you want to include as a submodule.
- You check out your sources with an SSH address like
path = project
url = email@example.com:secret-group/project.git
In this case, use the
to instruct GitLab Runner to convert the URL to HTTPS before it clones the submodules.
Alternatively, if you also use HTTPS locally, you can configure an HTTPS URL:
path = project
url = https://gitlab.com/secret-group/project.git
You do not need to configure additional variables in this case, but you need to use a personal access token to clone it locally.
Using relative URLs
WARNING: If you use relative URLs, submodules may resolve incorrectly in forking workflows. Use absolute URLs instead if you expect your project to have forks.
When your submodule is on the same GitLab server, you can also use relative URLs in
path = project
url = ../../project.git
The above configuration instructs Git to automatically deduce the URL to use when cloning sources. You can clone with HTTPS in all your CI/CD jobs, and you can continue to use SSH to clone locally.
For submodules not located on the same GitLab server, always use the full URL:
path = project-x
url = https://gitserver.com/group/project-x.git
Use Git submodules in CI/CD jobs
To make submodules work correctly in CI/CD jobs:
You can set the
GIT_SUBMODULE_STRATEGYvariable to either
recursiveto tell the runner to fetch your submodules before the job:
variables: GIT_SUBMODULE_STRATEGY: recursive
For submodules located on the same GitLab server and configured with a Git or SSH URL, make sure you set the
GIT_SUBMODULE_DEPTHto configure the cloning depth of submodules independently of the
variables: GIT_SUBMODULE_DEPTH: 1
You can filter or exclude specific submodules to control which submodules are synchronized using
variables: GIT_SUBMODULE_PATHS: submoduleA submoduleB
You can provide additional flags to control advanced checkout behavior using
variables: GIT_SUBMODULE_STRATEGY: recursive GIT_SUBMODULE_UPDATE_FLAGS: --jobs 4
If you use the
CI_JOB_TOKEN to clone a submodule in a
pipeline job, the user executing the job must be assigned to a role that has
permission to trigger a pipeline
in the upstream submodule project. Additionally, CI/CD job token access must be properly configured in the upstream submodule project.
Can't find the
.gitmodules file might be hard to find because it is usually a hidden file.
You can check documentation for your specific OS to learn how to find and display
If there is no
.gitmodules file, it's possible the submodule settings are in a
git config file.
fatal: run_command returned non-zero status error
This error can happen in a job when working with submodules and the
GIT_STRATEGY is set to
clone should resolve the issue.