[JAWS-UG CLI] CodeBuild: #4 ビルドの実行



前提条件
========


CodeBuildへの権限
-----------------

CodeBuildに対してフル権限があること。


AWS CLIのバージョン
-------------------

以下のバージョンで動作確認済

- AWS CLI 1.11.57

```bash:コマンド:
aws --version
```

結果(例):

      aws-cli/1.11.70 Python/2.7.12 Linux/4.4.11-23.53.amzn1.x86_64 botocore/1.5.33

バージョンが古い場合は最新版に更新しましょう。

```bash:コマンド:
sudo -H pip install -U awscli
```


0. 準備
=======

まず変数の確認をします。

```bash:変数の確認:
cat << ETX

        AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:  (0.2) ${AWS_DEFAULT_REGION}
        CODEB_PROJECT_NAME:  (0.3) ${CODEB_PROJECT_NAME}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE: (0.1) <CodeBuildにフル権限のあるプロファイル>
      AWS_DEFAULT_REGION:  (0.2) ap-northeast-1
      CODEB_PROJECT_NAME:  (0.3) codebuild-demo-java-20170417

変数が入っていない、適切でない場合は、それぞれの手順番号について作業を
行います。


0.1. プロファイルの指定
-----------------------

プロファイルの一覧を確認します。

```bash:コマンド:
cat ~/.aws/credentials \
       | grep '\[' \
       | sed 's/\[//g' | sed 's/\]//g'
```

結果(例):

      iamFull-prjz-mbpr13
      <CodeBuildにフル権限のあるプロファイル>

```bash:変数の設定:
export AWS_DEFAULT_PROFILE='<CodeBuildにフル権限のあるプロファイル>'
```


0.2. リージョンの指定
---------------------

```bash:変数の設定:
export AWS_DEFAULT_REGION='ap-northeast-1'
```


0.3. プロジェクト名の指定
-------------------------

```bash:変数の設定:
CODEB_PROJECT_NAME='codebuild-demo-java-20170417'
```


最終確認
--------------

```bash:変数の確認:
cat << ETX

        AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:  (0.2) ${AWS_DEFAULT_REGION}
        CODEB_PROJECT_NAME:  (0.3) ${CODEB_PROJECT_NAME}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE: (0.1) <CodeBuildにフル権限のあるプロファイル>
      AWS_DEFAULT_REGION:  (0.2) ap-northeast-1
      CODEB_PROJECT_NAME:  (0.3) codebuild-demo-java-20170417


1. 事前作業
===========


2. Buildの実行
==============

```bash:変数の確認:
cat << ETX

        CODEB_PROJECT_NAME: ${CODEB_PROJECT_NAME}

ETX
```

```bash:コマンド:
aws codebuild start-build \
        --project-name ${CODEB_PROJECT_NAME}
```

結果(例):

      {
        "build": {
          "buildComplete": false,
          "initiator": "hoge/hoge-session",
          "artifacts": {
              "location": "arn:aws:s3:::artifact-20170417-XXXXXXXXXXXX/codebuild-demo-java-20170417"
          },
          "projectName": "codebuild-demo-java-20170417",
          "timeoutInMinutes": 60,
          "buildStatus": "IN_PROGRESS",
          "environment": {
              "computeType": "BUILD_GENERAL1_SMALL",
              "image": "aws/codebuild/java:openjdk-8",
              "type": "LINUX_CONTAINER",
              "environmentVariables": []
          },
          "source": {
              "type": "S3",
              "location": "src-20170417-XXXXXXXXXXXX/MessageUtil.zip"
          },
          "currentPhase": "SUBMITTED",
          "startTime": 14xxxxxxxx.000,
          "id": "codebuild-demo-java-20170417:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
          "arn": "arn:aws:codebuild:ap-northeast-1:XXXXXXXXXXXX:build/codebuild-demo-java-20170417:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
        }
      }


3. 事後作業
===========


3.1. ビルドIDの取得
-------------------

プロジェクトに存在するビルドのIDを全て取得します。

```bash:コマンド:
ARRAY_CODEB_BUILD_IDS=$( \
        aws codebuild list-builds-for-project \
          --project-name ${CODEB_PROJECT_NAME} \
          --query 'ids' \
          --output text \
) \
        && echo ${ARRAY_CODEB_BUILD_IDS}
```

結果(例):

      codebuild-demo-java-20170417:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

プロジェクト内のビルドで完了していない(つまり実行中の)もののIDを取得し
ます。

```bash:コマンド:
CODEB_BUILD_ID=$( \
        aws codebuild batch-get-builds \
          --ids ${ARRAY_CODEB_BUILD_IDS} \
          --query 'builds[?currentPhase != `COMPLETED`].id' \
          --output text \
) \
        && echo ${CODEB_BUILD_ID}
```

結果(例):

      codebuild-demo-java-20170417:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx


3.2. ビルドフェイズの確認
-------------------------

```bash:コマンド:
CODEB_BUILD_PHASE=$( \
        aws codebuild batch-get-builds \
          --ids ${CODEB_BUILD_ID} \
          --query "builds[].currentPhase" \
          --output text \
) \
        && echo ${CODEB_BUILD_PHASE}
```

結果(例):

      PROVISIONING


3.3. ログの確認
---------------

```bash:コマンド:
LOGS_GROUP_NAME=$( \
        aws codebuild batch-get-builds \
          --ids ${CODEB_BUILD_ID} \
          --query "builds[].logs.groupName" \
          --output text \
) \
        && echo ${LOGS_GROUP_NAME}
```

結果(例):

      /aws/codebuild/ codebuild-demo-java-20170417

```bash:コマンド:
LOGS_STREAM_NAME=$( \
        aws codebuild batch-get-builds \
          --ids ${CODEB_BUILD_ID} \
          --query "builds[].logs.streamName" \
          --output text \
) \
        && echo ${LOGS_STREAM_NAME}
```

結果(例):

      xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

```bash:コマンド:
aws logs get-log-events \
        --log-group-name ${LOGS_GROUP_NAME} \
        --log-stream-name ${LOGS_STREAM_NAME}
```


3.4. ビルドフェイズの確認
-------------------------

```bash:コマンド:
CODEB_BUILD_PHASE=$( \
        aws codebuild batch-get-builds \
          --ids ${CODEB_BUILD_ID} \
          --query "builds[].currentPhase" \
          --output text \
) \
        && echo ${CODEB_BUILD_PHASE}
```

結果(例):

      COMPLETED


3.5. ビルドステータスの確認
---------------------------

```bash:コマンド:
CODEB_BUILD_STATUS=$( \
        aws codebuild batch-get-builds \
          --ids ${CODEB_BUILD_ID} \
          --query "builds[].buildStatus" \
          --output text \
) \
        && echo ${CODEB_BUILD_STATUS}
```

結果(例):

      SUCCEEDED


3.6. View Summarized Build Information
--------------------------------------

```bash:コマンド:
aws codebuild batch-get-builds \
        --ids ${CODEB_BUILD_ID}
```

結果(例):

      {
        "buildsNotFound": [],
        "builds": [
          {
              "buildComplete": true,
              "phases": [
                  {
                      "phaseStatus": "SUCCEEDED",
                      "endTime": 14xxxxxxxx.543,
                      "phaseType": "SUBMITTED",
                      "durationInSeconds": 0,
                      "startTime": 14xxxxxxxx.977
                  },
                  {
                      "contexts": [],
                      "phaseType": "PROVISIONING",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 15,
                      "startTime": 14xxxxxxxx.543,
                      "endTime": 14xxxxxxxx.654
                  },
                  {
                      "contexts": [],
                      "phaseType": "DOWNLOAD_SOURCE",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 4,
                      "startTime": 14xxxxxxxx.654,
                      "endTime": 14xxxxxxxx.34
                  },
                  {
                      "contexts": [],
                      "phaseType": "INSTALL",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 0,
                      "startTime": 14xxxxxxxx.34,
                      "endTime": 14xxxxxxxx.454
                  },
                  {
                      "contexts": [],
                      "phaseType": "PRE_BUILD",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 0,
                      "startTime": 14xxxxxxxx.454,
                      "endTime": 14xxxxxxxx.559
                  },
                  {
                      "contexts": [],
                      "phaseType": "BUILD",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 59,
                      "startTime": 14xxxxxxxx.559,
                      "endTime": 14xxxxxxxx.448
                  },
                  {
                      "contexts": [],
                      "phaseType": "POST_BUILD",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 0,
                      "startTime": 14xxxxxxxx.448,
                      "endTime": 14xxxxxxxx.541
                  },
                  {
                      "contexts": [],
                      "phaseType": "UPLOAD_ARTIFACTS",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 0,
                      "startTime": 14xxxxxxxx.541,
                      "endTime": 14xxxxxxxx.329
                  },
                  {
                      "contexts": [],
                      "phaseType": "FINALIZING",
                      "phaseStatus": "SUCCEEDED",
                      "durationInSeconds": 5,
                      "startTime": 14xxxxxxxx.329,
                      "endTime": 14xxxxxxxx.808
                  },
                  {
                      "phaseType": "COMPLETED",
                      "startTime": 14xxxxxxxx.808
                  }
              ],
              "logs": {
                  "groupName": "/aws/codebuild/codebuild-demo-java-20170417",
                  "deepLink": "https://console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#logEvent:group=/aws/codebuild/codebuild-demo-java-20170417;stream=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                  "streamName": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
              },
              "artifacts": {
                  "location": "arn:aws:s3:::artifact-20170417-XXXXXXXXXXXX/codebuild-demo-java-20170417"
              },
              "projectName": "codebuild-demo-java-20170417",
              "timeoutInMinutes": 60,
              "initiator": "hoge/hoge-session",
              "buildStatus": "SUCCEEDED",
              "environment": {
                  "computeType": "BUILD_GENERAL1_SMALL",
                  "image": "aws/codebuild/java:openjdk-8",
                  "type": "LINUX_CONTAINER",
                  "environmentVariables": []
              },
              "source": {
                  "type": "S3",
                  "location": " src-20170417-XXXXXXXXXXXX /MessageUtil.zip"
              },
              "currentPhase": "COMPLETED",
              "startTime": 14xxxxxxxx.977,
              "endTime": 14xxxxxxxx.808,
              "id": "codebuild-demo-java-20170417:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
              "arn": "arn:aws:codebuild:ap-northeast-1:XXXXXXXXXXXX:build/codebuild-demo-java-20170417:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
          }
        ]
      }


3.7. Get the Build Output Artifact
----------------------------------

```bash:コマンド:
aws s3 ls s3://${S3_BUCKET_ARTIFACTS} --recursive
```

結果(例):

      2017-04-1700:01:14       2065 codebuild-demo-java-20170417/target/target/messageUtil-1.0.jar


完了
====