본문 바로가기

Technical/Development

Curl로 새로운 GitHub 프로젝트(repository) 쉽게 생성하기


GitHub 에 새로운 repository를 생성할 때 어떻게 하시나요? 주로 github 접속/로그인-New Repository 클릭-이름입력-Create Repository 클릭-로컬 git 디렉토리 생성-git init-git add-commit-push 과정을 통해서 진행할텐데, 과정 자체가 어려운 건 아니지만 번거로운 점이 적지 않습니다. 이 과정을 간단한 스크립트를 만들어서 진행하면 어떨까요?





준비사항

  • GitHub Account: github.com 에 가입(It's free!)
  • Git client와 curl이 설치된 linux box



스크립트 작성 & 실행


여기서 개발자의 작업 PC는 CentOs/RedHat linux 박스로 하고, 우선 Github에 올릴 소스 작업 디렉토리가 필요하겠지요. 기존 작업 디렉토리가 아닌 완전히 새로운 것으로 해 볼텐데요, 이를 GitHub의 자신의 계정과 연결하는 식으로 진행할 겁니다. 응용하면, 기존 디렉토리도 당연히 비슷한 방식으로 연결 가능하겠지요.


현재 작업 PC의 계정이 git config 로 git 서버에 연결된 적이 없다는 것을 전제로 진행합니다.


스크립트를 만듭니다(아래 스크립트를 응용하면 GitHub Account와 Email, 프로젝트명을 실행인자로 전달해서 수행하는 것도 가능하겠네요. 필요하다면 ... ^^). 스크립트를 수행하면 현재 디렉토리 아래에 해당 작업 디렉토리가 생성되고 이것이 바로 GitHub repository와 연결 됩니다. 


# vi github-repo-set.sh

#!/bin/bash
# Set your GitHub username and email
export GITHUB_USERNAME="YourGithubAccount"
export GITHUB_EMAIL="youraccount@domain.com"

git config --global user.name "${GITHUB_USERNAME}"
git config --global user.email "${GITHUB_EMAIL}"
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'

curl -u "${GITHUB_USERNAME}" https://api.github.com/user/repos -d '{"name":"my-test-project-001"}'

mkdir my-test-project-001
echo "# my-test-project-001" > my-test-project-001/README.md
cd my-test-project-001
git init
git add .
git commit -a -m "Create my-test-project-001 repository"
git remote add origin https://github.com/${GITHUB_USERNAME}/my-test-project-001.git
git push -u origin master

# chmod a+x github-repo-set.sh

# ./github-repo-set.sh

Enter host password for user 'YourGithubAccount': - 로그인 암호 입력 -

{

  "id": 123456789,

  "name": "my-test-project-001",

  "full_name": "YourGithubAccount/my-test-project-001",

  "owner": {

    "login": "YourGithubAccount",

    "id": 123456789,

    "url": "https://api.github.com/users/YourGithubAccount",

    "html_url": "https://github.com/YourGithubAccount",

...

  "git_url": "git://github.com/YourGithubAccount/my-test-project-001.git",

  "ssh_url": "git@github.com:YourGithubAccount/my-test-project-001.git",

  "clone_url": "https://github.com/YourGithubAccount/my-test-project-001.git",

...

}

Initialized empty Git repository in /yourdirectory/my-test-project-001/.git/

[master (root-commit) d5f9e80] Create my-test-project-001 repository

 1 file changed, 1 insertion(+)

 create mode 100644 README.md

Username for 'https://github.com': YourGithubAccount 입력

Password for 'https://YourGithubAccount@github.com': - 로그인 암호 입력 -

Counting objects: 3, done.

Writing objects: 100% (3/3), 253 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To https://github.com/YourGithubAccount/my-test-project-001.git

 * [new branch]      master -> master

Branch master set up to track remote branch master from origin.

* cache --timeout=3600 부분은, Git 서버에 로그인한 세션이 1시간 동안 유지되는 것으로 필요에 따라 줄이거나 늘이면 되겠습니다



이제 GitHub 에 접속, 로그인해서 만들어진 프로젝트 repo를 확인해 봅니다. 아래 이미지는 저의 GitHub 계정인 DragOnMe 에 생성된 프로젝트의 캡처 화면입니다. 참 쉽죠잉~ ^^




#보너스/Repository 이름을 변수로 하여 스크립트 재작성


위의 스크립트는 테스트를 위한 일회성 스크립트이다. 이번에는 현재 YourProjectName 이라는 워킹 디렉토리와 하위에 파일이 존재할 경우에 반복적으로 써먹을 수 있도록 위의 스크립트를 일부 수정해 보자.


vi github-repo-set.sh

#!/bin/bash 
# Set your GitHub username and email 
export GITHUB_USERNAME="YourGithubAccount" 
export GITHUB_EMAIL="YourEmail@domain.com" 
export PROJECT_NAME="YourProjectName"
#
git config --global user.name "${GITHUB_USERNAME}" 
git config --global user.email "${GITHUB_EMAIL}" 
git config --global credential.helper cache 
git config --global credential.helper 'cache --timeout=3600' 
#
curl -u "${GITHUB_USERNAME}" https://api.github.com/user/repos -d '{"name":"'"$PROJECT_NAME"'"}' 
# mkdir ${PROJECT_NAME}
echo "# ${PROJECT_NAME}" > ${PROJECT_NAME}/README.md 
cd ${PROJECT_NAME}
git init 
git add . 
git commit -a -m "Create ${PROJECT_NAME} repository" 
git remote add origin https://github.com/${GITHUB_USERNAME}/${PROJECT_NAME}.git 
git push -u origin master

* PROJECT_NAME 변수에 레포지터리 이름을 할당해서 사용하고 있는데, 이 부분을 $1 으로 바꾸어 스크립트의 실행 인자로 레포지터리 이름을 넣어서 사용하하거나, 또는 변수로 사용하는 계정, 이메일 등과 함꼐 모두 바깥으로 빼서 실행 인자로 바꿔서 사용하는 방법도 생각해 볼 수 있다

 



- Barracuda -