본문 바로가기
Study/Devops

[Continuous Deploy] Github Action with ECR

by 리노 Linho 2022. 9. 18.

배경

  • Github Repository에서 Docker를 빌드하고 AWS ECR에 배포

아키텍처

Github Actions WorkFlow 코드

# This is a basic workflow to help you get started with Actions

name: CD

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    name: build-image
    runs-on: ubuntu-latest
    

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Checkout
      uses: actions/checkout@v2.4.2
      
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.SECRET_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
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} #login-ecr id를 찾음
        ECR_REPOSITORY: seear_cicd #레포지토리 명
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . #docker 빌드
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG #docker를 푸시