From e8563337caf8b938e4e35940cc68c236d26fe1b3 Mon Sep 17 00:00:00 2001 From: Choi Jun Ho Date: Sun, 5 May 2024 17:18:17 +0900 Subject: [PATCH 1/7] =?UTF-8?q?test(Env)=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=ED=99=98=EA=B2=BD=20yml=20=EB=B6=84=EB=A6=AC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- src/main/resources/application-test.yml | 46 +++++++++++++++++++ .../comunity/CommunityApplicationTests.java | 2 + .../jwt/RefreshTokenRepositoryTest.java | 2 +- 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/application-test.yml diff --git a/.gitignore b/.gitignore index f1e79dc..ca66177 100644 --- a/.gitignore +++ b/.gitignore @@ -37,5 +37,4 @@ out/ .vscode/ ### env ### -.env -application-test.yml \ No newline at end of file +.env \ No newline at end of file diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml new file mode 100644 index 0000000..9425723 --- /dev/null +++ b/src/main/resources/application-test.yml @@ -0,0 +1,46 @@ +spring: + datasource: + url: "jdbc:h2:mem:community;MODE=mysql" + username: "sa" + password: "" + driver-class-name: org.h2.Driver + + # redis 설정 + data: + redis: + host: ${REDIS_HOST} + port: ${REDIS_PORT} + + jpa: + defer-datasource-initialization: true + hibernate: + ddl-auto: create + naming: + physical-strategy: org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy + properties: + hibernate: + dialect: org.hibernate.dialect.H2Dialect + + h2: + console: + enabled: true + path: /h2-console + + security: + oauth2: + client: + registration: + google: + client-id: ${GOOGLE_CLIENT_ID} + client-secret: ${GOOGLE_CLIENT_SECRET} + redirect-uri: ${GOOGLE_REDIRECT_URI} + +logging.level: + org.hibernate: + orm.jdbc.bind: trace + SQL: debug + +jwt: + secret: "${JWT_SECRET}" + + diff --git a/src/test/java/gdsc/comunity/CommunityApplicationTests.java b/src/test/java/gdsc/comunity/CommunityApplicationTests.java index 775da28..1b62628 100644 --- a/src/test/java/gdsc/comunity/CommunityApplicationTests.java +++ b/src/test/java/gdsc/comunity/CommunityApplicationTests.java @@ -2,8 +2,10 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; @SpringBootTest +@ActiveProfiles("test") class CommunityApplicationTests { @Test diff --git a/src/test/java/gdsc/comunity/security/jwt/RefreshTokenRepositoryTest.java b/src/test/java/gdsc/comunity/security/jwt/RefreshTokenRepositoryTest.java index dd1c30e..646f275 100644 --- a/src/test/java/gdsc/comunity/security/jwt/RefreshTokenRepositoryTest.java +++ b/src/test/java/gdsc/comunity/security/jwt/RefreshTokenRepositoryTest.java @@ -14,7 +14,7 @@ import org.springframework.transaction.annotation.Transactional; @Transactional -@ActiveProfiles("dev") +@ActiveProfiles("test") @SpringBootTest(webEnvironment = WebEnvironment.MOCK) class RefreshTokenRepositoryTest { From 1a09b86403621359f7bb29c07e9ee77afaad5382 Mon Sep 17 00:00:00 2001 From: Choi Jun Ho Date: Sun, 5 May 2024 17:31:36 +0900 Subject: [PATCH 2/7] =?UTF-8?q?chore(CI/CD)=20:=20Pull=20Request=EC=97=90?= =?UTF-8?q?=20=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8,=20?= =?UTF-8?q?=EB=B0=B0=ED=8F=AC=20=ED=8C=8C=EC=9D=B4=ED=94=84=EB=9D=BC?= =?UTF-8?q?=EC=9D=B8=20=EC=84=A4=EC=A0=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/gradle.yml | 125 +++++++++++++++++++++++++++++++++++ Dockerfile | 5 ++ docker-compose.dev.yaml | 15 +++++ script/deploy.sh | 7 ++ 4 files changed, 152 insertions(+) create mode 100644 .github/workflows/gradle.yml create mode 100644 Dockerfile create mode 100644 docker-compose.dev.yaml create mode 100644 script/deploy.sh diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000..702bb40 --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,125 @@ +name: Java CI with Gradle + +on: + # after merge to main with pr + pull_request: + types: [opened, synchronize, reopened, closed] + branches: [ "develop" ] + +jobs: + test: + if: github.action == 'opened' || github.action == 'synchronize' || github.action == 'reopened' + runs-on: ubuntu-22.04 + + services: + redis: + image: redis:latest + ports: + - 6379:6379 + + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3.1.0 + + - name: Test with Gradle Wrapper + run: ./gradlew test + env: + REDIS_HOST: ${{ secrets.REDIS_HOST }} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} + GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} + GOOGLE_REDIRECT_URI: ${{ secrets.GOOGLE_REDIRECT_URI }} + JWT_SECRET: ${{ secrets.JWT_SECRET }} + + build-and-deploy: + if: github.action == 'closed' && github.event.pull_request.merged == true + + runs-on: ubuntu-22.04 + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3.1.0 + + - name: Build with Gradle Wrapper + run: ./gradlew build -x test + env: + REDIS_HOST: ${{ secrets.REDIS_HOST }} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} + GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} + GOOGLE_REDIRECT_URI: ${{ secrets.GOOGLE_REDIRECT_URI }} + JWT_SECRET: ${{ secrets.JWT_SECRET }} + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and Push Docker Image + run: | + docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_REPOSITORY }}:${{ github.sha }} . + docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_REPOSITORY }}:${{ github.sha }} + + - name: Modify docker-compose.dev.yaml + run: | + sed -i "s|REPLACE_IMAGE_TAG|${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_REPOSITORY }}:${{ github.sha }}|g" docker-compose.dev.yaml + + - name: Send script to remote server + uses: appleboy/scp-action@master + with: + host: ${{ secrets.REMOTE_HOST }} + username: ${{ secrets.REMOTE_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + source: "docker-compose.dev.yaml,script/deploy.sh" + target: /home/${{ secrets.REMOTE_USERNAME }}/gdsc-project + + - name: SSH to remote server and run docker-compose + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.REMOTE_HOST }} + username: ${{ secrets.REMOTE_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + script: | + cd /home/${{ secrets.REMOTE_USERNAME }}/gdsc-project/script + chmod +x deploy.sh + ./deploy.sh + + dependency-submission: + if: github.action == 'closed' && github.event.pull_request.merged == true + + runs-on: ubuntu-latest + permissions: + contents: write + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + # Generates and submits a dependency graph, enabling Dependabot Alerts for all project dependencies. + # See: https://github.com/gradle/actions/blob/main/dependency-submission/README.md + - name: Generate and submit dependency graph + uses: gradle/actions/dependency-submission@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..eb2886a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM amd64/amazoncorretto:17 +WORKDIR /usr/src/app +ARG JAR_FILE=build/libs/*.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java", "-Duser.timezone=Asia/Seoul", "-jar", "app.jar"] \ No newline at end of file diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml new file mode 100644 index 0000000..c455d55 --- /dev/null +++ b/docker-compose.dev.yaml @@ -0,0 +1,15 @@ +version: '3' +services: + gradingx-redis: + image: redis:latest + container_name: redis + hostname: redis + ports: + - "6379:6379" + + gradingx-server: + image: REPLACE_IMAGE_TAG + ports: + - "8080:8080" + depends_on: + - gradingx-redis \ No newline at end of file diff --git a/script/deploy.sh b/script/deploy.sh new file mode 100644 index 0000000..a41ebc6 --- /dev/null +++ b/script/deploy.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +cd .. + +docker-compose -f docker-compose.dev.yaml up --build -d + +docker-compose ps From a8dcc39393bd520aa4e1c9db6b3b53e65572da71 Mon Sep 17 00:00:00 2001 From: fhiller Date: Tue, 7 May 2024 10:53:56 +0900 Subject: [PATCH 3/7] =?UTF-8?q?chore(CI/CD)=20:=20ci,=20cd=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EB=B6=84=EB=A6=AC.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/{gradle.yml => cd.yml} | 40 +----------------------- .github/workflows/ci.yml | 40 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 39 deletions(-) rename .github/workflows/{gradle.yml => cd.yml} (71%) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/gradle.yml b/.github/workflows/cd.yml similarity index 71% rename from .github/workflows/gradle.yml rename to .github/workflows/cd.yml index 702bb40..ba7d8ae 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/cd.yml @@ -3,47 +3,11 @@ name: Java CI with Gradle on: # after merge to main with pr pull_request: - types: [opened, synchronize, reopened, closed] + types: [ closed ] branches: [ "develop" ] jobs: - test: - if: github.action == 'opened' || github.action == 'synchronize' || github.action == 'reopened' - runs-on: ubuntu-22.04 - - services: - redis: - image: redis:latest - ports: - - 6379:6379 - - permissions: - contents: read - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'temurin' - - - name: Setup Gradle - uses: gradle/actions/setup-gradle@v3.1.0 - - - name: Test with Gradle Wrapper - run: ./gradlew test - env: - REDIS_HOST: ${{ secrets.REDIS_HOST }} - REDIS_PORT: ${{ secrets.REDIS_PORT }} - GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} - GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} - GOOGLE_REDIRECT_URI: ${{ secrets.GOOGLE_REDIRECT_URI }} - JWT_SECRET: ${{ secrets.JWT_SECRET }} - build-and-deploy: - if: github.action == 'closed' && github.event.pull_request.merged == true - runs-on: ubuntu-22.04 permissions: contents: read @@ -105,8 +69,6 @@ jobs: ./deploy.sh dependency-submission: - if: github.action == 'closed' && github.event.pull_request.merged == true - runs-on: ubuntu-latest permissions: contents: write diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2fec041 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: Java CI with Gradle + +on: + # after merge to main with pr + pull_request: + types: [ opened, synchronize, reopened ] + branches: [ "develop" ] + +jobs: + test: + runs-on: ubuntu-22.04 + services: + redis: + image: redis:latest + ports: + - 6379:6379 + + permissions: + contents: read + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3.1.0 + + - name: Test with Gradle Wrapper + run: ./gradlew test + env: + REDIS_HOST: ${{ secrets.REDIS_HOST }} + REDIS_PORT: ${{ secrets.REDIS_PORT }} + GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} + GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} + GOOGLE_REDIRECT_URI: ${{ secrets.GOOGLE_REDIRECT_URI }} + JWT_SECRET: ${{ secrets.JWT_SECRET }} \ No newline at end of file From 06703cfcc37220801b5d15fe808d41053b1f4163 Mon Sep 17 00:00:00 2001 From: fhiller Date: Tue, 7 May 2024 10:55:19 +0900 Subject: [PATCH 4/7] =?UTF-8?q?chore(CI/CD)=20:=20expose=20=ED=82=A4?= =?UTF-8?q?=EC=9B=8C=EB=93=9C=EB=A5=BC=20=ED=86=B5=ED=95=9C=20=EC=99=B8?= =?UTF-8?q?=EB=B6=80=20=ED=98=B8=EC=8A=A4=ED=8A=B8=20=EC=B0=A8=EB=8B=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.dev.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index c455d55..feae087 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -4,8 +4,8 @@ services: image: redis:latest container_name: redis hostname: redis - ports: - - "6379:6379" + expose: + - "6379" gradingx-server: image: REPLACE_IMAGE_TAG From a32261bf4cbb97a3d3af038c532b48e7051a57ef Mon Sep 17 00:00:00 2001 From: fhiller Date: Tue, 7 May 2024 11:04:22 +0900 Subject: [PATCH 5/7] =?UTF-8?q?chore(CI/CD)=20:=20gradlew=20=EA=B6=8C?= =?UTF-8?q?=ED=95=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 3 --- .github/workflows/ci.yml | 3 --- gradlew | 0 3 files changed, 6 deletions(-) mode change 100644 => 100755 gradlew diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index ba7d8ae..fcc3de6 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -20,9 +20,6 @@ jobs: java-version: '17' distribution: 'temurin' - - name: Setup Gradle - uses: gradle/actions/setup-gradle@v3.1.0 - - name: Build with Gradle Wrapper run: ./gradlew build -x test env: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fec041..7c68834 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,9 +26,6 @@ jobs: java-version: '17' distribution: 'temurin' - - name: Setup Gradle - uses: gradle/actions/setup-gradle@v3.1.0 - - name: Test with Gradle Wrapper run: ./gradlew test env: diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 From 534e76dc9bdca72f64fac51de919689c2956181d Mon Sep 17 00:00:00 2001 From: Choi Jun Ho Date: Sat, 11 May 2024 15:59:31 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix(CI/CD)=20:=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=8B=A4=ED=96=89=20=EC=97=90=EB=9F=AC=20=ED=95=B4=EA=B2=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 4 ++-- .github/workflows/ci.yml | 3 +-- script/deploy.sh | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index fcc3de6..31f756f 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -1,4 +1,4 @@ -name: Java CI with Gradle +name: SpringBoot deploy with Docker on: # after merge to main with pr @@ -63,7 +63,7 @@ jobs: script: | cd /home/${{ secrets.REMOTE_USERNAME }}/gdsc-project/script chmod +x deploy.sh - ./deploy.sh + sudo ./deploy.sh dependency-submission: runs-on: ubuntu-latest diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7c68834..27d038f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,6 @@ -name: Java CI with Gradle +name: SpringBoot test with Redis on: - # after merge to main with pr pull_request: types: [ opened, synchronize, reopened ] branches: [ "develop" ] diff --git a/script/deploy.sh b/script/deploy.sh index a41ebc6..3f6b892 100644 --- a/script/deploy.sh +++ b/script/deploy.sh @@ -4,4 +4,4 @@ cd .. docker-compose -f docker-compose.dev.yaml up --build -d -docker-compose ps +docker-compose -f docker-compose.dev.yaml ps From f6787d2f0ba28745ec8b31ed41159300ba7b8757 Mon Sep 17 00:00:00 2001 From: Choi Jun Ho Date: Sat, 11 May 2024 17:43:57 +0900 Subject: [PATCH 7/7] =?UTF-8?q?fix(CI/CD)=20:=20=ED=99=98=EA=B2=BD=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4=EA=B2=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 13 +++++-------- docker-compose.dev.yaml | 7 +++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 31f756f..f0fe8ac 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -22,13 +22,6 @@ jobs: - name: Build with Gradle Wrapper run: ./gradlew build -x test - env: - REDIS_HOST: ${{ secrets.REDIS_HOST }} - REDIS_PORT: ${{ secrets.REDIS_PORT }} - GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }} - GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }} - GOOGLE_REDIRECT_URI: ${{ secrets.GOOGLE_REDIRECT_URI }} - JWT_SECRET: ${{ secrets.JWT_SECRET }} - name: Login to Docker Hub uses: docker/login-action@v2 @@ -43,7 +36,11 @@ jobs: - name: Modify docker-compose.dev.yaml run: | - sed -i "s|REPLACE_IMAGE_TAG|${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_REPOSITORY }}:${{ github.sha }}|g" docker-compose.dev.yaml + sed -i "s|REPLACE_IMAGE_TAG|${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_REPOSITORY }}:${{ github.sha }}|g" docker-compose.dev.yaml && + sed -i "s|REPLACE_GOOGLE_CLIENT_ID|${{ secrets.GOOGLE_CLIENT_ID }}|g" docker-compose.dev.yaml && + sed -i "s|REPLACE_GOOGLE_CLIENT_SECRET|${{ secrets.GOOGLE_CLIENT_SECRET }}|g" docker-compose.dev.yaml && + sed -i "s|REPLACE_GOOGLE_REDIRECT_URI|${{ secrets.GOOGLE_REDIRECT_URI }}|g" docker-compose.dev.yaml && + sed -i "s|REPLACE_JWT_SECRET|${{ secrets.JWT_SECRET }}|g" docker-compose.dev.yaml - name: Send script to remote server uses: appleboy/scp-action@master diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index feae087..bf17b9c 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -9,6 +9,13 @@ services: gradingx-server: image: REPLACE_IMAGE_TAG + environment: + - REDIS_HOST=localhost + - REDIS_PORT=6379 + - GOOGLE_CLIENT_ID=REPLACE_GOOGLE_CLIENT_ID + - GOOGLE_CLIENT_SECRET=REPLACE_GOOGLE_CLIENT_SECRET + - GOOGLE_REDIRECT_URI=REPLACE_GOOGLE_REDIRECT_URI + - JWT_SECRET=REPLACE_JWT_SECRET ports: - "8080:8080" depends_on: