How to deploy git submodules to CapRover

Search for a command to run...

No comments yet. Be the first to comment.
Introduction I run a large WhatsApp group and I often need to mention everyone in the group. But there are no real solutions for mentioning all members. WhatsApp does not natively do it, and there was a Firefox browser extension, but it was abandoned...

Introduction This past week I was toying around with the HTML5 canvas element, trying to join two images together. At first, it seemed fine, but when I tried to reload the website, it was a mess. One image would load, but the other wouldn't. Investig...

Introduction I have worked on projects that required the production and testing database to be the same. This could be because some features work in MariaDB, but not in SQLite. Or some bugs appear in MySQL, but not in PostgreSQL. When you're working ...

Introduction I'm going to explain how to build a free plan for your Laravel Spark Next application. I will be using Paddle as the payment gateway, but the steps are almost identical with Stripe. Best of all? No credit card required for the free plan....

Introduction Sometimes you end up deploying an application to Dokku and then realize that you want to revert the changes you made. In this tutorial we'll go over how to roll back a Dokku deployment. Before we start Preface Keep in mind that rolling ...

In this tutorial I will explain how to get git submodules to deploy correctly to CapRover using the CapRover CLI.
Having some knowledge about CapRover, Docker and Git will help you understand how this solution works.
When you use caprover deploy, what happens underneath is that the CLI uses git archive to make a compressed tar of your repository. It then sends and deploys that file to your CapRover server.
But there are some problems with git archive:
It does NOT include the .git directory in the tar.
So what you end up deploying is not really a git repository...
And if you were using git submodules in your repository, they are not downloaded, since the .git directory is missing.
To solve that issue, I have found a solution that is separated into three steps.
The first step to use git submodules in CapRover is to create a Dockerfile and download the git submodules as a build step.
You will need to create a captain-definition file and point it to a Dockerfile.
For example:
{
"schemaVersion": 2,
"dockerfilePath": "./Dockerfile"
}
Then, you will need to create a Dockerfile that contains the following build step.
RUN git submodule update --init --recursive
For example:
FROM node:15-alpine
COPY . .
RUN apk --no-cache add git
# IMPORTANT: Download git submodules
RUN git submodule update --init --recursive
# ...
RUN npm ci
CMD ["node", "src/main"]
The second step is to improve what caprover deploy does.
Create a tar file of your repository, while adding the .git directory.
For that, you can use the following commands:
# Archive git repository
git archive HEAD > deploy.tar
# Add `.git` directory to `tar`
tar -rf deploy.tar .git
Now that you have both the tar with the .git directory, and a Dockerfile that downloads the git submodules, you are ready to deploy.
# Deploy the `tar` to your CapRover server
npx caprover deploy -t ./deploy.tar
# Remove the tar
rm ./deploy.tar
That was it, I hope you had luck and your deployment was successful!
Feel free to use the following script to perform all of these steps automatically.
#!/bin/bash
# Archive git repository
git archive HEAD > deploy.tar
# Add `.git` directory to `tar`
tar -rf deploy.tar .git
# Deploy the `tar` to your CapRover server
npx caprover deploy -t ./deploy.tar
# Remove the tar
rm ./deploy.tar
If you have found this useful, then you should follow me, I will be posting more interesting content! 🥰
Or support me financially. 💸
Congratulations, today you have learned how to deploy git submodules to your CapRover server.
Let me know if the tutorial was useful to you in the comments!