|
| 1 | +# This is the deployment recipe |
| 2 | +# You require to install runit on the server. |
| 3 | +# First you need to declare on which port the application will run |
| 4 | +# |
| 5 | +# echo 3000 > port |
| 6 | +# |
| 7 | +# Then you can setup a new runit service with |
| 8 | +# |
| 9 | +# make setup_service |
| 10 | +# |
| 11 | +# Also you need to install the git hooks, so you can push and update this repo |
| 12 | +# |
| 13 | +# make setup_git_hook |
| 14 | +# |
| 15 | +# Every time you push it will run |
| 16 | +# |
| 17 | +# make deploy |
| 18 | +# |
| 19 | +# Later you can test the application |
| 20 | +# |
| 21 | +# make service_run |
| 22 | +# |
| 23 | +# Last you can restart the application |
| 24 | +# |
| 25 | +# make restart |
| 26 | +# |
| 27 | +SHELL=/usr/local/rvm/bin/rvm-shell |
| 28 | + |
| 29 | +## service |
| 30 | +service_name = $(shell basename $(realpath .) ) |
| 31 | +app_dir = $(realpath .) |
| 32 | +service_dir = $(HOME)/service/$(service_name) |
| 33 | +run = $(service_dir)/run |
| 34 | +log_dir = $(service_dir)/log |
| 35 | +log = $(log_dir)/run |
| 36 | +port = $(shell cat port) |
| 37 | + |
| 38 | +setup_run: |
| 39 | + mkdir -p $(service_dir) |
| 40 | + echo "#!/bin/bash" > $(run) |
| 41 | + echo "cd $(app_dir)" >> $(run) |
| 42 | + echo "export HOME=$$(HOME)" >> $(run) |
| 43 | + echo "exec chpst -u $(shell whoami) make service_run" >> $(run) |
| 44 | + chmod +x $(run) |
| 45 | + |
| 46 | +setup_log: |
| 47 | + mkdir -p $(log_dir) |
| 48 | + echo "#!/bin/bash" > $(log) |
| 49 | + echo "exec chpst -u $(shell whoami) svlogd -tt $(app_dir)/log/" >> $(log) |
| 50 | + chmod +x $(log) |
| 51 | + |
| 52 | +setup_service: setup_run setup_log |
| 53 | + @echo "" |
| 54 | + @echo " You created a new runit service that will keep the application runinnig after a reboot" |
| 55 | + @echo " also it will create a log on log folder" |
| 56 | + @echo " just, one last step: run as sudo" |
| 57 | + @echo " cp -r $(service_dir) /etc/service/" |
| 58 | + @echo " chown $(shell whoami) /etc/service/$(service_name)/supervise/ " |
| 59 | + @echo " chown $(shell whoami) /etc/service/$(service_name)/supervise/ok " |
| 60 | + @echo " chown $(shell whoami) /etc/service/$(service_name)/supervise/control " |
| 61 | + @echo " chown $(shell whoami) /etc/service/$(service_name)/supervise/status" |
| 62 | + @echo "" |
| 63 | + |
| 64 | +service_run: |
| 65 | + |
| 66 | +## git hook |
| 67 | +hook = .git/hooks/post-receive |
| 68 | + |
| 69 | +setup_git_hook: |
| 70 | + echo 'unset GIT_DIR' |
| 71 | + echo 'cd .. && GIT_DIR=$$(pwd) && make git_hook deploy' > $(hook) |
| 72 | + chmod +x $(hook) |
| 73 | + |
| 74 | +git_hook: |
| 75 | + env -i git reset --hard |
| 76 | + env -i git submodule init |
| 77 | + env -i git submodule update |
| 78 | + |
| 79 | +## rails |
| 80 | +export RAILS_ENV = $(shell git branch | grep '*' | awk '{print $$(2)}' ) |
| 81 | + |
| 82 | +bundle_install: |
| 83 | + bundle install --without=development --without=test --deployment |
| 84 | + |
| 85 | +migrate: |
| 86 | + bundle exec rake db:migrate |
| 87 | + |
| 88 | +precompile: |
| 89 | + bundle exec rake assets:clean assets:precompile |
| 90 | + |
| 91 | +all: deploy |
| 92 | + |
| 93 | +deploy: bundle_install migrate precompile restart |
| 94 | + |
| 95 | +# application specific |
| 96 | + |
| 97 | +service_run: |
| 98 | + mkdir -p tmp/pids tmp/logs |
| 99 | + exec bundle exec puma -e $(RAILS_ENV) -p $(port) |
| 100 | + |
| 101 | +restart: |
| 102 | + sv restart $(service_name) |
| 103 | + |
0 commit comments