INNOBASE技術ブログ

技術的なことエンジニア的なこと制作的なこと全般

botでgitリポジトリをS3にアップロードする

S3にアップロードしたコンテンツをwebサイトとして公開する機能、
静的HPをwebサーバーの管理をせずに安心して便利ですね
S3へのアップロードユーティリティーソフト、かなり出てきていますがそこそこ手間がかかる気がします。

botでアップロードできればとてもお手軽!
ついでにcssのcontent-typeがたまにバクって「text/x+c」になってしまう問題にもbotで対応してみました。
いちいちcontent-typeを確認する手間ともおさらばです。

# Description
#   website s3 deploy script.
#   ### 事前準備
#   - /home/bot/website/ に対象のgit repositoryをgit clone
#   - s3cmdの初期設定を実施
#
#   ### 処理内容
#   - /home/bot/website/htdocs/ をs3cmdでbacket直下にsync
#   - cssのcontent-typeが稀におかしくなるので、content-typeを修正
#
# Commands:
#   deploy s3website - deploy web page (s3)
child_process = require 'child_process'

module.exports = (robot) -> 
    robot.hear /^deploy s3website/i, (msg) ->
        command  = "cd /home/bot/website;git pull origin master;s3cmd sync /home/bot/website/htdocs/ s3://BACKET_NAME/;"
        command += "cd /home/bot/website/htdocs;find . -name \"*.css\" |sed 's/^\\.\\///'|awk '{system(\"s3cmd --add-header=Content-Type:text/css modify s3://BACKET_NAME/\"$1)}';"

        msg.send "deploy website 実行します。\n===================================\n"
        command += "echo \"==================================\n\";"
        command += "echo \"deploy website を実行しました。\";"
        child_process.exec command, (error, stdout, stderr) ->
            msg.send stderr
            msg.send stdout
実行するとこんな感じ

差分も見れてそこそこ良い感じ

deploy s3website 実行します。
===================================
Updating bb0c50f..38d58b3
Fast-forward
htdocs/css/styles.css  |  26 ++-
htdocs/img/logo.png    |  Bin 1648 -> 2675 bytes
htdocs/img/logo@2x.png  |  Bin 0 -> 4592 bytes
htdocs/index.html      |    4 +-
4 files changed, 20 insertions(+), 685 deletions(-)
delete mode 100644 htdocs/css/styles**.css
create mode 100644 htdocs/img/logo@2x.png
File '/home/bot/website/htdocs/css/styles.css' stored as 's3://<<BACKET_NAME>>/css/styles.css' (30431 bytes in 0.2 seconds, 169.16 kB/s) [1 of 3]
File '/home/bot/website/htdocs/img/logo.png' stored as 's3://<<BACKET_NAME>>/img/logo.png' (2675 bytes in 0.1 seconds, 37.54 kB/s) [2 of 3]
File '/home/bot/website/htdocs/index.html' stored as 's3://<<BACKET_NAME>>/index.html' (8569 bytes in 0.1 seconds, 75.98 kB/s) [3 of 3]
Done. Uploaded 41675 bytes in 1.0 seconds, 40.70 kB/s. Copied 0 files saving 0 bytes transfer.
File s3://<<BACKET_NAME>>/css/styles.css modified
===================================
deploy s3website を実行しました。
このスクリプト含めたbotの実装サンプルをこちらで公開しています。

mr51/hubot_recipe · GitHub



FTPでアップロードするサンプルはこちら

botでgitリポジトリをFTPアップロードする - INNOBASE技術ブログ