Skip to content

Merge pull request #70 from HanaPiece/feat/delete-transactional #16

Merge pull request #70 from HanaPiece/feat/delete-transactional

Merge pull request #70 from HanaPiece/feat/delete-transactional #16

Workflow file for this run

name: CI/CD using github actions & docker
# event trigger
# main 브랜치에 push가 되었을 때 실행
on:
push:
branches: [ "main" ]
permissions:
contents: read
jobs:
CI-CD:
runs-on: ubuntu-latest
steps:
# JDK 설정
- 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@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# application.yml 파일 생성
- name: make application.yml
run: |
mkdir -p ./src/main/resources
cd ./src/main/resources
touch ./application.yml
echo "spring:" >> ./application.yml
echo " jpa:" >> ./application.yml
echo " database: mysql" >> ./application.yml
echo " hibernate:" >> ./application.yml
echo " ddl-auto: none" >> ./application.yml
echo " show-sql: true" >> ./application.yml
echo " datasource:" >> ./application.yml
echo " url: ${{ secrets.DB_URL }}" >> ./application.yml
echo " username: ${{ secrets.DB_USERNAME }}" >> ./application.yml
echo " password: ${{ secrets.DB_PASSWORD }}" >> ./application.yml
echo " driver-class-name: com.mysql.cj.jdbc.Driver" >> ./application.yml
echo "jwt:" >> ./application.yml
echo " secret: ${{ secrets.JWT_SECRET }}" >> ./application.yml
echo " access-expired-ms: 3600000000" >> ./application.yml
echo " claims:" >> ./application.yml
echo " auth-key: userId" >> ./application.yml
echo "ai:" >> ./application.yml
echo " gemini:" >> ./application.yml
echo " base-url: https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash-001:generateContent?key=" >> ./application.yml
echo " api-key: ${{ secrets.AI_API_KEY }}" >> ./application.yml
echo "server:" >> ./application.yml
echo " servlet:" >> ./application.yml
echo " encoding:" >> ./application.yml
echo " charset: UTF-8" >> ./application.yml
echo " enabled: true" >> ./application.yml
echo " force: true" >> ./application.yml
shell: bash
# Gradle 빌드
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew build -x test
# Docker 빌드 및 푸시
- name: Docker build & push
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/hana_piece .
docker push ${{ secrets.DOCKER_USERNAME }}/hana_piece
# 배포
- name: Deploy
uses: appleboy/ssh-action@master
id: deploy
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
script: |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/hana_piece
# 기존 컨테이너를 확인하여 중지 및 삭제
existing_container=$(docker ps -q --filter "name=hana_piece")
if [ ! -z "$existing_container" ]; then
docker stop $existing_container
docker rm $existing_container
fi
sudo docker run -d -p 8080:8080 --name hana_piece -e DATABASE_URL=${{ secrets.DATABASE_URL }} ${{ secrets.DOCKER_USERNAME }}/hana_piece
sudo docker image prune -f