LoginSignup
6
3

More than 5 years have passed since last update.

AWS CodeBuildでGoのプロジェクトをビルドしてみる

Posted at

今年のAWS RE:INVENTは盛り沢山でしたね。今回は、そのうちの1つ、CodeBuildを試してみようと思います。基本的にはGetting Startedに従って進めていきます。

1. ビルドのOutputを格納するS3バケットを作る

ビルドのOutputを格納するため、codebuild-demo-project-outputという名前でS3のバケットを作成する。

2. コードを書く

早速Goのコードを書く。今回は、codebuildというディレクトリを作成し、シンプルにSum関数を1つだけ持つコードを作った。

codebuild.go
package codebuild

// Sum adds two numbers.
func Sum(a, b int) int {
    return a + b
}

3. ビルドスペックを書く

ビルドスペックは、ビルドコマンドと関連する設定が記述されたファイルになる。YAMLで記述され、AWS CodeBuildでのビルドに利用される。

以下のようなbuildspec.ymlというファイルを作成し、プロジェクト直下に保存する。

buildspec.yml
version: 0.1

environment_variables:
  plaintext:
    MY_ENV: "codebuild"

phases:
  install:
    commands:
      - echo Nothing to do in the install phase...
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
      - echo Build started on `date`
      - go build codebuild.go
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  type: zip
  files:
    - codebuild

それぞれの項目は以下の通りである。詳しくはこちら

項目 必須? 説明
version 必須 ビルドスペックのバージョン。現在最も新しいのが0.1
environment_variables 任意 環境変数
phases 必須 install, pre_build, build, post_buildの4つの段階それぞれで実行するコマンドを列挙する。それぞれの段階の記述は任意となるため、4段階すべてを記入する必要はない。
artifacts 必須 CodeBuildがビルドのOutputを出力するための情報。typeは現在zipのみが対応。filesにはビルド環境でのビルドの生成物の場所を指定する。

ビルドスペックはプロジェクトのリポジトリに含める以外にも、ビルドプロジェクトを作成する際に宣言することができる。

4. プロジェクトをgithubにプッシュする

github上でcodebuild-demo-projectというリポジトリを作成し、以下のようにプロジェクトをプッシュする。リポジトリは自分のものを指定する。

git init
git add .
git commit -m "First commit"
git remote add origin git@github.com:Iwark/codebuild-demo-project.git
git push -u origin master

5. ビルドプロジェクトの作成

コンソールまたはAWS CLIを用いてビルドプロジェクトを作成する。今回はコンソールから作成する。

東京リージョンではまだ使えないので、オレゴンを選択する。

  • Project名はcodebuild-demo-projectにする。
  • RepositoryはGithubを選択し、先ほど作成したリポジトリを接続する。
  • RuntimeはGolangを選択する。
  • Versionは1.7.3を選択する。
  • ArtifactsはAmazon S3を選択する。
  • Bucketはcodebuild-demo-project-outputを選択。

あとはそのままの状態でContinueSave and Buildする。

6. ビルドの開始

Start buildをすると、ビルドが開始される。

あとがき

とりあえずビルドまでは簡単にできました。

Go1.7なのにvendoringが効いていないのか、vendor内にあるパッケージがnot foundになるので困ってます。またちゃんと調べてみようと思います。

6
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
6
3