docker caching, push image to ecr in github action

x·2024년 1월 22일
0

deploy

목록 보기
2/2

docker multistage로 이미지 캐싱, 재사용

FROM python:3.9-slim AS base-builder

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
RUN apt-get update 
RUN apt-get upgrade -y
RUN apt-get install -y gcc 
COPY . .

FROM base-builder AS poetry-builder

RUN pip install --upgrade pip
RUN pip install poetry
RUN poetry install
RUN poetry run python manage.py collectstatic --settings=project.settings.local

FROM poetry-builder AS run-builder

CMD ["sh", "-c", ""]

Multi stage 쓰기 전
이미지 빌드 13~ 16초

Docker compose 사용 시 빌드 시간 12 ~ 14.5초

Multistage 사용
0.8초

jobs:
  build:
    environment: development
    name: Build
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      
      - 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: ${{ secrets.AWS_REGION }}

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

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        uses: docker/build-push-action@v5
        env:
          ECR_REGISTRY: xxx.dkr.ecr.ap-northeast-2.amazonaws.com
          ECR_REPOSITORY: xxx
          IMAGE_TAG: latest
        with:
          file: dockerfile-local
          context: .
          push: true
          tags: ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
      
      - name: Image Path
        id: image-path
        run: |
          echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: django-task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: task-definition.json
          container-name: django
          image: ${{ steps.image-path.outputs.image }}

      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.django-task-def.outputs.task-definition }}
          service: service
          cluster: cluster
          wait-for-service-stability: true

GitHub runner에서 배포 시

빌드는 1분 25초 소요됨

재 빌드 시
1분35초

아마 runner는 workflow 돌 때마다 새로 할당돼서 캐시가 없는듯

docker/build-push-action 사용.

캐싱 적용 후
처음 빌드 1분 37초 걸림

이후 1분 10초 걸림

0개의 댓글