Springboot Fargate ECS buildspec

springboot 를 fargate 형태로 서비스하기 위한 스크립트이다. 사전에 springboot root directory에는 Dockerfile이 존재해야 한다. JenkinsFile 로 배포되는 형태 것을 buildspec.yml 으로 변경한 예 이다.


version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
      - REPOSITORY_URI=xxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/REPOSITORY_NAME
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
      - chmod +x ./gradlew
      - echo unit testing ...
      - ./gradlew test
      - echo make jar ...
      - ./gradlew assemble
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $REPOSITORY_URI:latest --build-arg JAR_FILE=build/libs/app-0.0.1-SNAPSHOT.jar .
      - 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 Writing image definitions file...
      - printf '[{"name":"xxx","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
      - cat imagedefinitions.json
cache:
  paths:
    - '/root/.gradle/**/*'

artifacts:
  files: imagedefinitions.json

이렇게 배포하면 ECR 의 버전이 하나씩 증가하면서 자동으로 배포 될 것이다.