커밋벨을 Heroku에 올리기

2016-10-10

    앞 포스팅에서 커밋벨을 어디에서 돌려야 하는지에 대해서 못 정했었다. 많은 분들이 Heroku를 추천해 주셔서 여기에 올려보려고 한다. Heroku를 쓰려면 Heroku 홈페이지에 들어가서 먼저 가입을 한다. 인증메일이 날아오고 확인하면 가입이 완료된다.

준비 할 것

  1. Git
        git으로 배포하기 때문에 필요하다.
  2. 헤로쿠 툴벨트 설치
        Heroku Dev Center에서 installer를 받을 수 있다. 각 운영체제에 맞는 installer 혹은 링크로 설치하면 된다.

Heroku에 올리기

    프로젝트 디렉토리에서 아래의 명령어로 Heroku를 시작 할 수 있다. 이메일과 비밀번호를 묻는다.

$ heroku create
heroku-cli: Installing CLI... 20.77MB/20.77MB
Enter your Heroku credentials.
[...]

    아래명령어로 로그인 하자. 모든 명령이 로그인한 계정의 권한 내에서 실행된다.

$ heroku login

    push해서 헤로쿠에 올리는데 흠… 왜인지 실패한다. 찾아보니 헤로쿠에 올릴때 꼭 필요한 파일들이 있다.

$ git push heroku master

필요한 파일

  • requirements.txt
    pip로 패키지들을 requirements.txt에 넣어놓아야한다.
pip freeze > requirements.txt
  • Procfile
    이 파일은 헤로쿠에 올리고 뭘 실행할지 설정하는 것이다. 지금은 단순하게 파일 하나 돌리는 것이므로 Procfile에 아래와 같이 한줄을 넣어준다.
clock: python commit-bell.py
  • runtime.txt 명시적으로 몇 버전에서 돌릴지 지정해 줘야한다. runtime.txt에 아래와 같은 한 줄을 넣어줬다.
python-3.5.2

빌드팩 세팅

    실패해서 빌드로그를 보니 아래와 같이 나왔다. 헤로쿠 홈페이지에서 로그들을 볼 수 있다.

 !     No default language could be detected for this app.
			HINT: This occurs when Heroku cannot detect the buildpack to use for this application automatically.
			See https://devcenter.heroku.com/articles/buildpacks
 !     Push failed

    찾아보니 빌드팩을 세팅해줘야한다고 한다. 아래의 명령어로 세팅 해 줬다. Heroku Devcenter에 언어별로 안내가 잘 되어있다!

$ heroku buildpacks:set heroku/python
Buildpack set. Next release on damp-dawn-81803 will use heroku/python.
Run git push heroku master to create a new release using this buildpack.
$ heroku create --buildpack heroku/python
Creating app... done, ⬢ safe-forest-10411
Setting buildpack to heroku/python... done
https://safe-forest-10411.herokuapp.com/ | https://git.heroku.com/safe-forest-10411.git

    음.. 그래도 실패한다.

-----> Failed to detect set buildpack https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/python.tgz
More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
 !     Push failed

    파일 추가한것들을 원격저장소에 안올려줘서 git add, commit, push를 해줬다. 그리고 나서 헤로쿠에 올리고 성공했다. 헐……

$ git push heroku master
 [...]
 [requirements.txt 설치..]
 [...]

Scheduler

    Scheduled Jobs with Custom Clock Processes in Python with APScheduler를 보면 스케줄링 할 수 있는 방법이 있다. apscheduler를 설치하고 requirements.txt에 넣어주자.

$ pip install apscheduler

    apscheduler.triggers.cron문서를 보고 적당히 스케줄링했다.

Commit-bell Git Repo

    hyesun03/commit-bell 여기에 올려놨다. 토큰을 어떻게 해야할지 몰랐는데 여기글로 힌트를 얻었다. 슬랙토큰을 환경변수에 넣은 뒤 os.environ.get으로 토큰을 가져왔다. 헤로쿠를 처음 써 봐서 그런가 많이 헤맸다. 더 이상 AWS에 치킨값을 헌납하지 않아도 된다! 룰루

Heroku에 환경변수 넣기

    로컬에서 멀쩡한데 헤로쿠에 올리니까 잘 안되었다. 이유는 환경변수를 헤로쿠에 넣어주지 않아서 그랬다.(…) 아래의 명령어로 슬랙 토큰을 헤로쿠에 세팅 할 수 있다.

$ heroku config:set SLACK_BOT_COMMIT_BELL_TOKEN=xoxb-슬랙-토큰
Setting SLACK_BOT_COMMIT_BELL_TOKEN and restarting ⬢ damp-dawn-81803... done, v7
SLACK_BOT_COMMIT_BELL_TOKEN: xoxb-슬랙-토큰

참고자료