Last active
June 19, 2020 09:05
-
-
Save gmolveau/e933f3e1cd7ed7ed531e08a511da6d8c to your computer and use it in GitHub Desktop.
Gitlab to github push mirror shell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # inspiration : https://jigarius.com/blog/multiple-git-remote-repositories | |
| ### | |
| # This script does not check if the repos are available | |
| # This script does not check if the tokens are valid | |
| # This method is not ideal if you use private emails | |
| # like : [email protected] | |
| # create a vars.env file with these fields set : | |
| #GITLAB_USERNAME=<> | |
| #GITLAB_TOKEN=<> | |
| #GITHUB_USERNAME=<> | |
| #GITHUB_TOKEN=<> | |
| WD="$(dirname "${0}")" | |
| set -a; [ -f "${WD}"/vars.env ] && source "${WD}"/vars.env; set +a | |
| # check that env variables are set | |
| declare -a ENV_VARS=(GITLAB_USERNAME GITLAB_TOKEN GITHUB_USERNAME GITHUB_TOKEN) | |
| for ENV_VAR in "${ENV_VARS[@]}"; do | |
| if [ -z "$(eval "echo \$$ENV_VAR")" ]; then | |
| echo "Missing environment variable $ENV_VAR" | |
| exit 1 | |
| fi | |
| done | |
| # user interaction | |
| if [ $# -lt 2 ]; then | |
| echo "Please provide a repo name and a visibility [public]" | |
| exit 1 | |
| else | |
| REPO=${1} | |
| VISIBILITY=${2} | |
| fi | |
| # TODO check that the tokens are valid before any request | |
| # curl --silent --show-error \ | |
| # --head \ | |
| # --header "Authorization: Bearer ${GITLAB_TOKEN}" \ | |
| # "https://gitlab.com/api/v4/projects" > /dev/null | |
| # curl --silent --show-error \ | |
| # --head \ | |
| # --header "Authorization: token ${GITHUB_TOKEN}" \ | |
| # "https://api.github.com/user/repos" > /dev/null | |
| # create gitlab repo | |
| echo "> creating gitlab remote repo ..." | |
| curl --silent --show-error \ | |
| --header "Authorization: Bearer ${GITLAB_TOKEN}" \ | |
| --header "Content-Type: application/json" \ | |
| --data "{\"name\":\"${REPO}\",\"visibility\":\"${VISIBILITY}\"}" \ | |
| "https://gitlab.com/api/v4/projects" > /dev/null | |
| # create github repo | |
| echo "> creating github remote repo ..." | |
| curl --silent --show-error \ | |
| --header "Authorization: token ${GITHUB_TOKEN}" \ | |
| --header "Content-Type: application/json" \ | |
| --data "{\"name\":\"${REPO}\",\"visibility\":\"${VISIBILITY}\"}" \ | |
| "https://api.github.com/user/repos" > /dev/null | |
| # ask user to set-up mirroring on Gitlab website | |
| GITHUB_MIRROR_REPO=https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${REPO}.git | |
| GITLAB_REPO=https://gitlab.com/${GITLAB_USERNAME}/${REPO}/-/settings/repository#js-push-remote-settings | |
| [email protected]+${GITLAB_USERNAME}:${GITLAB_USERNAME}/${REPO}.git | |
| echo "please visit : ${GITLAB_REPO} " | |
| echo | |
| echo "expand 'Mirroring repositories' if it's not the case " | |
| echo | |
| echo "set 'Git repository URL' with : ${GITHUB_MIRROR_REPO} " | |
| echo | |
| echo "set 'Mirror direction' to : 'Push' " | |
| echo | |
| echo "set 'Password' with : ${GITHUB_TOKEN} " | |
| echo | |
| echo "finally click on the green 'Mirror repository' button " | |
| echo | |
| echo "you can add this gitlab git remote : git add remote origin ${GITLAB_REMOTE}" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment