From 1a09b86403621359f7bb29c07e9ee77afaad5382 Mon Sep 17 00:00:00 2001 From: Choi Jun Ho Date: Sun, 5 May 2024 17:31:36 +0900 Subject: [PATCH] =?UTF-8?q?chore(CI/CD)=20:=20Pull=20Request=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8,=20=EB=B0=B0?= =?UTF-8?q?=ED=8F=AC=20=ED=8C=8C=EC=9D=B4=ED=94=84=EB=9D=BC=EC=9D=B8=20?= =?UTF-8?q?=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