LoginSignup
1

More than 3 years have passed since last update.

Google App Engine で始める Go 実践入門 Part 9 【Makefile】

Last updated at Posted at 2019-03-31

連載を通して簡単なブログアプリを作成しつつ Go/GAE について学んでいきます。
今回は Makefile についての説明です。

Makefile

Makefile にタスクを定義していきます。
Node.js で言うところの npm run-scripts に相当します。

まずはファイルを作成します。

# プロジェクトルートに移動
cd $GOPATH/src/github.com/rema424/go-gae-blog-app-example

# Makefile を作成
touch Makefile

Makefile を編集します。

go-gae-blog-app-example/Makefile
.PHONY: all

# 環境変数を読み込んで変数に代入する
# ただし、コマンドライン引数で値が渡された場合は、環境変数が定義されていてもコマンドライン引数の方を優先する
# e.g.) make xxxxxx project_id=xxxxxxxxxxxx
project_id := ${GOOGLE_PROJECT_ID}
version := ${GAE_VERSION}

# シェルコマンドの実行結果を代入する時は$でエスケープする
OAUTH2_ACCESS_TOKEN=$$(gcloud auth print-access-token 2> /dev/null)

#
# タスク定義
#

# 認証
# e.g.) make login
login:
    gcloud auth activate-service-account --key-file key/deploy.json

# ローカルサーバーの起動
# e.g.) make dev
dev:
    dev_appserver.py module/blog/main/app.yaml --support_datastore_emulator=False --go_debugging=True

# デバッガーのアタッチ
# e.g.) make debug
debug:
    @PID=$$(ps u | grep _go_ap[p] | head -1 | awk '{print $$2}'); dlv attach $$PID --headless --listen=127.0.0.1:2345 --api-version=2

# デプロイ
# e.g.) make update version=1
update:
    appcfg.py update -A ${project_id} -V ${version} --oauth2_access_token=${OAUTH2_ACCESS_TOKEN} module/blog/main

# トラフィックを徐々に切り替える (500が発生しない 基本的にはこっち)
# e.g) make migrate version=1
migrate:
    appcfg.py migrate_traffic -A ${project_id} -V ${version} --oauth2_access_token=${OAUTH2_ACCESS_TOKEN} module/blog/main

# トラフィックを一瞬で切り替える (一瞬500が発生する)
# e.g) make set version=1
set:
    appcfg.py set_default_version -A ${project_id} -V ${version} --oauth2_access_token=${OAUTH2_ACCESS_TOKEN} module/blog/main

# e.g) make open version=1
open:
    gcloud app browse --version ${version}

タスクを定義したので、今後ローカル開発サーバーの起動やデプロイは make コマンドを使って次のように実行できます。

# ローカル開発サーバーの起動
make dev

# デバッガーのアタッチ
make debug

# デプロイ
make update project_id=<your-project-id> version=<version>

# トラフィックの移行
make migrate project_id=<your-project-id> version=<version>

direnv

デプロイやトラフィックの移行を実行する際に project_id を指定する必要がありますが、version とは違い同一のプロジェクト内では常に一定です。

したがって project_id はコマンドライン引数で毎回指定するのではなく、設定ファイルに記述して値を使い回せるようにします。

これを実現するために、direnv というツールを使います。

まずは Homebrew で direnv をインストールします。

# direnv のインストール
brew install direnv

.envrc ファイルを作成します。

# プロジェクトルートに移動
cd $GOPATH/src/github.com/rema424/go-gae-blog-app-example

# .envrc を作成
touch .envrc

# .gitignore に .envrc を追加
echo '.envrc' >> .gitignore

.envrc を編集します。自分のプロジェクトの ID を記載してください。

go-gae-blog-app-example/.envrc
export GOOGLE_PROJECT_ID=XXXXXXXXXXXX

direnv を有効にします。

# direnv の有効化
direnv allow

# 環境変数が export されていることを確認
echo $GOOGLE_PROJECT_ID

デプロイやトラフィックの移行は次のコマンドで実行できるようになります。

# デプロイ
make update version=<version>

# トラフィックの移行
make migrate version=<version>

おわりに

次回のテーマは『Circle CI』です。

よかったら Twitter フォローしてね。@_rema424

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
1