Created
May 6, 2019 07:44
-
-
Save jk2K/05b31a4c8c89ed1962f1e28c197462b8 to your computer and use it in GitHub Desktop.
推送多个仓库到 GitLab, 支持 GitLab API v4
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
#!/bin/bash | |
# 自动创建组和仓库,自动上传仓库 | |
# 配置选项 | |
gitlab_access_token='your personal access token' | |
gitlab_base_url='http://gitlab.demo.com/api/v4' | |
repositorys=( | |
"xxxx" | |
"xxxx" | |
) | |
# start | |
working_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
gitlab_create_repository() { | |
repository_name=$1 | |
echo "create repository start" | |
curl -X POST \ | |
"${gitlab_base_url}/groups" \ | |
-H 'Content-Type: application/json' \ | |
-H "Private-Token: ${gitlab_access_token}" \ | |
-H 'cache-control: no-cache' \ | |
-d '{ | |
"name": "'${repository_name}'", | |
"path": "'${repository_name}'" | |
}' | |
echo "\n" | |
} | |
gitlab_create_group() { | |
group_name=$1 | |
echo "create group start" | |
curl -X POST \ | |
"${gitlab_base_url}/groups" \ | |
-H 'Content-Type: application/json' \ | |
-H "Private-Token: ${gitlab_access_token}" \ | |
-H 'cache-control: no-cache' \ | |
-d '{ | |
"name": "'${group_name}'", | |
"path": "'${group_name}'" | |
}' | |
echo "\n" | |
} | |
# avoid password prompt every push | |
git config --global credential.helper store | |
for i in "${repositorys[@]}" | |
do | |
echo "========" | |
echo "take ${i} start" | |
cd "${working_dir}/${i}" | |
repository_name=$(basename -s .git $(git config --get remote.origin.url)) | |
group_name=$(git config --get remote.origin.url | sed 's/.*\/\([^ ]*\)\/.*/\1/') | |
echo "find repository name/group_name success" | |
gitlab_create_group $group_name | |
gitlab_create_repository $repository_name | |
echo "${repository_name}/${group_name}" | |
echo "push repository" | |
git push origin master | |
echo "========" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment