Merge pull request #191 from Codeit-Part4-SFJs/hj/readme #13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD AWS | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
build-nextjs: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: [18.x, 20.x] | |
steps: | |
- name: git clone | |
uses: actions/checkout@v4 | |
- name: Get Env | |
run: | | |
touch ./.env.production | |
echo "${{secrets.PRODUCTION_ENV}}" > ./.env.production | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.node-version }} | |
cache: 'yarn' | |
- name: Cache node moudles | |
uses: actions/cache@v3 | |
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) | |
with: | |
path: '**/node_modules' | |
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} | |
- name: Install Dependencies | |
run: yarn | |
- name: Disable Next.js Telemetry | |
run: yarn next telemetry disable | |
- name: Build project | |
run: yarn build | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: 코드 체크아웃 | |
id: checkout-code | |
uses: actions/checkout@v4 | |
- name: Get Env | |
run: | | |
touch ./.env.production | |
echo "${{secrets.PRODUCTION_ENV}}" > ./.env.production | |
- name: Use Node.js 20.x | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20.x | |
- name: get-npm-version | |
id: package-version | |
uses: martinbeentjes/npm-get-version-action@v1.3.1 | |
- name: Cache node moudles | |
uses: actions/cache@v3 | |
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`) | |
with: | |
path: '**/node_modules' | |
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }} | |
- name: Install Dependencies | |
run: yarn | |
- name: Disable Next.js Telemetry | |
run: yarn next telemetry disable | |
- name: Build project | |
run: yarn build | |
- name: AWS IAM 사용자 설정 | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Sync static files with S3 | |
env: | |
MAX_AGE: ${{ vars.CACHE_CONTROL_MAX_AGE }} | |
run: aws s3 sync .next/static s3://${{ vars.S3_BUCKET }}/${{ steps.package-version.outputs.current-version }}/_next/static --cache-control max-age=$MAX_AGE,public,immutable | |
- name: ECR에 로그인 | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: 기존 ECR에 업로드된 lastest 태그 이미지 삭제 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
aws ecr batch-delete-image --profile ${{ secrets.IAM_PROFILE_USERNAME }} --repository-name wdyta-ecr --image-ids imageTag=latest | |
- name: 도커 이미지 빌드 및 업로드 | |
run: | | |
docker build -t wdyta-ecr . | |
docker tag wdyta-ecr:latest ${{ secrets.ECR_URI }}:latest | |
docker push ${{ secrets.ECR_URI }}:latest | |
- name: EC2에서 ECR에 로그인하기 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
aws ecr get-login-password --profile ${{ secrets.IAM_PROFILE_USERNAME }} --region ${{ secrets.AWS_REGION }} | sudo docker login --username AWS --password-stdin ${{ secrets.ECR_URI }} | |
- name: 기존 EC2에서 실행중인 컨테이너 종료 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker ps -q | xargs -r docker stop | |
- name: 기존 EC2에 존재하는 모든 컨테이너 삭제 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker ps -asq | xargs -r docker rm | |
- name: 기존 EC2에 저장되어있는 이미지 삭제 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker images -q | xargs -r docker rmi | |
- name: EC2에서 도커에서 사용하지 않는 자원 삭제처리 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker system prune -af | |
- name: EC2에서 도커 이미지를 가져오기 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker pull ${{ secrets.ECR_URI }} | |
- name: ECR에서 불러온 이미지를 도커에서 실행 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
sudo docker stop wdyta-ecr || true | |
sudo docker rm wdyta-ecr || true | |
sudo docker run -d -p 80:80 ${{ secrets.ECR_URI }} |