0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWSCodebuildでビルドする

Posted at

構成


locals {
  TENANT_ID = "xxxxxxxx"
}

resource "aws_s3_bucket" "manual" {
  bucket = "manual"
  acl    = "private"
}

resource "aws_codebuild_project" "manual" {
  name           = "manual"
  description    = ""
  build_timeout  = "60"
  queued_timeout = "480"
  source_version = "master"
  badge_enabled = false

  service_role = aws_iam_role.codebuild.arn

  artifacts {
    encryption_disabled    = false
    location               = aws_s3_bucket.manual_front.bucket
    name                   = "manual"
    namespace_type         = "BUILD_ID"
    override_artifact_name = false
    packaging              = "NONE"
    type                   = "S3"
  }

  cache {
    modes = []
    type  = "NO_CACHE"
  }

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                       = "aws/codebuild/standard:4.0-20.09.14"
    image_pull_credentials_type = "CODEBUILD"
    privileged_mode             = false
    type                        = "LINUX_CONTAINER"

    environment_variable {
      name  = "NODE_HOST"
      type  = "PLAINTEXT"
      value = "vamdemic"
    }
    environment_variable {
      name  = "API_HTTPS"
      type  = "PLAINTEXT"
      value = "true"
    }
  }

  logs_config {
    cloudwatch_logs {
      status = "ENABLED"
    }

    s3_logs {
      encryption_disabled = false
      status              = "DISABLED"
    }
  }

  source {
    git_clone_depth     = 1
    insecure_ssl        = false
    location            = "https://github.com/vamdemic/manual.git"
    report_build_status = false
    type                = "GITHUB"

    git_submodules_config {
      fetch_submodules = false
    }
  }

  tags = {
    Environment = "AutoDeployment"
  }
}

buildspecファイル

  • CodeBuildで指定したブランチのルートディレクトリにファイルを配置しておく
  • または、awscli実行時に上書き指定することで読み込ませることもできる
  • CodeBuildから呼び出されるビルド用コンテナの中で実行したいコマンドを列挙していく
    • install
      • yarn installやnpm installなどのセットアップ系を処理を書くところ
    • build
      • buildコマンドなどを書くところ
    • pre_なんとか
      • buildの前の処理を書くところ
      • sedしたりファイルを配置したりなど
    • artifacts
      • ビルドされたものの管理
      • この例では、カレントディレクトリのdistというディレクトリをまとめてアーティファクトとして保存するという意味
      • アーティファクト用に指定したs3に保存されているので見てみる
buildspec.yaml
version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 10
    commands:
      - echo Installing source NPM dependencies...
      - yarn install
  build:
    commands:
      - NODE_HOST=template yarn build
      - pwd
      - ls
artifacts:
  files:
    - '**/*'
  base-directory: 'dist'

実行

  • ビルドがSUCCESSになるまで待機するシェル
  • --environment-variables-overrideは環境変数を上書きしている
code-build-manual.sh
#!/bin/bash

# Build実行
BUILD_RESULT=$(aws codebuild start-build \
  --project-name manual \
  --environment-variables-override '[
    {
      "name": "API_HOST",
      "value": "'${1}'.vamdemic.jp",
      "type": "PLAINTEXT"
    },
    {
      "name": "ADMIN_ROOT_PATH",
      "value": "https://'${1}'.vamdemic.jp/",
      "type": "PLAINTEXT"
    }
  ]'
)


# IDを取得
EXECUTE_ID=$(echo ${BUILD_RESULT} | jq -r ".build.id")

BUILD_STATUS=$(aws codebuild batch-get-builds \
        --ids ${EXECUTE_ID} \
        | jq -r ".builds[].buildStatus"
)
#echo ${BUILD_STATUS}

while [ "${BUILD_STATUS}" != "SUCCEEDED" ]
do
  sleep 10
        BUILD_STATUS=$(aws codebuild batch-get-builds \
                --ids ${EXECUTE_ID} \
                | jq -r ".builds[].buildStatus"
        )
done

echo "$(echo ${EXECUTE_ID} | awk -F ':' '{print $2}')"
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?