Closed1

AWS CodeBuildでタスク定義を更新する

trysmrtrysmr
buildspec.yml
version: 0.2

env:
  parameter-store:
    RAILS_MASTER_KEY: "/sample-app/rails/master_key"
    DOCKER_HUB_USERNAME: "/sample-app/docker-hub/username"
    DOCKER_HUB_PASSWORD: "/sample-app/docker-hub/password"

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin <AWS_ACCOUNT>.dkr.ecr.ap-northeast-1.amazonaws.com
      - echo Logging in to Docker Hub...
      - echo $DOCKER_HUB_PASSWORD | docker login -u $DOCKER_HUB_USERNAME --password-stdin
      - REPOSITORY_URI=<AWS_ACCOUNT>.dkr.ecr.ap-northeast-1.amazonaws.com/sample-app
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build --build-arg RAILS_MASTER_KEY=${RAILS_MASTER_KEY} -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Registering new task definition...
      - TASK_FAMILY=sample-app
      - TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_FAMILY" --region "$AWS_DEFAULT_REGION")
      - NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$REPOSITORY_URI:$IMAGE_TAG" '.taskDefinition | .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) |  del(.registeredAt)  | del(.registeredBy)')
      - aws ecs register-task-definition --region "$AWS_DEFAULT_REGION" --cli-input-json "$NEW_TASK_DEFINITION"
      - echo Writing image definitions file...
      - printf '[{"name":"sample-app","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
  files: imagedefinitions.json
このスクラップは3ヶ月前にクローズされました