feat: set CI/CD on main, CI on release-dev (#14) #31
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: Build & deploy | |
on: | |
push: | |
branches: | |
- main | |
- release-dev | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
services: | |
mysql: | |
image: mysql:8.0 | |
env: | |
MYSQL_ROOT_PASSWORD: root | |
MYSQL_DATABASE: testdb | |
MYSQL_USER: user | |
MYSQL_PASSWORD: somepassword | |
ports: | |
- 3306:3306 | |
options: >- | |
--health-cmd="mysqladmin ping -h localhost" | |
--health-interval=10s | |
--health-timeout=5s | |
--health-retries=3 | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '17' | |
- name: Build with Gradle | |
run: | | |
chmod +x gradlew | |
./gradlew build | |
env: | |
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/testdb | |
SPRING_DATASOURCE_USERNAME: user | |
SPRING_DATASOURCE_PASSWORD: somepassword | |
SPRING_JPA_HIBERNATE_DDL_AUTO: create-drop | |
SPRING_PROFILES_ACTIVE: "prod" | |
# 배포는 main 브랜치에서만 실행 | |
- name: Upload JAR to EC2 | |
if: github.ref == 'refs/heads/main' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
source: "build/libs/*-SNAPSHOT.jar" | |
target: "~/app/" | |
- name: Delete latest JAR file | |
if: github.ref == 'refs/heads/main' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
script: | | |
cd ~/app | |
LATEST_JAR=$(ls -t *.jar | head -n 1) || true | |
echo "Latest JAR file: $LATEST_JAR" | |
for file in *.jar; do | |
if [[ "$file" != "$LATEST_JAR" ]]; then | |
echo "Deleting $file..." | |
rm -f "$file" | |
fi | |
done | |
continue-on-error: true | |
- name: Upload Dockerfile and Compose file | |
if: github.ref == 'refs/heads/main' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
source: "deploy-Dockerfile, docker-compose.yaml" | |
target: "~" | |
- name: Deploy via SSH | |
if: github.ref == 'refs/heads/main' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
script: | | |
echo "Starting containers..." | |
docker compose up -d --no-deps myapp |