본문 바로가기

개발

AWS EB, ECR과 도커를 활용한 배포

 

 

yml 파일을 수정한다.

name: Build and Push Docker Image
on:
  push:
    branches:
      - main
jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build with Gradle
        run: ./gradlew clean build -x test
      #        run: gradle build -x test

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-2

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1

      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: [도커 리포지토리] 여기 입력
          IMAGE_TAG: latest
        run: |
          docker buildx build --platform=linux/amd64 -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

      - name: Get current time
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYYMMDD_HH-mm-ss
          utcOffset: "+09:00"

      - name: Generate deployment package
        run: |
          mkdir -p deploy/.platform/nginx/conf.d
          cp Dockerrun.aws.json deploy/Dockerrun.aws.json
          cp .platform/nginx/conf.d/proxy.conf deploy/.platform/nginx/conf.d/proxy.conf
          cd deploy && zip -r deploy.zip .

      - name: Beanstalk Deploy
        uses: einaregilsson/beanstalk-deploy@v14
        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          application_name: [Elastic Beanstalk app 이름] 여기 입력
          environment_name: [Elastic Beanstalk Env 이름] 여기 입력
          version_label: earth-docker-${{steps.current-time.outputs.formattedTime}}
          region: ap-northeast-2
          deployment_package: deploy/deploy.zip
          wait_for_environment_recovery: 200

 

.platform / nginx / conf.d 디렉토리에 proxy.conf 파일을 수정한다. 

# nginx의 기본 업로드 제한 1MB
# 제한 풀기 -> 0
client_max_body_size 0;

 

"Dockerfile"을 생성한다. 

FROM openjdk:8-jdk-alpine
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

 

"Dockerrun.aws.json"을 생성한다. 

{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "[도커 리포지토리 endpoint]", 여기 입력
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": 8080
    }
  ]
}

 

AWS IAM으로 가서 역할(role)로 간다. 

elastic beanstalk의 역할에 "AmazonEC2ContainerRegistryReadOnly"을 추가한다. 

build.gradle에 다음 코드를 추가한다. 

jar {
    enabled = false
}

application.properties와 EB의 configuration의 환경변수를 맞게 설정한다. 

 

EbHealthCheck클래스를 추가한다. 

 

@RestController
public class EbHealthCheck {

    // EB 헬스체크를 위한 코드
    @GetMapping( "/")
    public ResponseEntity<?> doHealthCheck() {
        return ResponseEntity.ok("HealthCheck OK");
    }
}

아래와 같은 설정을 해준다. 

 

출처: 10팀의 KKJ 

https://velog.io/@rudwnd33/zero-downtime-deployment

 

GitHub Actions + Docker + AWS ECR + AWS EB를 활용한 무중단 배포

무중단 배포 구축해보기

velog.io