Last active
December 9, 2020 05:04
-
-
Save hayajo/511e11d4bbc8b756a72cdde1250d11bf to your computer and use it in GitHub Desktop.
Fabricでステージごとにenvを切り替える
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from fabric.api import task | |
from fabric.api import env | |
# コマンドラインから`stage.production`ではなく`production`と指定できるように関数をインポートする | |
from stage import production, integration, staging, testing, development | |
import myapp # アプリケーションごとにモジュールをわける | |
# import myapp2 | |
# import myapp3 | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from fabric.api import task, env | |
import pprint | |
from stage import stage_env | |
# 各ステージの設定を定義する | |
STAGE_ENV = { | |
'production': { | |
'hosts': ['user@example.com'], | |
'branch': 'production', | |
'token': { | |
'tokenA': 'production_0123456789ABCDEF', | |
'tokenB': 'production_FEDCBA9876543210', | |
}, | |
# ... | |
}, | |
'staging': { | |
'hosts': ['user@staging.example.com'], | |
'branch': 'master', | |
'token': { | |
'tokenA': 'staging_0123456789ABCDEF', | |
'tokenB': 'staging_FEDCBA9876543210', | |
}, | |
# ... | |
}, | |
'testing': { | |
'hosts': ['user@testing.example.com'], | |
'branch': 'development', | |
'token': { | |
'tokenA': 'testing_0123456789ABCDEF', | |
'tokenB': 'testing_FEDCBA9876543210', | |
}, | |
# ... | |
}, | |
# ... | |
} | |
@ task | |
def deploy(): | |
stage_env(STAGE_ENV) # 指定されたステージの設定をenvに反映する | |
pprint.pprint(env.hosts) | |
pprint.pprint(env.branch) | |
pprint.pprint(env.token) | |
pprint.pprint(env.warn_only) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from fabric.api import task, env | |
from fabric.operations import require | |
@task | |
def production(): | |
# 本番環境 | |
env.stage = 'production' | |
@task | |
def integration(): | |
# 統合テスト環境(現行バージョンから次期バージョンへのデプロイテスト用) | |
env.stage = 'integration' | |
@task | |
def staging(): | |
# QA環境(次期バージョンのテスト用) | |
env.stage = 'staging' | |
@task | |
def testing(): | |
# QA環境だったり開発環境だったり... | |
env.stage = 'testing' | |
@task | |
def development(): | |
# 開発環境 | |
env.stage = 'development' | |
def stage_env(stage_env): | |
# env.stageを必須とする。また、`stage_env`より先に各ステージのタスクが呼び出されるよう制限する。 | |
require('stage', provided_by=(production, integration, staging, testing, development)) | |
if not stage_env.has_key(env.stage): | |
return | |
for k, v in stage_env[env.stage].items(): | |
if not env.has_key(k) or not env[k]: | |
setattr(env, k, v) | |
env.hostsはタスク実行時にはFIXされているので、タスク内でenv.hosts=LIST
と再設定しても接続先の変更は不可能。残念。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行はつぎのとおり。