Skip to content

Commit

Permalink
Fix: gradle 참조 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
deveunhwa committed Nov 5, 2024
1 parent e277b0e commit 75bd6ea
Showing 1 changed file with 69 additions and 59 deletions.
128 changes: 69 additions & 59 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,42 @@ jobs:
contents: read

steps:
- uses: actions/checkout@v4

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

# application-prod.properties 암호화
- name: Add secrets into properties
run: |
echo "${{ secrets.APPLICATION_PROD }}" | base64 --decode > ./src/main/resources/application-prod.properties
# Gradle 설정
- name: Setup Gradle
uses: gradle/setup-gradle@v2

# Gradlew 실행 권한 부여
- name: Grant execute permission for Gradlew
run: chmod +x gradlew

# 프로젝트 빌드
- name: Build with Gradle Wrapper
run: ./gradlew build

# Docker Hub 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# Docker 이미지 빌드 및 푸시
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

# application-prod.properties 암호화
- name: add secrets into properties
run: |
echo "${{ secrets.APPLICATION_PROD }}" | base64 --decode >> ./src/main/resources/application-prod.properties
# 환경 설정
- name: Setup Gradle
uses: gradle/gradle-build-action@v2

# 실행 권한 부여
- name: Grant execute permission for Gradlew
run: chmod +x gradlew

# 프로젝트 빌드 (clean bootJar로 빌드만 수행)
- name: Build with Gradle Wrapper
run: ./gradlew build

# Docker Hub 로그인
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# Docker 이미지 빌드 및 푸시
- name: Build and push Docker image
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
deploy:
if: github.repository == 'prgrms-be-devcourse/NBB1_2_3_Team10' && github.ref == 'refs/heads/release'
Expand All @@ -62,30 +60,42 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

# EC2 서버 배포
- name: Deploy to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
# Docker 로그인 및 최신 이미지 가져오기
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
docker pull ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
# blue/green 컨테이너 전환
CURRENT_CONTAINER=$(docker ps --filter "name=backend-blue" -q)
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
docker pull ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
docker stop my-container || true
docker rm my-container || true
docker run -d --name my-container -p 80:80 ${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
# 현재 활성화된 blue/green 컨테이너 확인
CURRENT_CONTAINER=$(docker ps --filter "name=backend-blue" -q)
if [ -n "$CURRENT_CONTAINER" ]; then
# blue가 활성화된 경우 green으로 전환
TARGET_CONTAINER="backend-green"
[ -z "$CURRENT_CONTAINER" ] && TARGET_CONTAINER="backend-blue"
# 새 컨테이너 실행
docker stop $TARGET_CONTAINER || true
docker rm $TARGET_CONTAINER || true
docker run -d --name $TARGET_CONTAINER -p 8080:80 \
-e NODE_ENV=production \
${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
# Nginx 리로드하여 트래픽 전환
sudo systemctl reload nginx
else
# green이 활성화된 경우 blue로 전환
TARGET_CONTAINER="backend-blue"
fi
# 새 컨테이너 시작
docker stop $TARGET_CONTAINER || true
docker rm $TARGET_CONTAINER || true
docker run -d --name $TARGET_CONTAINER -p 8080:80 \
-e NODE_ENV=production \
${{ secrets.DOCKER_USERNAME }}/bitta-kotlin:latest
# Nginx 리로드하여 트래픽 전환
sudo systemctl reload nginx

0 comments on commit 75bd6ea

Please sign in to comment.