Github Actions로 AWS S3에 자동화 배포하기

젬마·2022년 12월 8일
0

codestates

목록 보기
16/18

Github Actions란?

GitHub Actions는 Github가 공식적으로 제공하는 빌드, 테스트 및 배포 파이프라인을 자동화할 수 있는 CI/CD 플랫폼이다.

레포지토리에서 Pull Request 나 push 같은 이벤트를 트리거로 GitHub 작업 Workflow를 구성할 수 있다. Workflow는 하나 이상의 작업이 실행되는 자동화 프로세스로, 각 작업은 자체 가상 머신 또는 컨테이너 내부에서 실행된다.

Workflow는 YAML 파일 형태로 작성되며, 테스트, 배포 등 기능에 따라 여러개의 Workflow도 만들 수 있다.

배포 실습

Workflow 작성

//.github/workflows/client.yml
name: client
on:
  push:
    branches:
      - reference
jobs:
  build:
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout source code.
        uses: actions/checkout@v2
      - name: Install dependencies
        run: npm install
        working-directory: ./my-agora-states-client
      - name: Build
        run: npm run build
        working-directory: ./my-agora-states-client
      - name: SHOW AWS CLI VERSION
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_EC2_METADATA_DISABLED: true
        run: |
          aws --version
      - name: Sync Bucket
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_EC2_METADATA_DISABLED: true
        run: |
          aws s3 sync \
          --recursive \
          --region ap-northeast-2 \
          build s3://fe-38-zemma0618-s3

        working-directory: ./my-agora-states-client
  • AWS CLI Key는 절대... 절대... 절대절대 외부 노출되면 안됩니다. 하드코딩? 안됩니다. Github Actions Secrets에 저장해야 함.
  • aws s3 명령어의 경우 여기를 참고. Sync Bucket - run 부분 작성이 많이 헷갈렸음.

Actions Secrets에 Key 저장


  1. 사용할 repository의 Settings 탭 - Secrets - Actions - New repository Secrets
  2. Name과 Secret에 각각 알맞은 값을 넣어주기

배포하기

  1. Source: Github reference 브랜치에 코드가 커밋되면
  2. Build: github acitons의 YAML 파일에 적힌 명령어를 토대로 Webpack을 이용해 빌드를 하고
  3. Deploy: github acitons의 YAML 파일에 적힌 명령어를 토대로 s3로 빌드 결과가 업로드됨.

그 결과 s3 버킷에서 다음과 같이 잘 업로드된 화면을 볼 수 있었다.

profile
취준생은 프론트엔드의 꿈을 꾸는가

0개의 댓글