Skip to content

Commit

Permalink
[COZY-431] feat: prod 서버 구성
Browse files Browse the repository at this point in the history
  • Loading branch information
eple0329 committed Dec 4, 2024
1 parent 180045a commit 4c1a684
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/publish_cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: PRODUCTION CI/CD

on:
push:
branches: [ "main" ]

jobs:
ci:
name: CI
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.PRODUCTION_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.PRODUCTION_AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.PRODUCTION_AWS_REGION }}
ECR_REPOSITORY: ${{ secrets.PRODUCTION_ECR_REPOSITORY }}
ECR_URI: ${{ secrets.PRODUCTION_ECR_URI }}
SUBMODULE_TOKEN: ${{ secrets.SUBMODULE_ACTION_TOKEN }}
IMAGE_TAG: ${{ github.sha }}


steps:
#체크아웃
- name: Checkout code
uses: actions/checkout@v4

# JDK 설치
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

#서브 모듈 접근
- name: Checkout repo
uses: actions/checkout@v3
with:
submodules: true

# 서브 모듈 변경 점 있으면 update
- name: Git Submodule Update
run: |
git submodule update --remote --recursive
# gradlew 권한 변경
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# 빌드(test는 제외)
- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
arguments: clean build -x test

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

## Main Branch Build
- name: Build, Tag (Main Branch)
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
docker build --build-arg SPRING_PROFILE=prod -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
- name: AWS ECR Push
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
cd:
needs: ci
name: CD
runs-on: ubuntu-latest
env:
AWS_ACCESS_KEY_ID: ${{ secrets.PRODUCTION_AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.PRODUCTION_AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.PRODUCTION_AWS_REGION }}
ECR_REPOSITORY: ${{ secrets.PRODUCTION_ECR_REPOSITORY }}
ECR_URI: ${{ secrets.PRODUCTION_ECR_URI }}
SUBMODULE_TOKEN: ${{ secrets.SUBMODULE_ACTION_TOKEN }}
IMAGE_TAG: ${{ github.sha }}

steps:
- name: Deploy to EC2 (Main Branch)
if: github.ref == 'refs/heads/main'
uses: appleboy/ssh-action@master
env:
ENV_FILE: "/home/ubuntu/.env"
COMPOSE: "/home/ubuntu/docker-compose.yml"
with:
host: $EC2_HOST
username: ubuntu
key: $SSH_KEY
script: |
echo "ECR_URI=$ECR_URI" > /home/ubuntu/.env
echo "GITHUB_SHA=$IMAGE_TAG" >> /home/ubuntu/.env
echo "SPRING_PROFILES=dev" >> /home/ubuntu/.env
sudo docker stop $(sudo docker ps -a -q) || true
sudo docker rm $(sudo docker ps -a -q) || true
sudo docker images -q | xargs -r docker rmi || true
sudo docker system prune -af
aws ecr get-login-password --region ap-northeast-2 | sudo docker login --username AWS --password-stdin $ECR_URI && sudo docker pull $ECR_URI:$IMAGE_TAG
sudo -E docker-compose up -d
2 changes: 1 addition & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.cozymate.cozymate_server.global.response.code.status.ErrorStatus;
import com.cozymate.cozymate_server.global.response.exception.GeneralException;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -260,9 +261,13 @@ private TodoType classifyTodoType(List<Long> todoIdList) {
* @param mateIdList 할당자 리스트
*/
private void checkMateIdListIsSameRoomWithMate(Mate mate, List<Long> mateIdList) {
List<Mate> mateList = mateRepository.findByRoomId(mate.getRoom().getId());
if (mateIdList.stream().anyMatch(
mateId -> mateList.stream().noneMatch(m -> m.getMember().getId().equals(mateId)))) {
List<Mate> roomMateList = mateRepository.findByRoomId(mate.getRoom().getId());

// roomMateList의 id만 추출해서 hashset으로 만들어줌
HashSet<Long> roomMateIdSet = roomMateList.stream().map(Mate::getId).collect(
HashSet::new, HashSet::add, HashSet::addAll);

if (!roomMateIdSet.containsAll(mateIdList)) {
throw new GeneralException(ErrorStatus._MATE_NOT_FOUND);
}
}
Expand Down

0 comments on commit 4c1a684

Please sign in to comment.