2019-09-04 21:01:54 +05:30
---
2020-06-23 00:09:42 +05:30
stage: Verify
2021-09-04 01:27:46 +05:30
group: Pipeline Execution
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-09-04 21:01:54 +05:30
type: reference
---
2021-09-30 23:02:18 +05:30
# Using Git submodules with GitLab CI/CD **(FREE)**
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
Use [Git submodules ](https://git-scm.com/book/en/v2/Git-Tools-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.
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
## Configure the `.gitmodules` file
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
When you use Git submodules, your project should have a file named `.gitmodules` .
You might need to modify it to work in a GitLab CI/CD job.
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
For example, your `.gitmodules` configuration might look like the following if:
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
- Your project is located at `https://gitlab.com/secret-group/my-project` .
- 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 `git@gitlab.com:secret-group/my-project.git` .
2017-08-17 22:00:37 +05:30
```ini
[submodule "project"]
path = project
url = ../../group/project.git
```
2021-04-17 20:07:23 +05:30
When your submodule is on the same GitLab server, you should use relative URLs in
your `.gitmodules` file. Then you can clone with HTTPS in all your CI/CD jobs. You
can also use SSH for all your local checkouts.
The above configuration instructs Git to automatically deduce the URL to
use when cloning sources. Git uses the same configuration for both HTTPS and SSH.
GitLab CI/CD uses HTTPS for cloning your sources, and you can continue to use SSH
to clone locally.
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
For submodules not located on the same GitLab server, use the full URL:
2017-08-17 22:00:37 +05:30
```ini
[submodule "project-x"]
path = project-x
url = https://gitserver.com/group/project-x.git
```
2021-04-17 20:07:23 +05:30
## Use Git submodules in CI/CD jobs
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
To make submodules work correctly in CI/CD jobs:
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
1. Make sure you use [relative URLs ](#configure-the-gitmodules-file )
for submodules located in the same GitLab server.
1. You can set the `GIT_SUBMODULE_STRATEGY` variable to either `normal` or `recursive`
2021-09-04 01:27:46 +05:30
to tell the runner to [fetch your submodules before the job ](runners/configure_runners.md#git-submodule-strategy ):
2019-09-30 21:07:59 +05:30
```yaml
variables:
GIT_SUBMODULE_STRATEGY: recursive
```
2022-01-26 12:08:38 +05:30
2021-12-11 22:18:48 +05:30
If you use the [`CI_JOB_TOKEN` ](jobs/ci_job_token.md ) to clone a submodule in a
pipeline job, the user executing the job must be assigned to a role that has
[permission ](../user/permissions.md#gitlab-cicd-permissions ) to trigger a pipeline
in the upstream submodule project.