Skip to content

Instantly share code, notes, and snippets.

@kyokomi
Last active September 16, 2020 08:29
Show Gist options
  • Save kyokomi/70c6ff4ca109bbba60c7 to your computer and use it in GitHub Desktop.
Save kyokomi/70c6ff4ca109bbba60c7 to your computer and use it in GitHub Desktop.
martiniを使ったgolangのwebappをherokuに載せるときのチートシート

地味に毎回やってるから自動化してサクッと開発を始めたい。

martini-heroku-init.goみたいなの作ってもいいかも?

server.go

※templateかな?

package main

import (
	"github.com/go-martini/martini"
)

func main() {
	m := martini.Classic()
	m.Get("/", func() string {
		return "Hello world!"
	})
	m.Run()
}

git

git initしないとgodepが使えない。

$ git init

godep

$ godep save

.godir

※2015/11/21 heroku公式Goサポートで不要になった。

$ echo ${PWD##*src/} > .godir

Procfile

$ echo "web: $(basename `pwd`)" > Procfile

heroku create

$ heroku create {name}

Wercker

herokuのAccount情報にあるAPIKeyをWerckerの対象プロジェクトのDeploys設定のタブで選択して、デプロイTargetとか設定。

wercker.ymlの最後に以下を追加してgithubにpushで完了。

box: wercker/golang
# Build definition
build:
  # The steps that will be executed on build
  steps:
    # Sets the go workspace and places you package
    # at the right place in the workspace tree
    - setup-go-workspace

    # Gets the dependencies
    - script:
        name: go get
        code: |
          cd $WERCKER_SOURCE_DIR
          go version
          go get -t ./...

    # Build the project
    - script:
        name: go build
        code: |
          go build ./...

    # Test the project
    - script:
        name: go test
        code: |
          go test ./...
deploy:
  steps:
    - heroku-deploy:
        key-name: MY_DEPLOY_KEY

WerckerのWeb上でMyAppのSettingで、masterをauto deployに指定する。 (スペース区切りで複数のブランチを指定可能)

あと、ssh-key設定しないと毎回新しいssh-keyでherokuにアクセスしてメールバンバンくるから設定したほうがいい。

heroku-deployのstepはこちら

https://github.com/wercker/step-heroku-deploy

package main
import (
"github.com/go-martini/martini"
)
func main() {
m := martini.Classic()
m.Get("/", func() string {
return "Hello world!"
})
m.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment