Infra/AWS

[AWS] Git 배포 자동화

민돌v 2021. 9. 28. 22:47
728x90

Git Action

  • GitHub Action은 간단하게는 서버에 소스를 배포하는 서비스 입니다.

  • Git Action은 Github에서 제공하는 배포 서비스에요.
  • GIthub가 MS에 인수되면서 기존의 소스저장소의 기능에서 DevOps플랫폼으로 으로 발전하고 있어요.
  • 비슷한 서비스로는 Gitlab, Bitbucket등이 있어요.
  • 요즘 트랜드는 CI/CD(지속적 통합/지속적 제공) 의 통합입니다. 소스저장소와 배포시스템을 통합하는 것입니다.
  • 아키텍처의 변화로 작아진 어플리케이션들을 부담없이 자주 배포하기 위함이죠.

 

Git Action 사용법

  • GitHub 소스 리파지토리에 .github/workflows/main.yml 파일만 추가하면 됩니다.

https://docs.github.com/en/actions

 

GitHub Actions Documentation - GitHub Docs

Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized wo

docs.github.com

 

 

GitHub Action을 이용한 자동 배포 만들기


1. IAM 권한 추가

IAM > 사용자 > 클릭

CloudFront 권한 추가

 


 

2. GitHub Action으로 레포지토리와 aws(cloudfront) 연동

1. git 폴더안에 "배포 스크립트 추가" - .github/workflows/main.yml

main.yml

name: github-repogitory name
on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_REGION: 'ap-northeast-2'

    steps:
      - name: Checkout source code.
        uses: actions/checkout@master

      - name: Upload binary to S3 bucket
        uses: jakejarvis/s3-sync-action@master
        with:
          args: --acl public-read --exclude '*' --include 'index.html'
        env:
          AWS_S3_BUCKET: ${{ secrets.BUCKET_NAME }}

      - name: Invalidate cache CloudFront
        uses: chetan/invalidate-cloudfront-action@master
        env:
          DISTRIBUTION: ${{ secrets.DISTRIBUTION_ID }}
          PATHS: '/index.html'
        continue-on-error: true

 


 

3. github 레파지토리에서 settings key 설정

AWS 연동 준비 - Settings > Secrets

 

AWS Key 저장

Aws key를 파일에 두지 않고, settings에 저장하여 숨기기위함이다.

Distribution_id 는 cloudfront id이다.

4. 배포 확인

반응형