I was trying to make the content repo of this website, a private gitsubmodule of the main repo which is public

  • But the problem was I was hosting my website on vercel and it doesnt seem to work when I make my submodules private, it works good when they are public

So I found a work around, so all of this will happen on vercel

  • 1st we need to create a temp folder and initialise git, then using github secret token, we can pull the github private repo into that temp folder
  • then copy all the information into the submodule folder and delete the temp folder

Here is the script to do that

# github submodule repo address without https:// prefix
 
SUBMODULE_GITHUB=github.com/swarajbachu/obsidian-notes
 
  
 
# .gitmodules submodule path
 
SUBMODULE_PATH=content
 
  
 
# github access token is necessary
 
# add it to Environment Variables on Vercel
 
if [ "$GITHUB_ACCESS_TOKEN" == "" ]; then
 
echo "Error: GITHUB_ACCESS_TOKEN is empty"
 
exit 1
 
fi
 
  
 
# stop execution on error - don't let it build if something goes wrong
 
set -e
 
  
 
# get submodule commit
 
output=`git submodule status --recursive` # get submodule info
 
no_prefix=${output#*-} # get rid of the prefix
 
COMMIT=${no_prefix% *} # get rid of the suffix
 
  
 
# set up an empty temporary work directory
 
rm -rf tmp || true # remove the tmp folder if exists
 
mkdir tmp # create the tmp folder
 
cd tmp # go into the tmp folder
 
  
 
# checkout the current submodule commit
 
git init # initialise empty repo
 
git remote add origin https://$GITHUB_ACCESS_TOKEN@$SUBMODULE_GITHUB # add origin of the submodule
 
git fetch --depth=1 origin main # fetch only the required version
 
git checkout main # checkout on the right commit
 
  
 
# move the submodule from tmp to the submodule path
 
cd .. # go folder up
 
rm -rf tmp/.git # remove .git
 
mv tmp/* $SUBMODULE_PATH/ # move the submodule to the submodule path
 
  
 
# clean up
 
rm -rf tmp # remove the tmp folder
 

After this I overwritten the build script in the vercel ./submodule.sh && npx quartz build

I just wanted it on vercel that is the reason, I wrote this, oresle you can also write something into your package.json and then replace with that for more cleaner approach ( i mean doesnt matter, do as you please )

then its done

But only one problem, this was specific to me actually,

  • So when I wont commit to the main repo regularly, but I kinda make changes to this submodule repo, but the build function will only run if I make changes in the main repo
  • So I just made a webhook to solve this
  1. Go to your submodule repository on GitHub.
  2. Navigate to Settings Webhooks Add webhook.
  3. Enter the Payload URL provided by Vercel (can be found in your Vercel project settings). a. For this Navigate to your project Settings git then scroll down to Deploy hooks b. Give it any name and also mention the branch for me I wanted it on main c. it will look something like this https://api.vercel.com/v1/integrations/deploy/prj_1234567890abcdefghijklmnopqrstuvwxyz/OUyMxthUPZ
  4. Set the Content type to application/json.
  5. Optionally, set up a secret for added security.
  6. Choose to send push events.
  7. Add the webhook.

Now you are good to go :)

lets fucking go we do it