AWS

ECS タスク定義を取得してイメージを更新するだけのbuildspec.yml

 

 

既存設定取得してIMAGE_URIを更新するパターン

 

buildspec.yaml

version: 0.2

env:
  variables:
    AWS_REGION: ap-northeast-1
    ECS_CLUSTER_NAME_PREFIX: sample-backup-to-gcp
    SERVICE_ARN_PREFIX: sample-backup-to-gcp-service

phases:
  install:
    commands:
  pre_build: # ビルド前処理
    commands:
      - echo Clean file...
      - rm -rf .git .gitignore README.md
      - echo Logging in to Amazon ECR...
      - pip install awscli --upgrade
      - sudo apt -y update
      - sudo apt -y install jq
      - aws --version
      - aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin {●AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
      - IMAGE_NAME_GCLOUD_SDK=sample-backup-to-gcp-cloudsdk-${ENVIRONMENT}
      - REPOSITORY_URI_GCLOUD_SDK={●AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME_GCLOUD_SDK}
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
      - REPOSITORY_URI_GCLOUD_SDK_AND_IMAGE_TAG=${REPOSITORY_URI_GCLOUD_SDK}:${IMAGE_TAG}
  build:
    commands:
      - echo ${ENVIRONMENT}
      - echo Build started on `date`
      - echo Building the Docker image...
      - pwd
      - ls -laht
      - docker build -t ${REPOSITORY_URI_GCLOUD_SDK}:latest --build-arg ENVIRONMENT=${ENVIRONMENT} -f ./environments/${ENVIRONMENT}/docker/GCLOUD_SDK/Dockerfile .
      - docker tag ${REPOSITORY_URI_GCLOUD_SDK}:latest ${REPOSITORY_URI_GCLOUD_SDK_AND_IMAGE_TAG}
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      # ECRにpush
      - docker push ${REPOSITORY_URI_GCLOUD_SDK}:$IMAGE_TAG
      - echo Writing image definitions file...
      - echo ${ENVIRONMENT}
      # 現在のタスク定義取得
      - SRC_TASKDEF_ARN=$(aws ecs describe-services --cluster ${ECS_CLUSTER_NAME_PREFIX}-${ENVIRONMENT} --services ${SERVICE_ARN_PREFIX}-${ENVIRONMENT} | jq -r '.services[0].taskDefinition')
      - echo ${SRC_TASKDEF_ARN}
      # 現在のタスク定義をJSONで取得し、イメージを指定して更新
      - aws ecs describe-task-definition --task-definition ${SRC_TASKDEF_ARN} | jq '.taskDefinition | del(.taskDefinitionArn, .status, .requiresAttributes, .compatibilities, .revision)' | jq '(.containerDefinitions[] | select(.name == "'${ECS_CLUSTER_NAME_PREFIX}-${ENVIRONMENT}'")).image = "'${REPOSITORY_URI_GCLOUD_SDK_AND_IMAGE_TAG}'"' > tmp_task_definition.json
      # エラー原因となるプロパティを削除
      - cat tmp_task_definition.json | jq 'del(.registeredAt, .registeredBy)' > task_definition.json
      # タスク定義を更新
      - NEW_TASKDEF=`aws ecs register-task-definition --cli-input-json file://task_definition.json | jq -r '.taskDefinition.taskDefinitionArn'`
      - echo ${NEW_TASKDEF}
      # サービスを更新
      - aws ecs update-service --cluster ${ECS_CLUSTER_NAME_PREFIX}-${ENVIRONMENT} --service ${SERVICE_ARN_PREFIX}-${ENVIRONMENT} --task-definition ${NEW_TASKDEF}

artifacts:
    files:
      - task_definition.json

 

 

 

task_definition.json

{
  "containerDefinitions": [
    {
      "name": "sample-backup-to-gcp-develop",
      "image": "{●AWS_ACCOUNT_ID}.dkr.ecr.ap-northeast-1.amazonaws.com/sample-backup-to-gcp-cloudsdk-develop:d84059a",
      "cpu": 0,
      "portMappings": [
        {
          "containerPort": 443,
          "hostPort": 443,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [],
      "mountPoints": [],
      "volumesFrom": [],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/sample-backup-to-gcp-develop",
          "awslogs-region": "ap-northeast-1",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ],
  "family": "sample-backup-to-gcp-develop",
  "taskRoleArn": "{●ROLE_ARN}",
  "executionRoleArn": "{●ROLE_ARN}",
  "networkMode": "awsvpc",
  "volumes": [],
  "placementConstraints": [],
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "cpu": "2048",
  "memory": "4096"
}

 

 

テンプレートファイルを用意しておいて、文字列置換で生成するパターン

 

task_definition_develop.json

{
  "containerDefinitions": [
    {
      "name": "sample-backup-to-gcp-develop",
      "image": "<IMAGE_URI>",
      "cpu": 0,
      "portMappings": [
        {
          "containerPort": 443,
          "hostPort": 443,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [],
      "mountPoints": [],
      "volumesFrom": [],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/sample-backup-to-gcp-develop",
          "awslogs-region": "ap-northeast-1",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ],
  "family": "sample-backup-to-gcp-develop",
  "taskRoleArn": "{●ROLE_ARN}",
  "executionRoleArn": "{●ROLE_ARN}",
  "networkMode": "awsvpc",
  "volumes": [],
  "placementConstraints": [],
  "requiresCompatibilities": [
    "FARGATE"
  ],
  "cpu": "1024",
  "memory": "2048"
}

 

buildspec.yaml

version: 0.2

env:
  variables:
    AWS_REGION: ap-northeast-1
    ECS_CLUSTER_NAME_PREFIX: sample-backup-to-gcp
    SERVICE_ARN_PREFIX: sample-backup-to-gcp-service

phases:
  install:
    commands:
  pre_build: # ビルド前処理
    commands:
      - echo Clean file...
      - rm -rf .git .gitignore README.md
      - echo Logging in to Amazon ECR...
      - pip install awscli --upgrade
      - sudo apt -y update
      - sudo apt -y install jq
      - aws --version
      - aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin {●AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
      - IMAGE_NAME_GCLOUD_SDK=sample-backup-to-gcp-cloudsdk-${ENVIRONMENT}
      - REPOSITORY_URI_GCLOUD_SDK={●AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME_GCLOUD_SDK}
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
      - REPOSITORY_URI_GCLOUD_SDK_AND_IMAGE_TAG=${REPOSITORY_URI_GCLOUD_SDK}:${IMAGE_TAG}
  build:
    commands:
      - echo ${ENVIRONMENT}
      - echo Build started on `date`
      - echo Building the Docker image...
      - pwd
      - ls -laht
      - docker build -t ${REPOSITORY_URI_GCLOUD_SDK}:latest --build-arg ENVIRONMENT=${ENVIRONMENT} -f ./environments/${ENVIRONMENT}/docker/GCLOUD_SDK/Dockerfile .
      - docker tag ${REPOSITORY_URI_GCLOUD_SDK}:latest ${REPOSITORY_URI_GCLOUD_SDK_AND_IMAGE_TAG}
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      # ECRにpush
      - docker push ${REPOSITORY_URI_GCLOUD_SDK}:$IMAGE_TAG
      - echo Writing image definitions file...
      - echo ${ENVIRONMENT}
      # 現在のタスク定義取得
      - sed -e "s@<IMAGE_URI>@$REPOSITORY_URI_GCLOUD_SDK_AND_IMAGE_TAG@g" task_definition_develop.json > task_definition.json
      # タスク定義を更新
      - NEW_TASKDEF=`aws ecs register-task-definition --cli-input-json file://task_definition.json | jq -r '.taskDefinition.taskDefinitionArn'`
      - echo ${NEW_TASKDEF}
      # サービスを更新
      - aws ecs update-service --cluster ${ECS_CLUSTER_NAME_PREFIX}-${ENVIRONMENT} --service ${SERVICE_ARN_PREFIX}-${ENVIRONMENT} --task-definition ${NEW_TASKDEF}

artifacts:
    files:
      - task_definition.json

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)