How to deploy Go program from windows to CentOS server

huangapple go评论69阅读模式
英文:

How to deploy Go program from windows to CentOS server

问题

我有一个在Windows上运行的Go包,目前工作正常,但现在我想在生产环境的CentOS 6.5服务器上进行测试。

从Windows部署到CentOS的最佳实践是什么?

我需要使用我的Git仓库将代码分发到Linux操作系统,然后编译并部署二进制文件到服务器吗?

另外,我有多个文件,所以我想go build *.go应该足够了,或者还有更好的编译选项吗?

英文:

I have a Go package running on Windows and is working fine but now I'm at a stage where I would like to test this on production CentOS 6.5 server.

What is the best practice to deploy this from Windows to CentOS?

Would I have to use my Git repo to distribute to Linux operating system, compile then deploy the binary to the server?

Also I have multiple files, so I would imagine go build *.go would suffice or are there better options for doing compilation?

答案1

得分: 1

在将代码从Windows部署到CentOS的最佳实践中,我建议使用持续集成。你可以设置Jenkins,或者使用一些云选项,如codeship.io、travis-ci.org、drone.io、wercker.com等。其中一些平台提供免费计划。

基本上,你需要将代码提交到Git并推送到Github(或者如果你想要免费的私有仓库,可以使用Bitbucket)。当你推送更改时,持续集成服务器将收到通知,并构建、测试并创建你的项目的发布tar归档文件。然后,你可以将生成的tar文件下载到你的CentOS服务器上。在CentOS 6.5中,你需要创建一个init.d脚本来保持你的程序运行。你可以在这里看到一个示例(system v脚本)。

CentOS 7现在使用systemd,设置会稍微容易一些。

进一步地,你还可以设置持续部署,其中下载、解压和安装也可以自动化。根据你的项目,设置持续部署可能有意义,也可能没有意义(自动推送到生产环境可能有点过于自动化)。你可以在wercker的这个示例中找到一个例子。

虽然设置持续集成需要一些初始成本,但如果这是一个其他人会贡献代码或者你打算长期开发的项目,这个成本绝对是值得的。(当你在6个月后回到这个项目,改变一行代码时,你将会感激不已,因为你不必记住所有手动部署所需的步骤)

英文:

> What is the best practice to deploy this from Windows to CentOS?

As far as best practices go I would recommend using continuous integration. You can setup jenkins, or there are some cloud options out there: codeship.io, travis-ci.org, drone.io, wercker.com, ... Some of them have free plans available.

Basically you'd commit your code to git and push that out to Github (or Bitbucket if you want free private repos). The continuous integration server will be notified whenever you push out changes, and will build, test and create a release tar archive of your project. You can then take this resulting tar and download it to your CentOS box. In 6.5 you'll need to create an init.d script to keep your program up and running. You can see an example here (the system v script).

CentoOS 7 uses systemd now which would be slightly easier to setup.

Taking this one step further it's also possible to setup continuos deployment, in which the download, extraction and installation can also be automated. Depending on your project it may or may not make sense to set up continuous deployment. (Auto-pushing to production might be a little too automatic) You can find an example in wercker here.

Although there is an an up-front cost to setting up continuous integration if this is a project that other people will contribute too, or one that you intend to work on long-term, the cost will definitely be worth it. (Future you will be greatful when you come back to this project 6 months from now, change 1 line of code, and don't have to remember all the manual steps it took to deploy)

huangapple
  • 本文由 发表于 2014年9月21日 16:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/25957481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定