LoginSignup
2
3

More than 5 years have passed since last update.

fablicでGitデプロイ

Last updated at Posted at 2016-06-08

やりたかったこと

コマンド一発で複数台サーバにGitデプロイ

参考URL

http://fabric-ja.readthedocs.io/ja/latest/tutorial.html
このページ読めば大体理解できます

インストール

sudo yum install fabric

# 確認
fab --version
python --version

fabfile.py作成

cd project/path
vim fabfile.py
fabfile.py
# coding: utf-8

from __future__ import with_statement
from fabric.api import run, env, abort, cd
from fabric.contrib.console import confirm

# デプロイ先のサーバ設定(この場合は2か所にデプロイ)
env.hosts = [
    'hoge_username@hoge.com',
    'fuga_username@fuga.com'
    ]
env.passwords = {
    'hoge_username@hoge.com:22': 'hoge_password',
    'fuga_username@fuga.com:22': 'fuga_password'
    }

# env.hostsの数だけ実行される
def deploy():
    hostname = run('hostname')

    # 確認
    if not confirm(hostname + " <- このサーバにデプロイします、よろしいですか?"):
        abort("中止しました")

    # デプロイ
    project_path = '/project/path/html'
    with cd(project_path):
        run("git pull origin master")

Point

  • run('any command')で外部サーバ上でコマンドが実行できる
  • cd('path/to')で外部サーバ上でディレクトリ移動ができる

デプロイ実行

fab deploy

fabコマンドはカレントディレクトリのfabfile.pyを処理対象とします

2
3
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
2
3