Git/GCP Cloud Source

From Ever changing code
< Git
Revision as of 00:42, 18 December 2021 by Pio2pio (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Download all repos across all projects:

GCP_USERNAME=$(gcloud info --format=yaml | grep account: | head -1 | tr -s " " | cut -f3 -d" " | tr -d " ")
GCP_PROJECTS_IDS=($(gcloud projects list | tail +2 | tr -s " " | cut -f1 -d " "))

for PROJECT in ${GCP_PROJECTS_IDS[@]}; do # loop over projects
    # discover repos in a project
    echo "[INFO] ---"
    echo -e "[INFO] Project: $PROJECT"
    REPOS=($(gcloud source repos list --project $PROJECT | cut -f1 -d" " | tail +2))
    if ((! ${#REPOS[@]})); then echo "[INFO] No repositories found."; continue; fi
    echo -e "[INFO] Found following repositories: ${#REPOS[@]}\n${REPOS[@]}"
    [ -d $PROJECT ] || mkdir $PROJECT # create a directory per project
    cd $PROJECT

    for REPO in ${REPOS[@]}; do # loop over repositories
      if [ -d $REPO ]; then
        echo "[INFO] Update repository: $PROJECT/$REPO"
        cd $REPO; git pull; cd -
        echo "[INFO] git clone ssh://${GCP_USERNAME}${PROJECT}/r/${REPO}"
                     git clone ssh://${GCP_USERNAME}${PROJECT}/r/${REPO}
    cd .. # return to root directory, level up from a project directory

Note: If any cloning get stuck in Resolving deltas: 100% (46222/46222), done, do: Ctrl+C, cd $REPO, git fetch to fetch branch information from the remote repository, git checkout master or any other branch