When to use Git submodules
Use Git submodules when:- A separate team maintains code that your Dags depend on, and that code lives in its own repository.
- You want to pin your Astro project to a specific version of an external repository.
- You need to combine code from multiple repositories into a single Astro project before deployment.
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.
Prerequisites
- The Astro CLI.
- An Astro project.
- Git installed on your local computer.
- A remote Git repository containing the code you want to include as a submodule.
Add a submodule to your Astro project
1
Add the submodule
Run the following command from your Astro project root folder to add the external repository as a submodule:Replace This command creates a
<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:.gitmodules file in your project root and clones the external repository into the specified folder. The .gitmodules file tracks the submodule configuration:.gitmodules
2
Commit the submodule
After you add the submodule, commit the changes to your repository:
3
Verify the submodule
Confirm the submodule is correctly linked by running the following command:The output displays the commit hash of the submodule and its folder path.
Example: dbt project with Cosmos
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:Dockerfile installs dbt into a virtual environment so that Cosmos can run dbt models:
Dockerfile
requirements.txt file includes the Cosmos package:
requirements.txt
Clone an Astro project that contains submodules
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-submodulesflag: -
If you already cloned the repository without the flag, initialize the submodules manually:
Update a submodule to the latest commit
When the external repository has new changes that you want to pull into your Astro project, update the submodule:1
Pull the latest changes
Run the following command from your Astro project root folder:This updates the submodule to the latest commit on its default branch.
2
Commit the updated reference
After you update the submodule, your Astro project repository tracks a new commit hash for it. Commit this change:
Configure CI/CD for submodules
When you deploy an Astro project that contains submodules, your CI/CD pipeline must clone the submodule contents before runningastro deploy. Without this step, the submodule folders are empty and the deploy fails.
GitHub Actions
Add thesubmodules option to your checkout step:
.github/workflows/deploy.yml
.github/workflows/deploy.yml
GitLab CI/CD
Set theGIT_SUBMODULE_STRATEGY variable in your .gitlab-ci.yml file:
.gitlab-ci.yml
Other CI/CD tools
For other CI/CD tools, run the following commands after cloning the repository and before deploying:Pin a submodule to a specific branch
By default, a submodule tracks a specific commit. To configure it to follow a specific branch, run the following command:git submodule update --remote to pull the latest commit from that branch.
Troubleshoot submodules
Empty submodule folder after cloning
If the submodule folder is empty after you clone the Astro project, run:Submodule points to the wrong commit
If the submodule shows an unexpected version of the code, check the commit hash withgit submodule status. To update the submodule to the latest commit: