How to specify branch in .gitlab-ci.yml for a submodule (different repo) in gitlab-ci?
2 Answers
You don't. You specify it in the .gitmodules
file of the project you are building.
[submodule "MyRepo"]
path = MyRepo
url = https://github.com/vendor/MyRepo.git
branch = master

- 4,330
- 23
- 25
-
This does't work for me. I have this in my `.gitsubmodules` file, but when i push to gitlab the most recent commit of any branch is used – Zach Smith Sep 02 '19 at 07:31
-
1@ZachSmith the file should be called `.gitmodules` – Stefan van Gastel Sep 03 '19 at 08:14
-
Yes.... indeed it should (and is). That was a typo in the question. Apologies. I've added an answer below – Zach Smith Sep 03 '19 at 11:15
Along with @stefan's answer to this question. You also have to tell Git to only look at the specified branch for the latest commit. git submodule update
seems to always fetch the latest commit regardless of the branch. Doing git submodule update --remote
seems to force git to focus on the branch you specify in the .gitmodules
file.
So in your .gitmodules
files, as @stefen mentions:
[submodule "MyRepo"]
path = MyRepo
url = https://github.com/vendor/MyRepo.git
branch = master
Then in your GitLab .gitlab-ci.yml
file, you need to specify to only look at the configured branch when setting up submodules. My file includes this before_script
:
# GitLab CI provides a variable "GIT_SUBMODULE_STRATEGY" that sets up submodules automatically
# But then branch tracking doesn't work (doesn't seem to allow for specifying the --remote) flag
before_script:
- git submodule sync --recursive
- git submodule update --init --remote --recursive
According to documentation, this before_script
is the same functionality as provided by GIT_SUBMODULE_STRATEGY
(except that I can add the --remote
flag to the before_script
): https://docs.gitlab.com/ee/ci/yaml/README.html#git-submodule-strategy

- 8,458
- 13
- 59
- 133