Main 머지 #25
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: GachTaxi-BE prod CI/CD | |
on: | |
push: | |
branches: [ "main"] | |
pull_request: | |
branches: [ "main"] | |
types: [opened, synchronize, reopened] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# 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: Build with Gradle Wrapper | |
run: ./gradlew build -x test | |
# 빌드된 JAR 파일 확인 | |
- name: List JAR files | |
run: ls build/libs | |
# Docker 이미지 빌드 및 푸시 | |
- name: Docker build & push | |
run: | | |
docker login -u ${{ secrets.DEV_DOCKER_USER_NAME }} -p ${{ secrets.DEV_DOCKER_USER_TOKEN }} | |
docker buildx create --use | |
docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile-dev -t ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi:${{ secrets.PROD_DOCKER_TAG }} --push . | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
if: github.event_name == 'push' | |
steps: | |
# SSH를 사용하여 원격 서버에 배포 | |
- name: Deploy to server | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.PROD_SSH_SECRET_HOST }} | |
username: ${{ secrets.PROD_SSH_SECRET_USER }} | |
port: 22 | |
key: ${{ secrets.PROD_SSH_SECRET_PRIVATE_KEY }} | |
script: | | |
sudo docker pull ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi:${{ secrets.PROD_DOCKER_TAG }} | |
sudo docker compose up -d --no-deps gachtaxi | |
# 사용하지 않는 이미지 정리 | |
echo "** 사용하지 않는 Docker 이미지 정리" | |
sudo docker image prune -f |