Heroku CLI を使わずに、BottleアプリをHerokuへデプロイする

通常、Heroku CLI (以前のHeroku Toolbelt)を使ってHerokuにデプロイしています。
Heroku CLI | Heroku Dev Center

ただ、Heroku CLIを使わずにデプロイする方法があるのか気になったため、その方法を調べた時のメモです。

目次

 

環境

 

Bottleアプリまわりの作業

Bottleのインストール
$ pip install bottle

 

Bottleアプリの作成

Hello, world的なものを作成します。

 

Bottleアプリ本体

app.py

import os
from bottle import get, run

@get('/')
def index():
    return 'Hello, heroku'

if __name__ == "__main__":
    run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))

 
起動して動作確認します。

$ python app.py 

 

requirements.txtの作成
$ pip freeze > requirements.txt

 

Procfileの作成
web: python app.py

 

runtime.txtの作成
python-3.6.2

 
なお、サポートされているバージョンは現時点では以下の2つでした。

参考:Specifying a Python Runtime | Heroku Dev Center

 
そのため、

python-3.5.3

のようなサポート外のバージョンを指定すると、push時に

remote: -----> Python app detected
remote: -----> Installing python-3.5.3
remote: -----> Installing pip
remote: -----> Installing requirements with pip
remote:        /app/tmp/buildpacks/779a8bbfbbe7e1b715476c0b23fc63a2103b3e4131eda558669aba8fb5e6e05682419376144189b29beb5dee6d7626b4d3385edb0954bffea6c67d8cf622fd51/bin/steps/pip-install: line 7: /app/.heroku/python/bin/pip: No such file or directory
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed

とエラーになりました。

 

Herokuの準備

アプリの作成

Personal appより作成します。

 

公開鍵の登録

ssh-keygen -t rsaで公開鍵を作成します。
Managing Your SSH Keys | Heroku Dev Center

 
作成した鍵は、HerokuのAccount settingsの SSH Keys に登録しておきます。

 

接続の確認

sshでHerokuにアクセスできるか確認します。

状況に応じて、~/.ssh/id_rsa_herokuとしてあるプライベート鍵ファイルのパスを変更します。

$ ssh -i ~/.ssh/id_rsa_heroku -v git@heroku.com
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: ~/.ssh/id_rsa_heroku
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: Authentication succeeded (publickey).
Authenticated to heroku.com ([50.19.85.156]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = ja_JP.UTF-8
PTY allocation request failed on channel 0
shell request failed on channel 0

と表示されればOKです。

 
なお、接続に失敗した場合は

$ ssh -i ~/.ssh/id_rsa_heroku -v git@heroku.com
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: ~/.ssh/id_rsa_heroku
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

となります。

その時は、ここを見ると解決するかもしれません。
Fixing problems with keys | Managing Your SSH Keys | Heroku Dev Center

 

Herokuへのデプロイ

コミット
$ git add .
$ git commit -m "first commit"

 

push

add remoteします。

Heroku_Git_URLは、アプリのSettingsページにあるHeroku Git URLの値を設定します。

$ git remote add heroku https://git.heroku.com/<Heroku_Git_URL>

 
あとはpushしてデプロイします。

$ git push heroku master

 
なお、git - Can a Rails app be deployed without using Heroku Toolbelt? If so, how? - Stack Overflowでは

git remote add heroku git@heroku.com:falling-wind-1624.git

となっていましたが、その方法だと

$ git push heroku master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

となり、デプロイに失敗します。

 

デプロイ後の確認

動作確認

ターミナルに

https://<app_name>.herokuapp.com/ deployed to Heroku

と記載されているURLにアクセスし、動作を確認します。

 

ログ確認

Heroku CLIを使っていないため、heroku logsが使えません。

そのため、アドオンでの確認となります。手元ではPapertrailを使いました。

ただ、無料プランであっても、Papertrailを追加するにはクレジットカード認証が必要でした。

 

参考資料