Ionic Pro - Package Build fails due to git submodule

As indicated in this Zendesk issue, Submodules are supported now, but only for public repos with no authorization required.

One option suggested in othersimilar issues is to switch from submodules to subtrees.

Finally, if it’s not an option, here is a bash script I came up with as a workaround. I still want to support submodules in my original private repo on Bitbucket, but I don’t care how it is stored on Ionic Pro. So I just create a new temporal branch, copy submodule and include it as a simple directory in the source code. Then I remove .gitmodules file and push the changes to ionic origin. Then I delete the local branch and clean up.

Here is the code that you can save as a shell script and run it each time instead of git push ionic HEAD. But first of all, consider switching to subtrees :slight_smile:

#!/bin/bash

# Prepare repository and push to Ionic Pro
# - checkout a new branch
# - copy submodule to another directory, update its alias in tsconfig.json and commit
#   (we need it because Ionic Pro does not support private submodules yet)
# - push to Ionic Pro and clean up (switch to original branch and delete the new one)

now=$(date +"%m/%d/%Y %H:%M")
original_branch=$(git symbolic-ref --short HEAD)
ionic_origin="ionic"
branch_id=$(date +"%m%d%Y-%H%M%S")
tmp_branch="tmp/$branch_id/$original_branch"
submodule_dir="my-submodule"
target_dir="my-submodule-copy"
commit_msg="Copied submodule for Ionic Pro ($now)"

set -e # exit script if any command fails

printf "Preparing repository for Ionic Pro...\n"

if [ `git branch --list $tmp_branch` ]
then
  printf "  - branch $tmp_branch already exists, deleting it\n"
  git branch -D ${tmp_branch}
fi

printf "  - checking out a new branch $tmp_branch\n"
git checkout -b ${tmp_branch}

printf "  - copying $submodule_dir into $target_dir\n"
cp -r ${submodule_dir} ${target_dir}

printf "  - removing .git directory from the copy\n"
rm -rf ${target_dir}/.git

# this is optional, only if you use TS alias for your submodule! The regex should be updated for your needs
printf "  - replacing @my-submodule alias in tsconfig.json\n"
sed -i "s_\.\.\/${submodule_dir}\/_\.\.\/${target_dir}\/_g" tsconfig.json

printf "  - removing .gitmodules file"
git rm .gitmodules

printf "\nSubmitting...\n"
printf "  - adding changes to git\n"
git add ${target_dir} tsconfig.json

printf "  - committing changes\n"
git commit -m "$commit_msg" > /dev/null

printf "  - pushing changes\n"
git push ${ionic_origin} ${tmp_branch}

printf "\nCleaning up...\n"
printf "  - checking out $original_branch\n"
git checkout ${original_branch} > /dev/null

printf "  - deleting branch $tmp_branch\n"
git branch -D $tmp_branch

printf "\nDone!\n"
printf "  now you can go to Ionic Pro dashboard and build a package for commit with this message:\n    \"$commit_msg\"\n\n"