From 73d271b9cb3d5e19f18a08474d2e6f019a0d1572 Mon Sep 17 00:00:00 2001 From: Jangan Lee Date: Fri, 8 Nov 2024 18:36:45 +0900 Subject: [PATCH] =?UTF-8?q?[Weekly/10/Chore/Deploy]=20Github=20Action=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8=20=EC=88=98=EC=A0=95=20(#8?= =?UTF-8?q?9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: attempt 1 * fix: attempt 2 (log show) * fix: attempt 3 (add secrets.env) * feat: .env 업데이트 기능 추가 * feat: jobs 단계 세분화 * fix: test fail fix attempt 1 * fix: docker build fail fix attempt 2 * chore: add echo * chore: rename jobs step naming --- .github/workflows/gradle.yml | 102 +++++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 9 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index e1ea7e26..c4dddfa0 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -9,6 +9,16 @@ on: permissions: contents: read +env: + GOOGLE_CLIENT_ID : ${{ secrets.ENV_GOOGLE_CLIENT_ID }} + GOOGLE_CLIENT_SECRET: ${{ secrets.ENV_GOOGLE_CLIENT_SECRET }} + GOOGLE_REDIRECT_URI : ${{ secrets.ENV_GOOGLE_REDIRECT_URI }} + JWT_SECRET : ${{ secrets.ENV_JWT_SECRET }} + KAKAO_CLIENT_ID : ${{ secrets.ENV_KAKAO_CLIENT_ID }} + KAKAO_CLIENT_SECRET : ${{ secrets.ENV_KAKAO_CLIENT_SECRET }} + KAKAO_REDIRECT_URI : ${{ secrets.ENV_KAKAO_REDIRECT_URI }} + KAKAOPAY_SECRET_KEY : ${{ secrets.ENV_KAKAOPAY_SECRET_KEY }} + jobs: ## 1단계: 프로젝트 빌드 Build: @@ -32,37 +42,111 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - - name: Build Application only using gradlew + - name: Build Application run: | chmod +x gradlew - ./gradlew clean build -x test + ./gradlew clean build -x test + + - name: Store build failure reports (execute when build fail) + if: failure() + uses: actions/upload-artifact@v3 + with: + name: build-failure-reports + path: | + **/build/reports/ + + - name: Store build artifacts + uses: actions/upload-artifact@v3 + with: + name: build-artifacts + path: build/libs/*.jar + + ## 2단계: 테스트 실행 + Test: + runs-on: ubuntu-22.04 + needs: Build # Build 단계가 완료되어야 실행됨 + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'corretto' - - name: Test Application using gradlew + - name: Gradle Caching (for faster build) + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Test Application run: | chmod +x gradlew - ./gradlew test + ./gradlew test + + - name: Store test failure reports (execute when test fail) + if: failure() + uses: actions/upload-artifact@v3 + with: + name: test-failure-reports + path: | + **/build/test-results/ + + ## 3단계: Docker 빌드 및 푸시 + Docker-Build: + runs-on: ubuntu-22.04 + needs: Test # Test 단계가 성공적으로 완료되어야 실행됨 + if: github.event_name == 'push' + steps: + - uses: actions/checkout@v4 + + - name: Download Build Artifacts + uses: actions/download-artifact@v3 + with: + name: build-artifacts + path: build/libs - name: Docker Hub Login - if: github.event_name == 'push' uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_TOKEN }} - name: Docker Build - if: github.event_name == 'push' run: docker build -f Dockerfile --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_FULLNAME }} . - name: Docker Push - if: github.event_name == 'push' run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }} - ## 2단계: 서버에 배포 + ## 4단계: 서버에 배포 Deploy: runs-on: ubuntu-22.04 - needs: Build + needs: Docker-Build if: github.event_name == 'push' steps: + - name: Update .env + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: ${{ secrets.SSH_PORT }} + script: | + echo "GOOGLE_CLIENT_ID=${{ secrets.ENV_GOOGLE_CLIENT_ID }}" > ~/.env + echo "GOOGLE_CLIENT_SECRET=${{ secrets.ENV_GOOGLE_CLIENT_SECRET }}" >> ~/.env + echo "GOOGLE_REDIRECT_URI=${{ secrets.ENV_GOOGLE_REDIRECT_URI }}" >> ~/.env + echo "JWT_SECRET=${{ secrets.ENV_JWT_SECRET }}" >> ~/.env + echo "KAKAO_CLIENT_ID=${{ secrets.ENV_KAKAO_CLIENT_ID }}" >> ~/.env + echo "KAKAO_CLIENT_SECRET=${{ secrets.ENV_KAKAO_CLIENT_SECRET }}" >> ~/.env + echo "KAKAO_REDIRECT_URI=${{ secrets.ENV_KAKAO_REDIRECT_URI }}" >> ~/.env + echo "KAKAOPAY_SECRET_KEY=${{ secrets.ENV_KAKAOPAY_SECRET_KEY }}" >> ~/.env + echo "Environment setup has been completed." + - name: Pull New Docker Image uses: appleboy/ssh-action@master with: