はじめに
最近、Hugoでブログを作って、GCPのGAE(Google App Engine)で公開した。
公開したブログ
選定理由などはブログに書いているが、GAEに公開する所でちょっと苦戦した部分を記載する。
参考にしたサイト
Hugoでブログつくるまで
https://qiita.com/bake0937/items/e0914efbd9434be474a4
GAEでの公開
https://blog.youyo.info/post/2015/10/23/publish-hugo-in-gae/
GAEへの公開でエラー
deployでエラー1
$ gcloud app deploy
ERROR: (gcloud.app.deploy) The [application] field is specified in file [/home/hoge/dir/app.yaml]. This field is not used by gcloud and must be removed. Project name should instead be specified either by `gcloud config set project MY_PROJECT` or by setting the `--project` flag on individual command executions.
-
yamlの様式が変わった模様
http://hamarimakuri.blogspot.com/2018/03/gae.html -
ドキュメントみて修正
deployでエラー2
$ gcloud app deploy
The component [app-engine-go] is required for staging this
application.
ERROR: (gcloud.app.deploy) You cannot perform this action because this Cloud SDK installation is managed by an external package manager.
Please consider using a separate installation of the Cloud SDK created through the default mechanism described at: https://cloud.google.com/sdk/
-
gcloudコマンドをaptで入れたため、古かったのが原因っぽい
https://qrunch.net/@regonn/entries/sIescQ9qdgJZ0qWa
deployでエラー3
$ gcloud app deploy
ERROR: (gcloud.app.deploy) Staging command [/home/hoge/google-cloud-sdk/platform/google_appengine/go-app-stager /home/hoge/dir/app.yaml /home/hoge/dir /tmp/tmprq6eNx/tmpSQWQij] failed with return code [1].
------------------------------------ STDOUT ------------------------------------
------------------------------------ STDERR ------------------------------------
2020/01/26 18:44:00 staging for go1.11
2020/01/26 18:44:00 GO111MODULE=auto, but no go.mod found, so building with dependencies from GOPATH
2020/01/26 18:44:00 Staging second-gen Standard app (GOPATH mode): failed analyzing /home/hoge/dir: could not get package for dir "/home/hoge/dir": no buildable Go source files in /home/hoge/dir
GOPATH: /home/hoge/dir/.go
--------------------------------------------------------------------------------
- goのコードが存在しない事が原因のよう
-
ドキュメントとGitHubのサンプルを見つつ、
main.go
を作成
デプロイできた!
出来上がった設定ファイル
app.yaml
runtime: go111
handlers:
- url: /(.*\.css)
mime_type: text/css
static_files: public/\1
upload: public/(.*\.css)
- url: /(.*\.html)
mime_type: text/html
static_files: public/\1
upload: public/(.*\.html)
secure: always
- url: /(.*\.ico)
mime_type: image/x-icon
static_files: public/\1
upload: public/(.*\.ico)
- url: /(.*\.js)
mime_type: text/javascript
static_files: public/\1
upload: public/(.*\.js)
- url: /(.*\.ttf)
mime_type: font/truetype
static_files: public/\1
upload: public/(.*\.ttf)
- url: /(.*\.woff)
mime_type: application/x-font-woff
static_files: public/\1
upload: public/(.*\.woff)
- url: /(.*\.xml)
mime_type: application/xml
static_files: public/\1
upload: public/(.*\.xml)
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: public/\1
upload: public/(.*\.(bmp|gif|ico|jpeg|jpg|png))
- url: /(.+)/
static_files: public/\1/index.html
upload: public/(.+)/index.html
secure: always
- url: /(.+)
static_files: public/\1/index.html
upload: public/(.+)/index.html
secure: always
- url: /
static_files: public/index.html
upload: public/index.html
secure: always
※元の設定ファイルだとhttpでアクセスできてしまうので「secure: always」を追加しました。
Google App Engineにデプロイしたアプリへの接続を常時https化する
main.go
package main
import (
// "fmt"
"log"
"net/http"
"os"
)
func main() {
// http.HandleFunc("/", indexHandler)
// [START setting_port]
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
// [END setting_port]
}
最後に
ちょっと苦戦はしたけど、Hugoは楽ちんです!