LoginSignup
18
7

More than 5 years have passed since last update.

Makefileを使った作業の共通化

Posted at

streampack の Tana です。

アプリケーションが増え、マイクロサービス化されてくると、
アプリケーション固有なコマンドや環境によってそれぞれ実行方法が異なります。
また、何回もコマンドを叩かないといけないケースだとハードルが上がりますし、
コマンドを忘れたりもして悩ましい時がたまにあります。

  • セットアップ・インストール
  • アプリ起動
  • コンパイル
  • リリース(デプロイ)

上記の振る舞いを Makefile を使って共通化する方法です。
Makefile といえば、以前は C++ とかのコンパイルとかに
よく使われていたかと思いますが、コマンドの統一化などには便利です。

例えば、Rails と Golang のアプリケーションがあったとします。
それぞれの Makefile を準備します。

Makefile(Rails用)

help: ## Usage
    @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

install: ## install gems
    bundle install

db: ## database create & migrate
    rails db:create
    rails db:migrate
    rails db:seed

run: ## start the app
    rails server

build: ## build image
    docker build

push: ## push to somewhere
    docker push xxx

release: build push ## build&pushしてリリース

Makefile(Golang用)

help: ## Usage
    @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

install: ## install packages
    go get github.com/golang/xxx

run: ## start the app
    go run main.go

build: ## build image
    docker build

push: ## push to somewhere
    docker push xxx

release: build push ## build&pushしてリリース

あとは、make help でコマンドを確認して、叩くだけです。

help                           Usage
install                        install packages
run                            start the app
build                          build image
release                        push to somewhere

install したければ、

$ make install

リリースしたければ、

$ make release

コマンドを統一することにより
誰もが同じコマンドを叩くことによって
環境を構築したり、起動したり、リリースすることも可能です。
README.md の手順もシンプルになります。
$ make all で全てできればクールですね。

もっと汎用的に docker関連でいろんなことをやりたいのであれば、
こちらが便利なので、参考にしてみてください。
https://gist.github.com/mpneuried/0594963ad38e68917ef189b4e6a269db

ではでは。

18
7
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
18
7