Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy and build my go app on my server

Tags:

git

linux

macos

go

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 bin/revel 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 (revel build myapp /path/to/deploy). 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 :

  1. Code on my mac
  2. Push my code (with git)
  3. Build the app on the server

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)

like image 304
Zico Avatar asked Jul 29 '14 20:07

Zico


People also ask

How do I deploy a service in go?

To successfully deploy a service in the Go 1.12+ runtimes, a package main statement must be defined at the beginning of at least one of your Go source files. Learn more: You do not have to put the app.yaml file and the main.go file in the same directory.

How do I build and install a program in go?

To do this, you can use the Go toolchain to build and install your program. In Go, the process of translating source code into a binary executable is called building. Once this executable is built, it will contain not only your application, but also all the support code needed to execute the binary on the target platform.

How do I create a Go app for App Engine?

In your go-app/ folder, create a file called app.yaml, and add the following contents: This is the simplest configuration for an App Engine app. It indicates to App Engine that you're using Go. The app.yaml file can specify other Go versions, network settings, scaling settings, and more.

How do I deploy a service from an App Engine project?

Every App Engine project has an app.yaml configuration file which specifies your service's runtime environment settings. Your service will not deploy without this file. Create a new folder called go-app for your Go 1.12+ service: In your go-app/ folder, create a file called app.yaml, and add the following contents:


2 Answers

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.

like image 144
Simon Whitehead Avatar answered Oct 19 '22 16:10

Simon Whitehead


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.

like image 27
elithrar Avatar answered Oct 19 '22 18:10

elithrar