本文讨论如何通过写个简单的 makefile 来达到自动化部署的目的。
1. 本地化服务器配置
图中,_deploy目录下的内容就是我们要部署到服务器上的配置,其目录结构和服务器要完全一致,这样可以用 cp 直接將文件拷贝过去: $ sudo cp -r _deploy/etc/. /etc/.
$ sudo cp /bin/cp /bin/sucopy $ sudo visudo
dev ALL=(ALL) NOPASSWD: /usr/bin/supervisorctl, /etc/init.d/nginx, /bin/sucopy, /usr/bin/apt-get
sudo sucopy -r _deploy/etc/. /etc/.
tchen@tchen-mbp: ~/projects/kahn/_deploy/etc/nginx/sites-available on master$ cat kahn server { listen 80; server_name api.awesome-server.com; access_log /var/log/nginx/kahn.access.log; error_log /var/log/nginx/kahn.error.log; location / { proxy_pass http://localhost:6080; include /etc/nginx/proxy_params; } } tchen@tchen-mbp: ~/projects/kahn/_deploy/etc/nginx/sites-enabled on master$ ls -l total 8 lrwxr-xr-x 1 tchen 522017917 23 Jun 11 13:48 kahn -> ../sites-available/kahn
tchen@tchen-mbp: ~/projects/kahn/_deploy/etc/supervisor/conf.d on master$ cat kahn.conf [program:kahn] directory=/home/dev/deployment/kahn user=dev command=coffee app.coffee redirect_stderr=true stderr_logfile=none stdout_logfile=/var/log/supervisor/kahn.log autostart=true autorestart=true
2. Makefile 解决了服务器的本地化配置问题,自动化部署就不在话下。这里只自动化了下列任务,对简单的小项目来说足够了:
Makefile文件比较简单,不解释了。注意这里我们把 remote_deploy 放在第一位,让 make 缺省调用时可以调用它,这样,我们会自动登录到服务器上(服务器的连接请使用rsa key,避免输入密码),pull代码,然后执行 make deploy 进行部署。 tchen@tchen-mbp: ~/projects/kahn on master$ cat Makefile CHECK=\033[32m?\033[39m DONE="\n$(CHECK) Done.\n" SERVER=awesome-server.com PROJECT=kahn PATH=deployment/$(PROJECT) SUPERVISORCTL=/usr/bin/supervisorctl SUCOPY=/bin/sucopy SSH=/usr/bin/ssh ECHO=/bin/echo -e NPM=/usr/local/bin/npm SUDO=/usr/bin/sudo remote_deploy: @$(SSH) -t $(SERVER) "echo Deploy $(PROJECT) to the $(SERVER) server.; cd $(PATH); git pull; make deploy;" dependency: @$(ECHO) "\nInstall project dependencies..." @$(NPM) install configuration: @$(ECHO) "\nUpdate configuration..." @$(SUDO) $(SUCOPY) -r _deploy/etc/. /etc/. supervisor: @$(ECHO) "\nUpdate supervisor configuration..." @$(SUDO) $(SUPERVISORCTL) reread @$(SUDO) $(SUPERVISORCTL) update @$(ECHO) "\nRestart $(PROJECT)..." @$(SUDO) $(SUPERVISORCTL) restart $(PROJECT) nginx: @$(ECHO) "\nRestart nginx..." @$(SUDO) /etc/init.d/nginx restart deploy: dependency configuration supervisor nginx @$(ECHO) $(DONE)
tchen@tchen-mbp: ~/projects/kahn on master$ make Deploy kahn to the awesome-server.com server. Current branch master is up to date. Install project dependencies... Update configuration... Update supervisor configuration... No config updates to processes Restart kahn... kahn: stopped kahn: started Restart nginx... Restarting nginx: nginx. ✔ Done. Shared connection to awesome-server.com closed.原文:http://tchen.me/posts/2013-06-12-use-makefile-to-automate-deployment.html 转载请保留固定链接: https://linuxeye.com/Linux/1751.html |