Includes all branches and tags, but not description/PRs/Wikis/Releases
Especially useful to backup dynamic environments where people are adding new repos all the time, avoids having to maintain configurations as finds and iterates all non-fork repos by default
Can't use Terraform to dynamically create these backups because a simple commented/deleted code mistake would bypass prevent_destroy and delete your backup repos as well as your originals!
Cron this script as per your preferred backup schedule
Unfortunately GCloud SDK doesn't currently support configuring GCP's GitHub automatic mirroring, which is only available through the UI Console, making it unsuitable for automation
Cloud Source Repos are extremely rudimentary with no features and only really suitable for mirror trigger repos or as backups, and can't even configure a different default branch
If no repos are given, iterates all non-fork repos for the current user or GitHub organization
Each repo will have the same name in GCP Source Repos to avoid clashing with manually configured GCP automatically mirroring functionality which creates repos in the format 'github_{owner}_{repo}'
Any non-letter leading characters and non alphanumeric/dots/dashs/underscores will be removed to meet GCP Source Repos naming conventions eg. '.github' -> 'github' repo
For source GitHub accounts, requires:
- \$GITHUB_TOKEN
- \$GITHUB_ORGANIZATION, \$GITHUB_USER or else infers owner of the \$GITHUB_TOKEN
For GCP Source Repos requires:
- \$CLOUDSDK_CORE_PROJECT
- GCloud SDK installed and authenticated
In a GitHub Organization, only repos the user can read will be mirrored, others won't be returned in the list of GitHub repos to even try (as an outside collaborator user)
If \$CLEAR_CACHE_GITHUB_MIRROR is set to any value, deletes the /tmp cache and uses a fresh clone mirror. This can sometimes clear push errors.
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="[<repo1> <repo2> <repo3> ...]"
check_env_defined "GITHUB_TOKEN"
check_env_defined "CLOUDSDK_CORE_PROJECT"
help_usage "$@"
#min_args 1 "$@"
timestamp "Starting GitHub to GCP Source Repos mirroring"
echo >&2
user="${GITHUB_USER:-$(get_github_user)}"
owner="${GITHUB_ORGANIZATION:-$user}"
if is_blank "$owner";then
die "Failed to determine GitHub owner"
fi
if[$# -gt 0];then
repos="$*"
else
timestamp "Getting list of all non-fork GitHub repos owned by '$owner'"