LoginSignup
2
3

More than 3 years have passed since last update.

サードパッケージを使用したGoのアプリケーションをHerokuにデプロイする

Last updated at Posted at 2019-11-24

2020/02/29 現在この記事のやり方ではデプロイできません。下記、リンク参照
govendorで依存関係のファイルが取得できないエラーについて
go modulesを使用したやり方
サードパッケージを使用したGoのアプリケーションをHerokuにデプロイする v2

概要

Portfolioを作るに当たってGoでHerokuにデプロイしようとしたらハマった
結論からいうとパッケージのコミット漏れ

Herokuで公式HPにデプロイ方法は載っていたが、サードパッケージを使用した際はハマるので今回の事象を押さえておく

Goでアプリケーションを作成

今回はginでプロジェクトを作成する
Heroku用のフォルダを作成し、以下のファイルを配置

main.go
package main

import (
    "github.com/gin-gonic/gin"
)

type User struct {
    Name string
    Age  int
}

func main() {
    router := gin.Default()
    // css、js などの静的ファイルを読み込む場合。今回は使用しない。
    // router.Static("/assets", "./assets")

    router.LoadHTMLGlob("templates/*.html")

    router.GET("/", handler)

    router.Run()
}

func handler(ctx *gin.Context) {

    user := User{"User", 20}

    ctx.HTML(200, "index.html", gin.H{
        "user": user,
    })
}
templates/index.html
<!DOCTYPE html>
<html>
    <div>
        <p>Name: {{.user.Name}} </p>
        <p>Age: {{.user.Age}} </p>
    </div>
</html>

govendorを使用してサードパッケージを追加

govendor をインストールしてから行ってください。
https://github.com/kardianos/govendor

govendorについては下記の記事がとても理解できたので参考にさせて頂きました。
Heroku への Go 言語製アプリケーションのデプロイと依存パッケージ管理方法の比較

# 初期化
$ govendor init
# 依存パッケージのダウンロード
$ govendor fetch +out

後はHeroku公式のコマンド通り、コミット→プッシュを行う

$ git init && git add -A .
Initialized empty Git repository in /Users/kazu/workspace/go/src/Heroku/.git/

$ git commit -m "init"
create mode 100644 main.go
create mode 100644 templates/index.html
〜〜〜 以下、vendor配下のファイル 〜〜〜〜〜〜
 create mode 100644 vendor/***
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
create mode 100644 vendor/vendor.json

Herokuのアプリを作成

$ heroku create
Creating app... done, ⬢ ancient-mesa-82891
https://ancient-mesa-82891.herokuapp.com/ | https://git.heroku.com/ancient-mesa-82891.git
$ git push heroku master

Goのアプリケーションのデプロイ完了

ちなみにpushする際にgovendor fetch +out
のコマンドを実行していない場合、下記のようなエラーが出る。


-----> Go app detected
-----> Fetching jq... done
-----> Fetching stdlib.sh.v8... done
-----> Checking vendor/vendor.json file.
 !!    The 'heroku.goVersion' field is not specified in 'vendor/vendor.json'.
 !!    
 !!    Defaulting to go1.12.12
 !!    
 !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-govendor#build-configuration
 !!    
-----> New Go Version, clearing old cache
-----> Installing go1.12.12
-----> Fetching go1.12.12.linux-amd64.tar.gz... done
-----> Fetching govendor... done
 !!    Installing package '.' (default)
 !!    
 !!    To install a different package spec set 'heroku.install' in 'vendor/vendor.json'
 !!    
 !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-govendor#build-configuration
 !!    
-----> Fetching any unsaved dependencies (govendor sync)
-----> Running: go install -v -tags heroku . 
main.go:4:2: cannot find package "github.com/gin-gonic/gin" in any of:
    /tmp/tmp.hTplalCHiU/.go/src/Portfolio/vendor/github.com/gin-gonic/gin (vendor tree)
    /app/tmp/cache/go1.12.12/go/src/github.com/gin-gonic/gin (from $GOROOT)
    /tmp/tmp.hTplalCHiU/.go/src/github.com/gin-gonic/gin (from $GOPATH)
 !     Push rejected, failed to compile Go app.
 !     Push failed

HerokuのGithubにサードパッケージをコミットできていないためビルドエラー起きてしまうとのことでした。
公式のやり方だけでは静的なアプリケーションをデプロイできてもサードパッケージを使用した際にできないため、少し工夫が必要でした。
GoPathの考え方に慣れていかないとですかね。

アプリケーション名の変更

Heroku公式の通りに進めていくとアプリケーション名が適当な文字列で作成されてしまい
URLもそのままの文字列になってしまうため、変更したい場合はHerokuのマイページ→設定から変更しましょう。
その場合はgitで作成したリモートのパスを合わせて変更する事を忘れずに

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3