LoginSignup
43
35

More than 5 years have passed since last update.

GAE/GoのWebアプリをCircleCIで自動テスト&自動デプロイする

Last updated at Posted at 2014-10-11

はじめに

以前投稿した「IntelliJ IDEAを使ってGAE/GoのHelloWorldする」のGAE/Goで作ったWebアプリを、CircleCIからGoogleAppEngineへdeployする手順の説明です。

前提条件・環境

  • GoogleAppEngineでアカウントが登録済み
  • CircleCIでアカウント登録済み
  • GAE/GoのWebアプリのリポジトリがGithubにある

CircleCIの設定

プロジェクト直下にcircle.ymlを追加

今回は、appengineのバージョン1.9.12の例。

circle.yml
machine:
  timezone:
    Asia/Tokyo
dependencies:
  pre:
    - curl -o $HOME/go_appengine_1.9.12.zip https://storage.googleapis.com/appengine-sdks/featured/go_appengine_sdk_linux_amd64-1.9.12.zip
    - unzip -q -d $HOME $HOME/go_appengine_1.9.12.zip
  override:
    - $HOME/go_appengine/goapp build
test:
  override:
    - $HOME/go_appengine/goapp test
deployment:
  appengine:
    branch: master
    commands:
      -  $HOME/go_appengine/appcfg.py --oauth2_refresh_token=$APPENGINE_TOKEN update .

refresh_tokenを用意する

一回だけ手動でデプロイする必要があります。
これ以外でrefresh_tokenを作成する方法がわからなかったので。。。

以下は、ホーム直下にgo_appengineをインストールした例です。

console
$ ~/go_appengine/appcfg.py --oauth2 --noauth_local_webserver update .

こんな感じでブラウザURLが出てきますので、oauth認証を行います。
発行されたコードをEnter verification code:に入力してそのまま1回deployします。

console-log
01:41 PM Application: xxxxxxx; version: 1
01:41 PM Host: appengine.google.com
01:41 PM
Starting update of app: xxxxxxx, version: 1
01:41 PM Getting current resource limits.
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?scope=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Enter verification code:

deploy成功すれば、ホーム直下にappcfg_oauth2_tokensというファイルができているので、この中にあるrefresh_tokenをメモします。

console
$ cat ~/.appcfg_oauth2_tokens

こんなjsonのやつです。

{
    "_module": "oauth2client.client",
    "_class": "OAuth2Credentials",
    "access_token": "xxxxxxxxxxx-xxxxxxx- xxxxxxxx",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "invalid": false,
    "client_id": "1111111111111.apps.googleusercontent.com",
    "id_token": null,
    "client_secret": "xxxxxxxxxxxxxxx",
    "token_expiry": "2014-07-26T12:42:51Z",
    "refresh_token": "1/xxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "user_agent": "appcfg_go/1.9.6 Darwin/13.3.0 Python/2.7.8.final.0"
}

CircleCIにプロジェクトを追加しAPPENGINE_TOKENを設定

CircleCIログイン後の左メニューにあるAdd ProjectsからGAE/GoプロジェクトのGithubを選択します。

最初のbuildが走ってしまいますが、そこは気にしないでproject settingsを開きます。

Environment variablesを選択して、先ほどのcircle.ymlに記載した環境変数APPENGINE_TOKENを追加します。

設定する値は、先ほど手動deploy時に発行されたrefresh_tokenを設定します。

スクリーンショット_2014-10-11_17_35_19.png

CircleCIのGOPATHを変更する

GAE/Goはプロジェクト直下をGOPATHとして動くため、それにあわせる(そうしないとsubpackage等がbuildエラーになる)

Environment variablesを選択して、GOPATH$HOME/$CIRCLE_PROJECT_REPONAMEを設定する。

おわり

これでRebuildもしくは、変更をgithubにpushした時にbuildが走ってtestが通ればGoogleAppEngineにdeployされます。

スクリーンショット 2014-10-11 17.51.40.png

ちなみに最初は、CircleCIのドキュメントに書かれているemailとpassを環境変数に持たせるやつを試していたんですが、最近のバージョンから廃止されたみたいで、かなりハマってました。。。OTL

使用したソースコードなど

試したソースコード等は、Githubで公開しているので参考にしていただければと思います。

参考

43
35
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
43
35