部署并在我的服务器上构建我的Go应用程序。

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

Deploy and build my go app on my server

问题

我完全是新手学习Go语言(看起来很棒)。所以我想用revel框架在Go中构建一个"web-"应用程序。问题是我在我的Mac上编码(操作系统:darwin,架构:amd64),而我想在我的服务器上部署应用程序(操作系统:ubuntu 12.04,架构:amd64)。我在本地使用"go get"命令获取了revel(所以bin/revel是一个Mach-O 64位可执行文件),但在我的服务器上无法执行。

目前,当我推送代码(使用git)时,我有一个post-receive脚本来构建应用程序(revel build myapp /path/to/deploy)。之前我尝试在服务器上使用"go get"命令获取revel,但也失败了。

这个方法不起作用,我可以理解为什么,但我不知道如何获得可行的工作流程:

  1. 在我的Mac上编码
  2. 推送我的代码(使用git)
  3. 在服务器上构建应用程序

PS:我已经阅读了http://blog.gopheracademy.com/auto-deploy-revel-site、http://revel.github.io/manual/deployment.html以及关于交叉编译的文章。

英文:

I'm totaly new to go (it looks fabulous btw).

So I want to build a "web-"app in go with the revel framework. The problem is I code on my mac (os : darwin, arch : amd64) and I want to deploy the app on my server (os : ubuntu 12.04, arch : amd64).
I "go get" revel in local (so <code>bin/revel</code> it's a: Mach-O 64-bit executable) which is non executable on my server.

For now when I push (with git), I've got a post-receive script to build the app (<code>revel build myapp /path/to/deploy</code>). Before I've tried to "go get" revel on my server, but it failed too.

It's not working, I could understand why, but I don't have any idea how to get a workable workflow :
<ol><li>Code on my mac</li><li>Push my code (with git)</li><li>Build the app on the server</li></ol>

PS: I've read http://blog.gopheracademy.com/auto-deploy-revel-site, http://revel.github.io/manual/deployment.html as well as articles about cross-compilation)

答案1

得分: 4

我不确定这是否有帮助,但是我来试试翻译:

你目前的情况正是我每天在开发我的第一个正式的Go Web应用时所做的。我在Windows笔记本电脑上进行开发(在工作时感到无聊...我在工作中是一名.NET开发人员!),在家里用Mac进行开发。然后,我将应用部署到托管在Digital Ocean上的Ubuntu服务器上。

我的工作流程是:

  • 进行任何更改。
  • 提交到代码仓库(BitBucket)。
  • 从仓库中拉取代码(可能是在我的Ubuntu服务器上)。
  • 在服务器上进行代码编译(使用go build命令)。
  • 获取任何在服务器上缺失的库(例如,今天服务器上没有gorilla/mux库,所以我只需运行相应的命令)。
  • 再次进行代码编译(如果需要的话)。

然后,在服务器上运行应用程序。

在开始使用这个工作流程时(我仍在尝试使用bash脚本等来完善它...),我发现在不同环境中使用相同的GOPATH非常有帮助。

例如,我每台机器上的GOPATH如下:

  • Windows:C:\GOPATH
  • Mac:~/go-code/
  • Linux:/home/simon/go-code/

它们的结构完全相同:

  • $GOPATH
    • src
      • github.com/
        • gorilla/
        • revel/
        • etc.../
      • Simon
        • WebApp1 <--- git仓库
          • .git
          • src
        • WebAPp2
        • WebApp3

...等等。这极大地简化了整个过程,并且使我能够在三个环境中无缝开发。

作为一个.NET开发人员,我仍在逐渐适应Go和这个设置,但目前看来它似乎还不错。

英文:

I'm not sure if this helps.. but here goes..

Your exact situation is what I am currently doing daily whilst I develop my first proper web app in Go. I develop on both a Windows laptop (whilst bored at work.. I'm a .NET developer at my workplace!) and my Mac at home. I then deploy it to an Ubuntu server hosted on Digital Ocean.

My workflow is:

  • Make any changes.
  • Commit to repository (BitBucket)
  • Pull from repo (wherever that may be - e.g, on my Ubuntu server)
  • go build the code in place on the server
  • go get any libraries that it complains about which I don't have on the server (for example, gorilla/mux wasn't on the server today so I just ran that)
  • go build again (if applicable)

..then just run it on the server.

When starting with this workflow (which I am still trying to perfect with bash scripts, etc ...) I found that a consistent GOPATH across environments really helps.

For example, my GOPATH on each machine is:

  • Windows: C:\GOPATH
  • Mac: ~/go-code/
  • Linux: /home/simon/go-code/

Each of them have exactly the same structure:

  • $GOPATH
    • src
      • github.com/
        • gorilla/
        • revel/
        • etc.../
      • Simon
        • WebApp1 <--- git repo
          • .git
          • src
        • WebAPp2
        • WebApp3

...etc. This greatly simplifies the entire thing and is what allows me to develop across 3 environments seamlessly.

I am still getting used to Go and this setup (being a .NET developer at my core) - but it seems to be doing the trick for now.

答案2

得分: 4

简单、易于测试的方法是在您的 Mac 上交叉编译一个 Linux amd64 二进制文件,然后使用 scp/Fabric/其他工具将二进制文件传输到您的服务器上。无需在服务器上获取依赖项(并有破坏的风险),也无需构建任何东西:您只需发送一个二进制文件。

选项 1:http://dave.cheney.net/2012/09/08/an-introduction-to-cross-compilation-with-go - 然后使用 go-linux-amd64 build 构建二进制文件,或者直接设置环境变量(如下面的选项所示)。

选项 2:使用 Homebrew 安装 Go brew install go --cross-compile-common,然后运行 GOOS=linux GOARCH=amd64 go build - 或者使用 -o 标志指定不同的输出文件名。我通常将输出文件命名为 myapp-linux,以免覆盖平台本机的二进制文件。

英文:

The simple, easy to test way is to cross-compile a Linux amd64 binary on your Mac and push the binary over to your server using scp/Fabric/other tool of choice here. There's no need to then fetch deps (and risk breaking things), build anything on your server: you just ship a binary.

Option 1: http://dave.cheney.net/2012/09/08/an-introduction-to-cross-compilation-with-go - and then build the binary using go-linux-amd64 build or by setting the environmental variables directly (as per the below option).

Option 2: install Go using Homebrew brew install go --cross-compile-common and then run GOOS=linux GOARCH=amd64 go build - or use the -o flag to specify a different output filename. I typically output mine as myapp-linux so it doesn't overwrite the platform native binary.

答案3

得分: -1

在类似的情况下,我想在Mac上编码,然后在Linux上部署。这种快速而简单的方法适用于Go 1.6.2版本。你可以在这个链接中找到详细步骤:http://kumargaurav.co/2016/08/10/deploy-go-lang-app-linux-server/

英文:

In a similar situation, I wanted to code on mac and deploy on Linux. This quick and dirty way worked for me on Go 1.6.2 http://kumargaurav.co/2016/08/10/deploy-go-lang-app-linux-server/

huangapple
  • 本文由 发表于 2014年7月30日 04:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/25024893.html
匿名

发表评论

匿名网友

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

确定