How to deploy git submodules to CapRover

How to deploy git submodules to CapRover

Introduction

In this tutorial I will explain how to get git submodules to deploy correctly to CapRover using the CapRover CLI.


Before we start

Preface

Having some knowledge about CapRover, Docker and Git will help you understand how this solution works.


The problem

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.


First step: Create a Dockerfile

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"]

Second step: Include .git directory in the tar

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

Third step: Deploy the tar

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

End

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

Self-promotion

If you have found this useful, then you should follow me, I will be posting more interesting content! 🥰

Or support me financially. 💸

Conclusion

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!