From 0c11fef1edd5951faf1f6c6eb2549e75468ca5bb Mon Sep 17 00:00:00 2001 From: Haesong Lee <79689822+pinetree2@users.noreply.github.com> Date: Sun, 18 Feb 2024 21:55:26 +0900 Subject: [PATCH] [ WHAT -176 ] cicd fix (#23) * feat : cicd * fix : modify * fix : modify --------- Co-authored-by: kim ye jin <108534554+dPwls0125@users.noreply.github.com> Co-authored-by: pinetree2 --- .github/workflows/cd-workflows.yaml | 72 +++++++++++++++++++++++++++++ .github/workflows/ci-workflows.yaml | 62 +++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 .github/workflows/cd-workflows.yaml create mode 100644 .github/workflows/ci-workflows.yaml diff --git a/.github/workflows/cd-workflows.yaml b/.github/workflows/cd-workflows.yaml new file mode 100644 index 00000000..223989ef --- /dev/null +++ b/.github/workflows/cd-workflows.yaml @@ -0,0 +1,72 @@ +# 작업명 +name: CD Pipeline +on: + push: + branches: + - 'develop' + - 'main' + +jobs: + CD: + runs-on: ubuntu-latest + env: + RESOURCE_PATH: ./src/main/resources/application.yml + steps: + ## Project JDK 17 Setting + - name: Checkout + uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Set yml file + uses: microsoft/variable-substitution@v1 + with: + files: ${{ env.RESOURCE_PATH }} + env: + spring.datasource.url: ${{ secrets.DEV_SERVER_DNS }} + spring.datasource.username: ${{ secrets.DB_USERNAME }} + spring.datasource.password: ${{ secrets.DB_PASSWORD }} + + # Login to ghcr + - name: Login to ghcr + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: pinetree2 + password: ${{ secrets.GHCR_TOKEN }} + + # Docker 빌드 생성 및 푸시 + - name: dev Docker build & push + if: contains(github.ref, 'main') || contains(github.ref, 'develop') + run: | + docker build -f Dockerfile -t ghcr.io/onion-city/whatssue_be_v2/whatssue:latest . + docker push ghcr.io/Onion-City/Whatssue_BE_v2/whatssue:latest + + + # 개발서버 SSH접속 및 배포 + - name: Deploy + uses: appleboy/ssh-action@master + id: deploy + if: contains(github.ref, 'main') || contains(github.ref, 'develop') + with: + host: ${{ secrets.DEV_SERVER_DNS }} + username: ${{ secrets.DEV_SERVER_USER }} + password: ${{ secrets.DEV_SERVER_PASSWORD }} + port: ${{ secrets.DEV_SERVER_SSH_PORT }} + script: | + echo "${{ secrets.GHCR_TOKEN }}" | sudo docker login ghcr.io -u pinetree2 --password-stdin + if [ "$(docker ps -aq -f name=whatssue-be)" ]; then + sudo docker stop whatssue-be + sudo docker rm whatssue-be + fi + + if [ "$(docker images -q ghcr.io/onion-city/whatssue_be_v2/whatssue)" ]; then + sudo docker rmi ghcr.io/onion-city/whatssue_be_v2/whatssue + fi + sudo docker pull ghcr.io/onion-city/whatssue_be_v2/whatssue:latest + sudo docker stop $(docker ps -a -q) + sudo docker run -d -p 8080:8080 --name whatssue-be ghcr.io/onion-city/whatssue_be_v2/whatssue:latest + sudo docker image prune -a -f diff --git a/.github/workflows/ci-workflows.yaml b/.github/workflows/ci-workflows.yaml new file mode 100644 index 00000000..856bdb06 --- /dev/null +++ b/.github/workflows/ci-workflows.yaml @@ -0,0 +1,62 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: Java CI with Gradle + +on: + push: + branches: + - "develop" + - "main" + pull_request: + branches: + - "develop" + - "main" + +jobs: + build: + + runs-on: ubuntu-latest + + env: + RESOURCE_PATH: ./src/main/resources/application.yml + steps: + ## Project JDK 17 Setting + - name: Checkout + uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Set yml file + uses: microsoft/variable-substitution@v1 + with: + files: ${{ env.RESOURCE_PATH }} + env: + spring.datasource.url: ${{ secrets.DEV_SERVER_DNS }} + spring.datasource.username: ${{ secrets.DB_USERNAME }} + spring.datasource.password: ${{ secrets.DB_PASSWORD }} + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build with Gradle + uses: gradle/gradle-build-action@v2 + with: + arguments: build + cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }} + + - name: Execute Gradle build + run: ./gradlew build + + - name: Upload Apk + uses: actions/upload-artifact@v3 + with: + name: build 파일 + path: build/libs/whatssue-0.0.1-SNAPSHOT-plain.jar \ No newline at end of file