Create a new Gitlab repo from the command line
Git does some funny magic so you can create script named git-foo
and run the like they're arguments to git like git foo
.
Create a script and name it something like git-init-remote
Note: I have used GitLab's free instance they now provide. Change the url to your own if you're running a private instance of GitLab
#!/bin/sh
repo=$1
token=put_your_api_token_here
test -z $repo_name && echo "Repo name required." 1>&2 && exit 1
curl -H "Content-Type:application/json" https://gitlab.com/api/v3/projects?private_token=$token -d "{ \"name\": \"$repo\" }"
Run chmod +x git-init-remote
(or whatever you called your script). And move it somewhere into you PATH.
Now you can run git init-remote my-cool-repo
to create a new repo. You'll get a bunch of json back... Someone could probably write a fancier script that actually checks for success or failure.
Written by Nate West
Related protips
4 Responses
You have a small typo in this script that prevents it from working. Replace the $repo_name
variable with $repo
. Thanks for writing this... I was doing something similar on GitHub, but wasn't using this as a git script. Perfect for my project creation automation.
Last paragraph, small typo. Should be:
Now you can run git-init-remote my-cool-repo to create a new repo.
There are multiple errors in the script ("repo" and "repo_name" variables are used interchangeably) for those who are getting errors, use this rectified version:
!/bin/sh
reponame=$1
token=yourapitokenhere
test -z $repo_name && echo "Repo name required." 1>&2 && exit 1
curl -H "Content-Type:application/json" http://your_domain_or_ip_here/api/v3/projects?private_token=$token -d "{ \"name\": \"$repo_name\" }"
#!/bin/bash
# Script to automate the deployment of new private gitlab repos
# TODO: simplify and enhance error handling (already existing repos etc.)
# expand script to handle: public repos, deletion, re-naming
token="your_token_here"
function usage {
echo "Usage: $0 -n name"
exit 1
}
number_args=$#
if [[ !("$number_args" -eq 2) ]]; then
echo "Incorrect number of args" >&2
echo "If two strings in name, quote them: e.g. \"str1 str2\""
usage
fi
while getopts "n:" name; do
case "${name}" in
n)
repo=${OPTARG}
;;
\?)
usage
;;
*)
usage
;;
esac
done
if [[ -z ${repo} ]]; then
echo "Option -n requires an argument." >&2
usage
else
echo "#######\nCreating new repo: ${repo}\n#######\n"
fi
# In case space separate words are used for the repo name, the first word will be used
# when naming the json output file.
repo_short=$(echo ${repo} | cut -d " " -f1)
response=$(curl -s -o ./temp.json -w '%{http_code}' \
-H "Content-Type:application/json" https://gitlab.com/api/v3/projects?private_token=$token \
-d "{ \"name\": \"${repo}\" }")
# Format JSON log
cat ./temp.json | python -m json.tool > ./${repo_short}_repo.json
rm -f ./temp.json
echo "Any JSON output is logged in ./${repo_short}_repo.json"
if [ $response != 201 ]; then
echo "Error\nResponse code: $response"
exit 1
else
echo "Repo successfully created\nResponse code: $response"
exit 0
fi