简体   繁体   English

如何设置Git将python应用程序文件部署到Ubuntu Server中?

[英]How to setup Git to deploy python app files into Ubuntu Server?

I setup a new Ubuntu 12.10 Server on VPN hosting. 我在VPN托管上设置了新的Ubuntu 12.10服务器。 I have installed all the required setup like Nginx, Python, MySQL etc. I am configuring this to deploy a Flask + Python app using uWSGI. 我已经安装了所有必需的安装程序,例如Nginx, Python, MySQL等。我正在配置它以使用uWSGI部署Flask + Python应用程序。 Its working fine. 它的工作正常。

But to create a basic app i used Putty tool (from Windows) and created required app .py files. 但是,要创建一个基本应用程序,我使用了Putty工具(从Windows)并创建了所需的应用程序.py文件。

But I want to setup a Git functionality so that i can push my code to required directory say /var/www/mysite.com/app_data so that i don't have to use SSH or FileZilla etc everytime i make some changes into my website. 但是我想设置一个Git功能,这样我就可以将我的代码推送到要求的目录,例如/var/www/mysite.com/app_data这样我每次对网站进行一些更改时都不必使用SSHFileZilla等。

Since i use both Ubuntu & Windows for development of app, setting up a Git kind of functionality would help me push or change my data easily to my Cloud Server. 由于我同时使用Ubuntu和Windows进行应用程序开发,因此设置Git功能将有助于我轻松地将数据推送或更改到Cloud Server。

How can i setup a Git functionality in Ubuntu ? 如何在Ubuntu中设置Git功能? and How could i access it and Deploy data using tools like GitBash etc. ? 以及如何使用GitBash等工具访问它并部署数据?

Please Suggest 请建议

  • Create a bare repository on your server. 在服务器上创建一个裸存储库。
  • Configure your local repository to use the repository on the server as a remote. 配置本地存储库以将服务器上的存储库用作远程服务器。
  • When working on your local workstation, commmit your changes and push them to the repository on your server. 在本地工作站上工作时,提交所做的更改并将其推送到服务器上的存储库。
  • Create a post-receive hook in the server repository that calls "git archive" and thus transfers your files to some other directory on the server. 在服务器存储库中创建一个称为“ git archive”的接收后挂钩,从而将文件传输到服务器上的其他目录。

Modified version of innaM: innaM的修改版本:

Concept 概念

Have three repositories 有三个存储库

  1. devel - development on your local development machine 开发-在本地开发机器上进行开发
  2. central - repository server - like GitHub, Bitbucket or anything other 中央-储存库服务器-像GitHub,Bitbucket或其他任何东西
  3. prod - production server 产品-生产服务器

Then you commit things from devel to central and as soon as you want to deploy on prod, than you ask prod to pull data from prod. 然后,您将事情从开发提交到中央,并在要部署到产品上时立即提交,而不是要求产品从产品中提取数据。

"asking" prod server to pull the updates can be managed by cron (then you have to wait a moment) or you may use other means like one shot call of ssh asking to do git pull and possibly restart your app. “询问”产品服务器以获取更新可以由cron管理(然后您必须等待一会儿),或者您可以使用其他方式,例如ssh的一次调用,要求执行git pull并可能重新启动您的应用程序。

Step by step 一步步

In more details you can go this way. 更详细地讲,您可以采用这种方式。

Prepare repo on devel 在开发中准备回购

Develop and test the app on your devel server. 在您的devel服务器上开发和测试该应用。

Put it into local repository: 将其放入本地存储库:

$ git init
$ git add *
$ git commit -m "initial commit"

Create repo on central server 在中央服务器上创建仓库

Eg bitbucket provides this description: https://confluence.atlassian.com/display/BITBUCKET/Import+code+from+an+existing+project 例如,bitbucket提供了以下描述: https : //confluence.atlassian.com/display/BITBUCKET/Import+code+from+an+existing+project

Generally, you create the project on Bitbucket, find the url of it and then from your devel repo call: 通常,您在Bitbucket上创建项目,找到它的URL,然后从devel repo调用中进行:

$ git remote add origin  <bitbucket-repo-url>
$ git push origin

Clone central repo to prod server 将中央存储库克隆到产品服务器

Log onto your prod server. 登录到您的产品服务器。

Go to /var/www and clone form bitucket: 转到/ var / www并克隆bitucket:

$ cd /var/www
$ git clone <bitbucket-repo-url>
$ cd mysite.com

and you shall have your directory ready. 您应该已经准备好目录。

Trigger publication of updates to prod3 触发发布对prod3的更新

There are numerous options. 有很多选择。 One being a cron task, which would regularly call 一个是cron任务,会定期调用

$ git pull

In case, your app needs restart afte an update, then you have to ensure, the restart would happen (this shall be possible using git log command, which will show new line after the update, or you may check, if status code would tell you. 万一您的应用需要在更新后重新启动,则必须确保重新启动会发生(这可以使用git log命令执行,该命令将在更新后显示新行,或者您可以检查状态代码是否可以告诉您您。

Personally I would use "one shot ssh" (you asked not to use ssh, but I assume you are asking for "simpler" solution, so one shot call shall work simpler then using ftp, scp or other magic. 就我个人而言,我将使用“单次ssh”(您要求不使用ssh,但我假设您正在要求“更简单”的解决方案,因此一次射击应比使用ftp,scp或其他魔术方法更简单。

From your devel machine (assuming you have ssh access there): 在您的开发机器上(假设您在那里有ssh访问权限):

$ ssh user@prod.server.com "cd /var/www/mysite.com && git pull origin && myapp restart"

Advantage is, that you do control the moment, the update happens. 好处是,您可以控制瞬间,即会进行更新。

Discussion 讨论区

I use similar workflow. 我使用类似的工作流程。

rsync seems in many cases serve well enough or better (be aware of files being created at app runtime and by files in your app, which shall be removed during ongoing versions and shall be removed on server too). rsync在许多情况下似乎可以很好地发挥作用或更好(请注意在应用程序运行时以及应用程序中的文件所创建的文件,这些文件应在进行中的版本中删除,并且也应在服务器上删除)。

salt (saltstack) could serve too, but requires a bit more learning and setup). saltsalt堆)也可以使用,但需要更多的学习和设置)。

I have learned, that keeping source code and configuration data in the same repo makes sometime situation more dificult (that is why I am working on using salt). 我了解到,将源代码和配置数据保存在同一存储库中会使某些情况更加困难(这就是为什么我正在使用salt的原因)。

fab command from Fabric (python based) may be best option (in case installation on Windows becomes difficult, look at http://ridingpython.blogspot.cz/2011/07/installing-fabric-on-windows.html Fabric(基于python)的fab命令可能是最好的选择(以防在Windows上安装变得困难,请查看http://ridingpython.blogspot.cz/2011/07/installing-fabric-on-windows.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM