Skip to content

[#6] NCP 배포 환경 구축 및 CI/CD 파이프라인 구성 #15

[#6] NCP 배포 환경 구축 및 CI/CD 파이프라인 구성

[#6] NCP 배포 환경 구축 및 CI/CD 파이프라인 구성 #15

Workflow file for this run

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:
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: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Test with Gradle
run: ./gradlew clean test
env:
GRADLE_OPTS: "-Dorg.gradle.daemon=false -Dorg.gradle.parallel=true"
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'
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