Backend Main Continuous Delivery #40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Backend Main Continuous Delivery | |
| on: | |
| push: | |
| paths: | |
| - "backend/main-service/**" | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| permissions: write-all | |
| env: | |
| DEVLOG_GOOGLE_CLIENT_ID: ${{secrets.DEVLOG_GOOGLE_CLIENT_ID}} | |
| DEVLOG_GOOGLE_CLIENT_SECRET: ${{secrets.DEVLOG_GOOGLE_CLIENT_SECRET}} | |
| MAIN_JAR_TARGET_PATH: "backend/main-service/build/libs/main-service.jar" | |
| MAIN_DOCKER_TARGET_PATH: "backend/main-service/Dockerfile.prod" | |
| MAIN_FOLDER_PATH: "/home/app/backend/main-service" | |
| jobs: | |
| # 사전 작업 | |
| setup: | |
| runs-on: ubuntu-22.04 | |
| # defaults: | |
| # run: | |
| # working-directory: backend | |
| outputs: | |
| build-cache-key: ${{ steps.build-cache.outputs.key }} | |
| current-datetime: ${{ steps.datetime.outputs.datetime }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Get current date and time | |
| id: datetime | |
| run: echo "datetime=$(date '+%Y-%m-%d_%H-%M-%S')" >> $GITHUB_OUTPUT | |
| - name: Generate main-service build cache key | |
| id: build-cache | |
| run: echo "key=$(echo build-${{ runner.os }}-gradle-${{ hashFiles('backend/main-service/**/*.gradle*', 'backend/main-service/**/gradle-wrapper.properties') }})" >> $GITHUB_OUTPUT | |
| - name: docker-compose.prod.yml hash key | |
| id: compose-hash | |
| run: | | |
| echo "DOCKER_KEY=$(sha256sum docker-compose.prod.yml | awk '{ print $1 }')" >> $GITHUB_ENV | |
| # 캐시가 존재하면 파일 전송 스킵 | |
| - name: Check cache for docker-compose.prod.yml | |
| id: cache-check | |
| uses: actions/cache@v4 | |
| with: | |
| path: ./docker-compose.prod.yml | |
| key: ${{ runner.os }}-docker-compose-prod-${{ env.DOCKER_KEY }} | |
| - name: Modify permissions | |
| run: chmod o+rwx docker-compose.prod.yml | |
| - name: Send docker-compose.prod.yml | |
| if: steps.cache-check.outputs.cache-hit != 'true' | |
| uses: appleboy/scp-action@master | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| source: "docker-compose.prod.yml" | |
| target: "/home/app" | |
| build: | |
| needs: setup | |
| runs-on: ubuntu-22.04 | |
| defaults: | |
| run: | |
| working-directory: backend/main-service | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Gradle Caching | |
| id: cache-gradle | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ needs.setup.outputs.build-cache-key }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| - name: Cache Check | |
| if: steps.cache-gradle.outputs.cache-hit == 'true' | |
| run: echo 'Gradle cache hit!' | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: "17" | |
| distribution: "temurin" | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: Build with Gradle | |
| run: ./gradlew build -x test | |
| # (Actions에서 생성한 파일은 권한을 수정해야 scp로 보낼 때 오류가 안생김) | |
| - name: Modify permissions | |
| run: chmod o+rwx build/libs/main-service.jar | |
| - name: Send jar | |
| uses: appleboy/scp-action@master | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| source: ${{ env.MAIN_JAR_TARGET_PATH }} | |
| target: ${{ env.MAIN_FOLDER_PATH }} | |
| strip_components: 4 # 파일을 복사할 때 source 경로에서 몇 개의 컴포넌트(디렉토리)를 제거할 것인지를 지정합니다. 4이므로, backend/main-service/build/libs/devlog.jar 에서 backend, main-service, build, libs가 사라짐. | |
| - name: Modify permissions Dockerfile.prod | |
| run: chmod o+rwx Dockerfile.prod | |
| - name: Send Dockerfile.prod | |
| uses: appleboy/scp-action@master | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| source: ${{ env.MAIN_DOCKER_TARGET_PATH }} | |
| target: ${{ env.MAIN_FOLDER_PATH }} | |
| strip_components: 2 | |
| startup: | |
| needs: | |
| - build | |
| runs-on: ubuntu-22.04 | |
| steps: | |
| - name: Restart Server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.SERVER_HOST }} | |
| username: ${{ secrets.SERVER_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| cd /home/app | |
| docker-compose -f docker-compose.prod.yml up -d --build main-service |