From d02217a61d6f231dec109e59068fd1ad21d9e1e9 Mon Sep 17 00:00:00 2001 From: grantzile <88519798+Grantzile@users.noreply.github.com> Date: Thu, 16 Jan 2025 00:43:21 +0900 Subject: [PATCH] feat: set CI/CD on main, CI on release-dev (#14) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Test github workflow * fix: ddl_auto - create_drop - 이제 테스트가 데이터베이스에 영향을 주지 않습니다. * feat: Use h2 on build * fix: Use test profile on build * fix: h2 Dialect sync * revert: h2 use on test * fix: use .env & refactor actions - Just for test & dev - Need to be fixed before prod * fix: apply env on build workflow * fix: use seperate test mysql container for build * fix: Rename ssh key * fix: Need ~/app folder, add mkdir command * fix: continue on error - stop running appliation on EC2 * fix: Now deploy w/docker * fix: run -> script typo * fix: typo on keyname * fix: fixed typo * fix: fixed typo on keyname * fix: fix dockerfile dir * fix: typo on dockerfile name * fix: typo on docker script * fix: docker compose env injection * fix: typo and unused .env create * fix: rewrite invalid env injection command * fix: fixed pwd typo, add --no-deps on docker compose up * fix: pwd change * fix: pwd typo * fix: remove hardcoded docker-compose environment * fix: change exposed port * fix: env_file import * fix: typo * feat: set deploy on main, build on main & release-dev --- .github/workflows/build-test.yaml | 98 +++++++++++++++++++++++++++++ deploy-Dockerfile | 7 +++ docker-compose.yaml | 11 ++-- src/main/resources/application.yaml | 1 + 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/build-test.yaml create mode 100644 deploy-Dockerfile diff --git a/.github/workflows/build-test.yaml b/.github/workflows/build-test.yaml new file mode 100644 index 0000000..3142465 --- /dev/null +++ b/.github/workflows/build-test.yaml @@ -0,0 +1,98 @@ +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/scp-action@v0.1.4 + 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/ssh-action@v0.1.10 + 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/scp-action@v0.1.3 + 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/ssh-action@v0.1.10 + 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 diff --git a/deploy-Dockerfile b/deploy-Dockerfile new file mode 100644 index 0000000..5d09fb2 --- /dev/null +++ b/deploy-Dockerfile @@ -0,0 +1,7 @@ +FROM openjdk:17 + +# 가장 최신의 JAR 파일 하나만 있음, app.jar로 복사 +COPY app/build/libs/*.jar /app.jar + +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "/app.jar"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 46793f9..0cc85d9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,17 +12,18 @@ services: networks: - my-network myapp: + build: + context: . + dockerfile: deploy-Dockerfile image: myapp:1.0 + env_file: + - .env restart: on-failure container_name: myapp-container ports: - - "8080:8080" + - "80:8080" depends_on: - mysql - environment: - SPRING_DATASOURCE_URL: "jdbc:mysql://mysql-db:3306/testdb" - SPRING_DATASOURCE_USERNAME: "user" - SPRING_DATASOURCE_PASSWORD: "somepassword" networks: - my-network networks: diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 83640d3..78136a2 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -13,3 +13,4 @@ spring: dialect: org.hibernate.dialect.MySQL8Dialect format_sql: false show_sql: false + ddl_auto: create-drop