DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Streamlining AWS Lambda Deployments
  • Source Code Management and Branching Strategies for CI/CD
  • Pipeline as a Service: How To Test Pipelines in GitLab
  • Unraveling the Siloing Issue When Using Argo CD With Other Similar Tools

Trending

  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • How to Save Money Using Custom LLMs for Specific Tasks
  • Frame Buffer Hashing for Visual Regression on Embedded Devices
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Using Git Submodules With Gitlab CI/CD

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.

By 
Rishab Aggarwal user avatar
Rishab Aggarwal
·
Apr. 18, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
30.0K Views

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.

Configure the .gitmodules File

When you use Git submodules, your project should have a file named .gitmodules. 

To create .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:
Textile
 




x


 
1
$ git submodule add ../chaconinc/project-y.git
2
Cloning into 'project-y'...
3
remote: Counting objects: 11, done.
4
remote: Compressing objects: 100% (10/10), done.
5
remote: Total 11 (delta 0), reused 11 (delta 0)
6
Unpacking objects: 100% (11/11), done.
7
Checking connectivity... done.



  • For submodules not located on the same GitLab server, use the full URL:  
Textile
 




xxxxxxxxxx
1


 
1
$ git submodule add https://gitserver.com/group/project-y.git
2
Cloning into 'project-y'...
3
remote: Counting objects: 11, done.
4
remote: Compressing objects: 100% (10/10), done.
5
remote: Total 11 (delta 0), reused 11 (delta 0)
6
Unpacking objects: 100% (11/11), done.
7
Checking connectivity... done.



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:

  1. Make sure you use relative URLs for submodules located in the same GitLab server.
  2. You can set the GIT_SUBMODULE_STRATEGY variable to either normal or recursive.

In the variables section of your gitlab-ci file, add the following:

Textile
 




xxxxxxxxxx
1


 
1
variables:
2
  GIT_SUBMODULE_STRATEGY: recursive 



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.

Textile
 




x


 
1
default:
2
  tags:
3
    - <your runner Name>
4
  before_script:
5
    - git config --global http.sslVerify "false"
6
    - git submodule init
7
    - git submodule update --recursive --remote



Once the above lines were added, submodules for us worked seamlessly in GitLab.

Continuous Integration/Deployment Git GitLab

Opinions expressed by DZone contributors are their own.

Related

  • Streamlining AWS Lambda Deployments
  • Source Code Management and Branching Strategies for CI/CD
  • Pipeline as a Service: How To Test Pipelines in GitLab
  • Unraveling the Siloing Issue When Using Argo CD With Other Similar Tools

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook