๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Frontend Dev/๊ธฐํƒ€ ์‹ค์Šต

Github Actions, AWS S3๋ฅผ ์‚ฌ์šฉํ•œ CI/CD

๋ฐ˜์‘ํ˜•

 

 ๐Ÿ’ฌ Code States ๋ถ€ํŠธ์บ ํ”„์˜ ๋ง‰๋ฐ”์ง€์— CI/CD๋ฅผ ์œ„ํ•œ Github Action ์‹ค์Šต์ด ์žˆ์—ˆ๋‹ค. ์ด์ „ ์ฑ•ํ„ฐ์—์„œ AWS S3 ๋ฒ„ํ‚ท์„ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ๋ฅผ ๋ฐฐํฌํ–ˆ์—ˆ๊ณ , ์ด๋ฏธ ๋ฐฐํฌ ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋œ S3์— Github Action์„ ํ†ตํ•ด ํŒŒ์ผ์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์ด๋ฒˆ ์‹ค์Šต์˜ ๋ชฉํ‘œ์˜€๋‹ค. ์ฒ˜์Œ์ด๋ผ ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ, ํ•˜๋‹ค๋ณด๋‹ˆ ํฅ๋ฏธ๊ฐ€ ์ƒ๊ฒจ์„œ ๊ณผ์ •์„ ์ •๋ฆฌํ•ด๋ณผ๊นŒ ํ•œ๋‹ค.

 

 ๐Ÿ“Œ GitHub Actions๋Š” Github๊ฐ€ ๊ณต์‹์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋นŒ๋“œ, ํ…Œ์ŠคํŠธ ๋ฐ ๋ฐฐํฌ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ž๋™ํ™”ํ•  ์ˆ˜ ์žˆ๋Š” CI/CD ํ”Œ๋žซํผ์ด๋‹ค.

 

๐Ÿš€ Github Actions๋ฅผ ํ†ตํ•œ ํด๋ผ์ด์–ธํŠธ ๋ฐฐํฌ Flow

1. Source Github ๋ธŒ๋žœ์น˜์— ์ฝ”๋“œ๊ฐ€ push (push ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ๋„ ๊ฐ€๋Šฅ) ๋˜๋ฉด

2. Build Github Acitons์˜ YAML ํŒŒ์ผ์— ์ ํžŒ ๋ช…๋ น์–ด๋ฅผ ํ† ๋Œ€๋กœ Webpack์„ ์ด์šฉํ•ด ๋นŒ๋“œ๋ฅผ ํ•˜๊ณ 

3. Deploy Github Acitons์˜ YAML ํŒŒ์ผ์— ์ ํžŒ ๋ช…๋ น์–ด๋ฅผ ํ† ๋Œ€๋กœ s3๋กœ ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฅผ ์—…๋กœ๋“œํ•œ๋‹ค.

 


๐Ÿ”– ์ง„ํ–‰ ๊ณผ์ • ์š”์•ฝ

1. ๋ฐฐํฌํ•  ํด๋ผ์ด์–ธํŠธ ํŒŒ์ผ ์ค€๋น„์™€ ๋ฐฐํฌํŒŒ์ผ์ด ์˜ฌ๋ผ๊ฐˆ git repository ์ƒ์„ฑ

2. ๐Ÿ”’ AWS_ACCESS_KEY, AWS_SECRET_KEY ์ค€๋น„

3. yml ํŒŒ์ผ ์ž‘์„ฑ

4. ๋ฐฐํฌ ํ™•์ธ

 


 

1. ๋ฐฐํฌํ•  ํด๋ผ์ด์–ธํŠธ ํŒŒ์ผ ์ค€๋น„์™€ ๋ฐฐํฌํŒŒ์ผ์ด ์˜ฌ๋ผ๊ฐˆ git repository ์ƒ์„ฑ

• repository๋Š” public์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ Github Action์„ ๋ฌด๋ฃŒ๋กœ ์ด์šฉ ๊ฐ€๋Šฅ

• ์ž‘์—…ํ•œ ํด๋ผ์ด์–ธํŠธ ํŒŒ์ผ์„ git repository์— ์—ฐ๊ฒฐ

git remote add origin [repository]

 

2. ๐Ÿ”’ AWS_ACCESS_KEY, AWS_SECRET_KEY ์ค€๋น„

• YAML ํŒŒ์ผ์˜ ACCESS_KEY๋Š” Secretํ•˜๊ฒŒ ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Github Setting์˜ Security์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค. (ํ•˜๋“œ์ฝ”๋”ฉ ์ ˆ๋Œ€ X)

AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Secret key ๋“ฑ๋ก

 

3. ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป yml ํŒŒ์ผ ์ž‘์„ฑ

๐Ÿ“ ํŒŒ์ผ์œ„์น˜: ./.github/workflows/client.yml

# client.yml

name: client # workflow ์ด๋ฆ„
on:
  push:
    branches:
      - reference # reference ๋ธŒ๋žœ์น˜์— push๋ฅผ ํ•˜๋ฉด, ์•„๋ž˜ jobs๋ฅผ ์‹คํ–‰
jobs:
  build: # job์˜ ์ด๋ฆ„
    runs-on: ubuntu-20.04 # ์‹คํ–‰ ํ™˜๊ฒฝ
    steps: # jobs์˜ ์‹คํ–‰ ์ˆœ์„œ

        # ์ฐธ๊ณ 
        # actions๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” uses ์†์„ฑ ์‚ฌ์šฉ
        # command๋‚˜ script ์‹คํ–‰์‹œ run ์†์„ฑ ์‚ฌ์šฉ				

      - name: Checkout source code.
        uses: actions/checkout@v2 # github์˜ ์†Œ์Šค์ฝ”๋“œ ํ™•์ธ

      - name: Install dependencies
        run: npm install # dependencies ์„ค์น˜
        working-directory: ./my-agora-states-client-react

      - name: Build
        run: npm run build # ๋ฐฐํฌ์šฉ ๋ฆฌ์•กํŠธ ํŒŒ์ผ ์ƒ์„ฑ
        working-directory: ./my-agora-states-client-react

      - name: SHOW AWS CLI VERSION # AWS ๋ฒ„์ „ํ™•์ธ(์ƒ๋žต๊ฐ€๋Šฅ)
        run: | 
          aws --version 

      - name: Sync Bucket # AWS S3 ๋ฒ„ํ‚ท ์—ฐ๊ฒฐ
        env:
          # ๋‘๋ฒˆ์งธ ๊ณผ์ •์—์„œ secret key๋กœ ๋“ฑ๋กํ•œ ๋ถ€๋ถ„ (ํ•˜๋“œ์ฝ”๋”ฉ X)
          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 \\ 
            --region ap-northeast-2 \\ 
            build s3://fe-50-sw2377-s3 \\
            --delete
           # AWS sync : ๋ฒ„ํ‚ท๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ฝ˜ํ…์ธ  ๋™๊ธฐํ™” 
           # --region : ์„œ์šธ 
           # build s3://[๋ฒ„ํ‚ท ์ด๋ฆ„]
           # --delete : ์›๋ณธ์— ์—†๋Š” ํŒŒ์ผ์ด๋‚˜ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์—์„œ ์ œ๊ฑฐ
        working-directory: ./my-agora-states-client-react

 

4. ๋ฐฐํฌ ํ™•์ธ

 yml ํŒŒ์ผ ์ž‘์„ฑ ํ›„ push๋ฅผ ํ•˜๋ฉด S3 ๋ฐฐํฌ๊นŒ์ง€ ์ž๋™ํ™”๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

 

โœจ Github Actions์—์„œ๋Š” yml ํŒŒ์ผ์ด ๋™์ž‘ํ•œ๋‹ค.
โœจ S3 ๋ฒ„ํ‚ท์— ์ž๋™์œผ๋กœ ๋นŒ๋“œ๋œ ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋œ๋‹ค.
โœจ ๋ฐฐํฌ๋œ ์›น์‚ฌ์ดํŠธ์—์„œ๋Š” ์ƒˆ๋กœ์šด ํด๋ผ์ด์–ธํŠธ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•