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
Newsletter
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

  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • Compress RabbitMQ Messages
  • AI Agents for Software Engineering and Autonomous Development Workflows
  • AI-Powered CI Test Failure Triage

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Apr. 18, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
30.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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