본문 바로가기

verdantjuly/Today I Learned

TIL 20230925

728x90

이번 주 목표

이력서 pass

오늘 목표

이력서 외부 링크 작성

오늘 한 것

이력서 외부 링크 작성 

- CI/CD

1. 이력서 보완

2. CI/CD

CI/CD = 테스트 배포 자동화 

 

CI = Continuous Integration = 지속적 통합

애플리케이션 코드의 새로운 변경 사항이 빌드 및 테스트를 거쳐 공유 리포지토리에 병합

 

CD = Continuous Delivery = 지속적 제공

CI의 과정을 수행이후 자동으로 배포

 

1. Github Secrests 등록

2. .github > workflows > deploy.yml

아래의 deploy.yml 파일은 https://vanillacreamdonut.tistory.com/323를 토대로 작성

name: deploy

on:
  push:
    branches: [master]

jobs:
  build-and-deploy:
    runs-on: ubuntu-22.04

    strategy:
      matrix:
        node-version: [18.17.0]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
      - name: ✅ 체크아웃 source code.
        uses: actions/checkout@v3

      - name: ⚙️ ${{ matrix.node-version }} 버전의 노드로 세팅합니다.
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: ⚙️ 환경변수를 설정합니다.
        working-directory: ./
        env:
          HOST: ${{secrets.HOST}}
          GATE_PORT: ${{secrets.GATE_PORT}}
          DIS_PORT: ${{secrets.DIS_PORT}}
          USERS_PORT: ${{secrets.USERS_PORT}}
          POSTS_PORT: ${{secrets.POSTS_PORT}}
          THREADS_PORT: ${{secrets.THREADS_PORT}}
          ADMIN_PORT: ${{ secrets.ADMIN_PORT }}
          USERS_MYSQL_USER_NAME: ${{secrets.USERS_MYSQL_USER_NAME}}
          USERS_MYSQL_PASSWORD: ${{secrets.USERS_MYSQL_PASSWORD}}
          USERS_MYSQL_HOST: ${{secrets.USERS_MYSQL_HOST}}
          ADMIN_MYSQL_USER_NAME: ${{secrets.ADMIN_MYSQL_USER_NAME}}
          ADMIN_MYSQL_PASSWORD: ${{secrets.ADMIN_MYSQL_PASSWORD}}
          ADMIN_MYSQL_HOST: ${{secrets.ADMIN_MYSQL_HOST}}
          THREADS_MYSQL_USER_NAME: ${{secrets.THREADS_MYSQL_USER_NAME}}
          THREADS_MYSQL_PASSWORD: ${{secrets.THREADS_MYSQL_PASSWORD}}
          THREADS_MYSQL_HOST: ${{secrets.THREADS_MYSQL_HOST}}
          POSTS_MYSQL_USER_NAME: ${{secrets.POSTS_MYSQL_USER_NAME}}
          POSTS_MYSQL_PASSWORD: ${{secrets.POSTS_MYSQL_PASSWORD}}
          POSTS_MYSQL_HOST: ${{secrets.POSTS_MYSQL_HOST}}
          JWT_SECRET_KEY: ${{secrets.JWT_SECRET_KEY}}
          JWT_SECRET_KEY_REFRESH: ${{secrets.JWT_SECRET_KEY_REFRESH}}
          JWT_EXPIRE_TIME: ${{secrets.JWT_EXPIRE_TIME}}
          JWT_EXPIRE_TIME_REFRESH: ${{secrets.JWT_EXPIRE_TIME_REFRESH}}
          REDIS_HOST: ${{secrets.REDIS_HOST}}
          REDIS_PORT: ${{secrets.REDIS_PORT}}
          REDIS_USERNAME: ${{secrets.REDIS_USERNAME}}
          REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
          NODEMAILER_EMAIL: ${{secrets.NODEMAILER_EMAIL}}
          NODEMAILER_PASSWORD: ${{secrets.NODEMAILER_PASSWORD}}
          DIRECTORY: ${{secrets.DIRECTORY}}
          client_id: ${{secrets.client_id}}
          client_secret: ${{secrets.client_secret}}
          redirect_uri: ${{secrets.redirect_uri}}
          kakaoJSkey: ${{secrets.kakaoJSkey}}
          S3_AWS_ACCESS_KEY_ID: ${{secrets.S3_AWS_ACCESS_KEY_ID}}
          S3_AWS_SECRET_ACCESS_KEY: ${{secrets.S3_AWS_SECRET_ACCESS_KEY}}
          S3_AWS_REGION: ${{secrets.S3_AWS_REGION}}
          S3_AWS_BUCKET_NAME: ${{secrets.S3_AWS_BUCKET_NAME}}
          MONGO_DB: ${{secrets.MONGO_DB}}
        run: |
          pwd
          touch .env
          echo "HOST=$HOST" >> .env
          echo "GATE_PORT=$GATE_PORT" >> .env
          echo "DIS_PORT=$DIS_PORT" >> .env
          echo "USERS_PORT=$USERS_PORT" >> .env
          echo "POSTS_PORT=$POSTS_PORT" >> .env
          echo "ADMIN_PORT=$ADMIN_PORT" >> .env
          echo "THREADS_PORT=$THREADS_PORT" >> .env
          echo "USERS_MYSQL_USER_NAME=$USERS_MYSQL_USER_NAME" >> .env
          echo "USERS_MYSQL_PASSWORD=$USERS_MYSQL_PASSWORD" >> .env
          echo "USERS_MYSQL_HOST=$USERS_MYSQL_HOST" >> .env
          echo "ADMIN_MYSQL_USER_NAME=$ADMIN_MYSQL_USER_NAME" >> .env
          echo "ADMIN_MYSQL_PASSWORD=$ADMIN_MYSQL_PASSWORD" >> .env
          echo "ADMIN_MYSQL_HOST=$ADMIN_MYSQL_HOST" >> .env
          echo "THREADS_MYSQL_USER_NAME=$THREADS_MYSQL_USER_NAME" >> .env
          echo "THREADS_MYSQL_PASSWORD=$THREADS_MYSQL_PASSWORD" >> .env
          echo "THREADS_MYSQL_HOST=$THREADS_MYSQL_HOST" >> .env
          echo "POSTS_MYSQL_USER_NAME=$POSTS_MYSQL_USER_NAME" >> .env
          echo "POSTS_MYSQL_PASSWORD=$POSTS_MYSQL_PASSWORD" >> .env
          echo "POSTS_MYSQL_HOST=$POSTS_MYSQL_HOST" >> .env
          echo "JWT_SECRET_KEY=$JWT_SECRET_KEY" >> .env
          echo "JWT_SECRET_KEY_REFRESH=$JWT_SECRET_KEY_REFRESH" >> .env
          echo "JWT_EXPIRE_TIME=$JWT_EXPIRE_TIME" >> .env
          echo "JWT_EXPIRE_TIME_REFRESH=$JWT_EXPIRE_TIME_REFRESH" >> .env
          echo "REDIS_HOST=$REDIS_HOST" >> .env
          echo "REDIS_PORT=$REDIS_PORT" >> .env
          echo "REDIS_USERNAME=$REDIS_USERNAME" >> .env
          echo "REDIS_PASSWORD=$REDIS_PASSWORD" >> .env
          echo "NODEMAILER_EMAIL=$NODEMAILER_EMAIL" >> .env
          echo "NODEMAILER_PASSWORD=$NODEMAILER_PASSWORD" >> .env
          echo "DIRECTORY=$DIRECTORY" >> .env
          echo "S3_AWS_ACCESS_KEY_ID=$S3_AWS_ACCESS_KEY_ID" >> .env
          echo "S3_AWS_SECRET_ACCESS_KEY=$S3_AWS_SECRET_ACCESS_KEY" >> .env
          echo "S3_AWS_REGION=$S3_AWS_REGION" >> .env
          echo "S3_AWS_BUCKET_NAME=$S3_AWS_BUCKET_NAME" >> .env
          echo "MONGO_DB=$MONGO_DB" >> .env
          cat .env

          cd ./src/public/js
          touch config.js
          echo "const urlParams = new URL(location.href).searchParams;" >> config.js
          echo "const kCode = urlParams.get('code');" >> config.js
          echo "const config = {" >> config.js
          echo "client_id: '$client_id' ," >> config.js
          echo "client_secret: '$client_secret' , " >> config.js
          echo "grant_type: 'authorization_code' ," >> config.js
          echo "redirect_uri: '$redirect_uri' ," >> config.js
          echo "code: kCode," >> config.js
          echo "};" >> config.js
          echo "const kakaoJSkey = '$kakaoJSkey' ;" >> config.js

          cat config.js

      - name: ✨ 패키지 설치
        working-directory: ./
        run: npm install

      - name: ✨ 빌드 과정을 시작
        working-directory: ./
        run: npm run build

      - name: 📦 빌드한 코드를 압축
        run: zip -r verdantjuly.zip ./dist ./scripts ./appspec.yml ./.env ./package.json

      - name: 🌎 AWS에 접속
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-2

      - name: 🦖 S3에 압축된 서버 코드를 업로드
        run: aws s3 cp --region ap-northeast-2 ./verdantjuly.zip s3://verdantjuly

      - name: 🚀 AWS codeDeploy로 배포를 시작
        run: aws deploy create-deployment
          --ignore-application-stop-failures
          --application-name verdantjuly
          --deployment-config-name CodeDeployDefault.OneAtATime
          --deployment-group-name dev
          --s3-location bucket=verdantjuly,bundleType=zip,key=verdantjuly.zip

 

3. IAM 역할 추가

AWS > IAM > 액세스 관리 > 역할 > 역할 만들기

  • ec2-deploy (EC2)

AWS CodeDeployFullAccess

AmazonS3Full Access

  • code-deploy (CodeDeploy)

AWSCodeDeployRole

 

4. IAM 사용자 추가

AWS > IAM > 액세스 관리 > 사용자 > 사용자 추가

  • deploy-user > Access Key Id, Secret Access Key > .csv 파일 꼭 다운로드 > github secrets에 등록

AWSCodeDeployFullAccess 

AmazonS3FullAccess

 

4. AWS S3 버킷 생성

버킷 정책

{
    "Version": "2012-10-17",
    "Id": "",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::나의 아이엠 번호:user/deploy-user"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::verdantjuly"
        }
    ]
}

5. CodeDeploy 애플리케이션 생성

AWS > CodeDeploy > 애플리케이션 > 애플리케이션 생성

배포그룹 생성 (dev) - 서비스 역할 : code-deploy, 인스턴스 선택 : 배포할 EC2, 로드밸런서 X

 

6. EC2에 codedeploy-agent 설치

아래의 명령어 목록은 https://vanillacreamdonut.tistory.com/323를 토대로 작성

sudo apt update
sudo apt install awscli
sudo aws configure

AWS Access Key ID : 다운 받은 csv 파일 내에 Access Key ID
AWS Secret Access KEy : 다운 받은 csv 파일 내에 Secret Access Key
Default region name : ap-northeast-2
Default output format : json

wget https://aws-codedeploy-ap-northeast-2.s3.amazonaws.com/latest/install
chmod +x ./install

sudo apt-get install ruby
sudo ./install auto
sudo service codedeploy-agent status

# 코드 디플로이 에이전트 자동으로 실행되게 쉘스크립트 작성

sudo vim /etc/init.d/codedeploy-startup.sh

i로 편집

내용 ----------------

#!/bin
sudo service codedeploy-agent restart

---------------------

sudo chmod +x /etc/init.d/codedeploy-startup.sh

7. appspec.yml (최상위 폴더에 위치)

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu/build
    overwrite: yes
    file_exists_behavior: OVERWRITE

hooks:
  AfterInstall:
    - location: scripts/after-deploy.sh
      runas: ubuntu

8. scripts > after-deploy.sh (최상위 폴더에 scripts 디렉토리 만들기)

#!/bin/bash

cd /home/ubuntu/build

sudo npm install
sudo pm2 kill
sudo pm2 start "npm run start:gate_build"
sudo pm2 start "npm run start:dis_build"
sudo pm2 start "npm run start:users_build"
sudo pm2 start "npm run start:posts_build"
sudo pm2 start "npm run start:admin_build"
sudo pm2 start "npm run start:threads_build"
sudo pm2 start "npm run start:chat_build"
sudo pm2 startup
sudo pm2 save

 

9. PR시 build.yml 작동

아래의 build.yml 파일은 https://vanillacreamdonut.tistory.com/323를 토대로 작성

name: build

on:
  pull_request:
    branches: [master]

jobs:
  build-check:
    runs-on: ubuntu-22.04

    strategy:
      matrix:
        node-version: [18.17.0]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
      - name: ✅ 체크아웃 source code.
        uses: actions/checkout@v3

      - name: ⚙️ ${{ matrix.node-version }} 버전의 노드로 세팅합니다.
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: ⚙️ 환경변수를 설정합니다.
        working-directory: ./
        run: |
          pwd
          touch .env
          echo HOST=${{ secrets.HOST }} >> .env
          echo GATE_PORT=${{ secrets.GATE_PORT }} >> .env
          echo DIS_PORT=${{ secrets.DIS_PORT }} >> .env
          echo USERS_PORT=${{ secrets.USERS_PORT }} >> .env
          echo POSTS_PORT=${{ secrets.POSTS_PORT }} >> .env
          echo ADMIN_PORT=${{ secrets.ADMIN_PORT }} >> .env
          echo THREADS_PORT=${{ secrets.THREADS_PORT }} >> .env
          echo USERS_MYSQL_USER_NAME=${{ secrets.USERS_MYSQL_USER_NAME }} >> .env
          echo USERS_MYSQL_PASSWORD=${{ secrets.USERS_MYSQL_PASSWORD }} >> .env
          echo USERS_MYSQL_HOST=${{ secrets.USERS_MYSQL_HOST }} >> .env
          echo ADMIN_MYSQL_USER_NAME=${{ secrets.ADMIN_MYSQL_USER_NAME }} >> .env
          echo ADMIN_MYSQL_PASSWORD=${{ secrets.ADMIN_MYSQL_PASSWORD }} >> .env
          echo ADMIN_MYSQL_HOST=${{ secrets.ADMIN_MYSQL_HOST }} >> .env
          echo THREADS_MYSQL_USER_NAME=${{ secrets.THREADS_MYSQL_USER_NAME }} >> .env
          echo THREADS_MYSQL_PASSWORD=${{ secrets.THREADS_MYSQL_PASSWORD }} >> .env
          echo THREADS_MYSQL_HOST=${{ secrets.THREADS_MYSQL_HOST }} >> .env
          echo POSTS_MYSQL_USER_NAME=${{ secrets.POSTS_MYSQL_USER_NAME }} >> .env
          echo POSTS_MYSQL_PASSWORD=${{ secrets.POSTS_MYSQL_PASSWORD }} >> .env3002
          echo POSTS_MYSQL_HOST=${{ secrets.POSTS_MYSQL_HOST }} >> .env
          echo JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }} >> .env
          echo JWT_SECRET_KEY_REFRESH=${{ secrets.JWT_SECRET_KEY_REFRESH }} >> .env
          echo JWT_EXPIRE_TIME=${{ secrets.JWT_EXPIRE_TIME }} >> .env
          echo JWT_EXPIRE_TIME_REFRESH=${{ secrets.JWT_EXPIRE_TIME_REFRESH }} >> .env
          echo REDIS_HOST=${{ secrets.REDIS_HOST }} >> .env
          echo REDIS_PORT=${{ secrets.REDIS_PORT }} >> .env
          echo REDIS_USERNAME=${{ secrets.REDIS_USERNAME }} >> .env
          echo REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }} >> .env
          echo NODEMAILER_EMAIL=${{ secrets.NODEMAILER_EMAIL }} >> .env
          echo NODEMAILER_PASSWORD=${{ secrets.NODEMAILER_PASSWORD }} >> .env
          echo DIRECTORY=${{ secrets.DIRECTORY }} >> .env
          echo S3_AWS_ACCESS_KEY_ID=${{ secrets.S3_AWS_ACCESS_KEY_ID }} >> .env
          echo S3_AWS_SECRET_ACCESS_KEY=${{ secrets.S3_AWS_SECRET_ACCESS_KEY }} >> .env
          echo S3_AWS_REGION=${{ secrets.S3_AWS_REGION }} >> .env
          echo S3_AWS_BUCKET_NAME=${{ secrets.S3_AWS_BUCKET_NAME }} >> .env
          echo MONGO_DB=${{ secrets.MONGO_DB }} >> .env

          cat .env

      - name: ✨ 패키지 설치
        working-directory: ./
        run: npm i

      - name: ✨ 빌드 과정을 시작
        working-directory: ./
        run: npm run build

 

 


정리 

정리는 좋은 과정이다. 머릿속에 있는 지식들을 정연하게 만든다.

 

 

KPT

Keep

뭐라도 해보려 하는 것

Problem

더 열심히 하지 못하고 있는 것

Try

최선을 다하기 

소감

지치지 말고 최선을 다하자.

 

 

 

 

'verdantjuly > Today I Learned' 카테고리의 다른 글

TIL 20230928  (0) 2023.09.28
TIL 20230926  (0) 2023.09.26
TIL 20230924  (0) 2023.09.24
TIL 20230922  (0) 2023.09.22
TIL 20230920  (0) 2023.09.20