๐Ÿ”ฅ TIL - Day 22

Kim Dae Hyunยท2021๋…„ 10์›” 7์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
25/93

๐Ÿ“Œ Github Action (S3, CloudFront)

๋ฐฐํฌ ์ค€๋น„๋ฅผ ํ”„๋กœ์ ํŠธ ๋ง‰๋ฐ”์ง€์— ์ค€๋น„ํ•˜๋ฉด ๊ทธ๋•Œ ๋งŽ์ด ๋ˆˆ๋ฌผ์„ ํ˜๋ฆด ๊ฒƒ ๊ฐ™์•„์„œ ๋ฏธ๋ฆฌ ์ค€๋น„๋ฅผ ์‹œ์ž‘ํ–ˆ๋‹ค.

๋ˆˆ๋ฌผ์„ ๋จผ์ € ํ˜๋ ธ๋‹ค...

๊ฐ€์žฅ ๋จผ์ € ํ”„๋กœ์ ํŠธ์˜ ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ S3์— ์˜ฌ๋ฆฌ๊ณ  ์›น ํ˜ธ์ŠคํŒ… ์„ค์ •์„ ํ•˜๊ณ  CloudFront๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ Github Action ์Šคํฌ๋ฆฝํŠธ์„ ํ†ตํ•ด ์ž๋™ํ™”ํ•œ๋‹ค.

.github/workflows/front.yml ์— ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

[ main ๋ธŒ๋žœ์น˜์— push ๋  ๋•Œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ ]
on:
  push:
    branches:
    - main

์ด์ œ jobs ์•„๋ž˜์— ๋ฐฐํฌ๋ฅผ ์œ„ํ•œ ์ผ๋ จ์˜ step์„ ์„ค์ •ํ•œ๋‹ค.

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      # IAM key ์„ธํŒ… [ repo - settings - secrets - new repository secret ]
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_REGION: 'ap-northeast-2'
    
    
    steps:
      # ๋ฐฐํฌ๋ฅผ ์œ„ํ•œ job์ด ์ˆ˜ํ–‰๋  ๊ฐ€์ƒ๋จธ์‹ ์— ๋Œ€ํ•œ ์„ค์ •
      - name: Checkout source code.
        uses: actions/checkout@master      
      
      # flask์˜ templates์™€ static ํด๋”๋ฅผ S3์— ์—…๋กœ๋“œ ํ•œ๋‹ค.
      - name: upload html css js file to S3
        run: |
          aws s3 sync ./templates s3://todaylaw-s3/templates --acl public-read
          aws s3 sync ./static s3://todaylaw-s3/static --acl public-read       
         
      # ๊ธฐ์กด์— CloudFront์— ์บ์‹ฑ๋œ ์ •์  ๋ฆฌ์†Œ์Šค๋“ค์„ ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
      # ์ƒˆ๋กญ๊ฒŒ S3์— ์—…๋กœ๋“œ๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ์บ์‹ฑํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.
      - name: Invalidate cache CloudFront
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        run: |
          aws cloudfront create-invalidation --distribution-id ${{ secrets.DISTRIBUTION_ID }} --paths "/*"

ํœด ...


๐Ÿ“Œ 2์ฐจ ํ”„๋กœ์ ํŠธ ํˆฌ๋ฐ์ด ๊ตฌํ˜„ ์ด์Šˆ

์–ด์ œ์— ์ด์–ด์„œ ๊ธฐ์กด ๊ธฐ๋Šฅ์— ์ธ์ฆ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์กฐ๊ธˆ ๋ณด์™„ํ•˜๋Š” ์ด์Šˆ์ด๋‹ค.
๊ธฐ์กด์—๋Š” ๋™์ผํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ณ„์†ํ•ด์„œ ์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์˜€๋Š”๋ฐ ํ•œ ๋ฒˆ ๋ˆ„๋ฅด๋ฉด ์ข‹์•„์š” ๋‘๋ฒˆ์งธ ๋ˆ„๋ฅด๋ฉด ์ข‹์•„์š”๊ฐ€ ์ทจ์†Œ ๋˜๋„๋ก ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

        if id_receive in like_laws: # ์ข‹์•„์š”๊ฐ€ ์ด๋ฏธ ๋ˆŒ๋ฆฐ ๋ฒ•์•ˆ์ธ ๊ฒฝ์šฐ
            # ์‚ฌ์šฉ์ž์˜ ์ข‹์•„์š” ๋ชฉ๋ก์—์„œ ์ œ๊ฑฐ
            db.users.update(
                {'user_id':user['user_id']},
                {'$pull':{'like_laws':id_receive}}
            )

            # ์ข‹์•„์š”๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๋ถ€๋ถ„
            likes = db.ranking.find_one({'id': id_receive})
            current_like = likes['like']
            new_like = current_like - 1
            current_hate = likes['hate']
            db.ranking.update_one({'id': id_receive}, {'$set': {'like': new_like}})
        else: # ์ข‹์•„์š”๊ฐ€ ์ฒ˜์Œ ๋ˆŒ๋ฆฐ ๋ฒ•์•ˆ์ธ ๊ฒฝ์šฐ
            # ์‚ฌ์šฉ์ž์˜ ์ข‹์•„์š” ๋ชฉ๋ก์— ์ถ”๊ฐ€
            db.users.update(
                {'user_id': user['user_id']},
                {'$push': {'like_laws': id_receive}}
            )

            # ์ข‹์•„์š”๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๋ถ€๋ถ„
            likes = db.ranking.find_one({'id': id_receive})
            current_like = likes['like']
            new_like = current_like + 1
            current_hate = likes['hate']
            db.ranking.update_one({'id': id_receive}, {'$set': {'like': new_like}})

        return jsonify({'id': id_receive, 'like': new_like, 'hate': current_hate})

์‹ซ์–ด์š” ๋˜ํ•œ ์™„์ „ํžˆ ๋˜‘๊ฐ™์€ ๋กœ์ง์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.

์–ด์ œ mongoDB ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๊ณ ์ƒ์„ ํ•˜๋‹ˆ ์˜ค๋Š˜์€ ๋‚˜๋ฆ„ ์ฆ๊ฑฐ์šด ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ–ˆ๋‹ค ๐Ÿ˜ ใ…Ž ๐Ÿ˜†

๋ฐœ์ „ํ–ˆ๋‹ค!!!!!!!

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€