Using Git Submodules With Gitlab CI/CD
We use Git submodules to keep a Git repository as a subdirectory of another Git repo. This is usually done for a set of common files you want to refer to in multiple repos.
Join the DZone community and get the full member experience.Join For Free
We use Git submodules to keep a Git repository as a subdirectory of another Git repository. This is usually done when you have a set of common files you want to refer to in multiple repos.
In my personal experience, I found git submodules especially useful when working with Terraform.
When you use Git submodules, your project should have a file named
.gitmodules, you need to use the
git submodule command. There are two ways you can use it:
- If the repo you want to use as a submodule exists on a relative path, i.e., on the same GitLab server, then use the following command:
- For submodules not located on the same GitLab server, use the full URL:
Both the above commands should be executed from within the project in which you want to use '
project-y' as a submodule.
Updating gitlab-ci.yml, Use Git Submodules in CI/CD Jobs
To make submodules work correctly in CI/CD jobs:
- Make sure you use relative URLs for submodules located in the same GitLab server.
You can set the
GIT_SUBMODULE_STRATEGYvariable to either
In the variables section of your gitlab-ci file, add the following:
We used the recursive option so that all folders inside submodules are checked out.
Next, in the default section of our gitlab-ci file, we had to add the following to make sure the latest version of submodules is checked out on every execution:
Adding tags, setting '
sslVerify' is optional, but we had to add submodule init and update with
--recursive --remote to make sure submodules work properly.
Once the above lines were added, submodules for us worked seamlessly in GitLab.
Opinions expressed by DZone contributors are their own.