When your Astro project depends on code maintained in a separate repository, Git submodules let you include that external repository as a nested folder inside your Astro project. This is useful when you want to keep shared code, such as a dbt project or a Python utility library, in its own repository while still deploying it as part of your Astro project.
This document covers how to add a Git submodule to an Astro project, configure CI/CD to deploy with submodules, and test your setup locally.
Use Git submodules when:
A common example is including an external dbt project as a submodule so that Cosmos can orchestrate dbt models as Airflow tasks.
Git submodules add complexity to your development and deployment workflows. If the external code changes infrequently or is small, consider copying the code directly into your Astro project instead.
Run the following command from your Astro project root folder to add the external repository as a submodule:
Replace <repository-url> with the URL of the external repository and <folder-name> with the name of the folder where the submodule code appears in your project. For example, to add a dbt project called jaffle-shop:
This command creates a .gitmodules file in your project root and clones the external repository into the specified folder. The .gitmodules file tracks the submodule configuration:
The cosmos-demo-submod repository demonstrates using a Git submodule to include an external dbt project in an Astro project. In this example, the jaffle-shop dbt project is included as a submodule and orchestrated with Cosmos.
The project structure looks like the following:
The Dockerfile installs dbt into a virtual environment so that Cosmos can run dbt models:
The requirements.txt file includes the Cosmos package:
When you clone a repository that contains submodules, the submodule folders are empty by default. To initialize and fetch the submodule contents, use one of the following methods:
Clone the repository with the --recurse-submodules flag:
If you already cloned the repository without the flag, initialize the submodules manually:
When the external repository has new changes that you want to pull into your Astro project, update the submodule:
When you deploy an Astro project that contains submodules, your CI/CD pipeline must clone the submodule contents before running astro deploy. Without this step, the submodule folders are empty and the deploy fails.
Add the submodules option to your checkout step:
If your submodule is in a private repository, configure authentication by adding a personal access token (PAT) or deploy key:
Set the GIT_SUBMODULE_STRATEGY variable in your .gitlab-ci.yml file:
For other CI/CD tools, run the following commands after cloning the repository and before deploying:
By default, a submodule tracks a specific commit. To configure it to follow a specific branch, run the following command:
After setting the branch, run git submodule update --remote to pull the latest commit from that branch.
If the submodule folder is empty after you clone the Astro project, run:
If the submodule shows an unexpected version of the code, check the commit hash with git submodule status. To update the submodule to the latest commit:
Ensure your CI/CD pipeline includes a recursive submodule checkout. See Configure CI/CD for submodules.