fix: Workflow 수정 #12
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: BlackFriday CI/CD | |
on: | |
push: | |
branches: | |
- 'feature/**' | |
- 'main' | |
paths-ignore: | |
- '**.md' | |
- 'docs/**' | |
- '.gitignore' | |
pull_request: | |
branches: [ main ] | |
paths-ignore: | |
- '**.md' | |
- 'docs/**' | |
- '.gitignore' | |
jobs: | |
test: | |
strategy: | |
matrix: | |
os: [ubuntu-latest, macos-latest, windows-latest] | |
runs-on: ${{ matrix.os }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: 'gradle' | |
- name: Grant execute permission for gradlew | |
if: runner.os != 'Windows' | |
run: chmod +x gradlew | |
- name: Test with Gradle | |
if: runner.os != 'Windows' | |
run: ./gradlew clean test | |
env: | |
GRADLE_OPTS: "-Dorg.gradle.daemon=false" | |
- name: Test with Gradle on Windows | |
if: runner.os == 'Windows' | |
run: .\gradlew.bat clean test | |
env: | |
GRADLE_OPTS: "-Dorg.gradle.daemon=false" | |
build-and-push: | |
needs: test | |
if: github.ref == 'refs/heads/main' && needs.test.result == 'success' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: 'gradle' | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_TOKEN }} | |
- name: Build and Push with Jib | |
run: | | |
./gradlew jib \ | |
-Djib.to.image=docker.io/${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app \ | |
-Djib.to.tags=latest,${{ github.sha }} | |
deploy-blue: | |
needs: build-and-push | |
if: needs.build-and-push.result == 'success' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Copy docker-compose files | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.NCP_BLUE_HOST }} | |
username: deploy-user | |
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }} | |
source: "docker-compose.yml" | |
target: "/app" | |
- name: Deploy to Blue Server | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.NCP_BLUE_HOST }} | |
username: deploy-user | |
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }} | |
script: | | |
cd /app | |
docker compose down | |
docker pull docker.io/${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }} | |
DOCKER_IMAGE=${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }} \ | |
MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }} \ | |
MYSQL_USER=${{ secrets.MYSQL_USER }} \ | |
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} \ | |
docker compose up -d | |
- name: Health Check Blue | |
run: | | |
echo "Waiting for application to initialize..." | |
sleep 60 | |
for i in {1..10}; do | |
if curl -s -f http://${{ secrets.NCP_BLUE_HOST }}:8080/health/liveness; then | |
echo "Health check passed" | |
exit 0 | |
fi | |
echo "Attempt $i failed. Retrying..." | |
sleep 10 | |
done | |
exit 1 | |
deploy-green: | |
needs: [deploy-blue] | |
if: needs.deploy-blue.result == 'success' # Blue 배포 성공해야만 실행 | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Copy docker-compose files | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.NCP_GREEN_HOST }} | |
username: deploy-user | |
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }} | |
source: "docker-compose.yml" | |
target: "/app" | |
- name: Deploy to Green Server | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.NCP_GREEN_HOST }} | |
username: deploy-user | |
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }} | |
script: | | |
cd /app | |
docker compose down | |
docker pull docker.io/${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }} | |
DOCKER_IMAGE=${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }} \ | |
MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }} \ | |
MYSQL_USER=${{ secrets.MYSQL_USER }} \ | |
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} \ | |
docker compose up -d | |
- name: Health Check Green | |
run: | | |
echo "Waiting for application to initialize..." | |
sleep 60 | |
for i in {1..10}; do | |
if curl -s -f http://${{ secrets.NCP_GREEN_HOST }}:8080/health/liveness; then | |
echo "Health check passed" | |
exit 0 | |
fi | |
echo "Attempt $i failed. Retrying..." | |
sleep 10 | |
done | |
exit 1 |