LoginSignup
5
4

More than 3 years have passed since last update.

SlackBotをHerokuでデプロイ

Last updated at Posted at 2019-07-29

折角Botを作成したので、デプロイしましょう!今回はHerokuを使って、簡単にデプロイします。(AWSのLamdaとAPI Gatewayあたりも考えていたのですが。。その辺りは知見のある方の意見が欲しいところです。)

デプロイ準備

※Herokuのアカウント登録などは諸々済んでいる前提で進みます。
登録がまだの方はherokuの記事を参考に
https://qiita.com/Arashi/items/b2f2e01259238235e187
heroku login まで終わらせると良いかと。

Procfile作成

ProcfileとはHerokuデプロイ後に実行されるファイルのこと。
プロセスタイプ: 実行するコマンド という形式で記述します。

プロセスタイプとは?
https://jp.heroku.com/dynos/configure

process_type.png

Procfile(正しい)
worker: bundle exec ruby slack_bot.rb -p $PORT

ちなみに、ここをずっと

Procfile(間違い)
web: bundle exec ruby slack_bot.rb -p $PORT

にしていて、

エラー
$ heroku logs

:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
:

プロセスの指定お気をつけください。今回の正解は worker です。

デプロイ

ターミナル
$ heroku login
Enter your Heroku credentials.
Email: sample@example.com
Password (typing will be hidden):
Authentication successful.

(デプロイしたいアプリケーションディレクトリに移動)
$ git init
$ git add .
$ git commit -m "hello heroku commit"

Heroku Buildpack for Rubyを利用してrubyアプリケーションをデプロイするための環境を整えます。

$ heroku create --buildpack https://github.com/heroku/heroku-buildpack-ruby.gitを実行
$ git push heroku master

Counting objects: 4, done.
:
:
remote: Verifying deploy... done.
To https://git.heroku.com/xxxxxxxxxxxxxxx.git
 * [new branch]      master -> master

環境変数

# 環境変数の設定
$ heroku config:set SLACK_API_TOKEN=xoxohogehoge(slackのtoken)
$ heroku config:set GITHUB_USERNAME=hogename(githubアカウント名)
$ heroku config:set GITHUB_PASSWORD=hogepass(githubパスワード)

# 環境変数の確認
$ heroku config

workerプロセスを起動する

$ heroku scale worker=1

プロセス確認

$ heroku ps
Free dyno hours quota remaining this month: 532h 36m (96%)
Free dyno usage for this app: 0h 36m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping

=== worker (Free): bundle exec ruby slack_bot.rb -p $PORT (1)
worker.1: up 2019/05/08 09:50:15 +0900 (~ 26m ago)

こんな感じでworker.1: up していればOK!
プロセスを停止したいときは、

$ heroku scale worker=0

以上でherokuでデプロイ完了です。
Slack場でBotが適切に動くか確認してみて下さい。

slack_bot.png

またこの記事に対して何か間違えている部分があれば、コメントして頂けると助かります。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4