refactor: docker-compose-prod.yml #209
Workflow file for this run
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: deploy | |
| on: | |
| push: | |
| paths: | |
| - '.github/workflows/**' | |
| - 'backend/**' | |
| branches: [ "main", "develop" ] | |
| pull_request: | |
| paths: | |
| - 'backend/**' | |
| branches: [ "main", "develop" ] | |
| jobs: | |
| backend-ci: | |
| runs-on: ubuntu-latest | |
| defaults: | |
| run: | |
| working-directory: ./backend | |
| env: | |
| APPLICATION_SECRET: ${{ secrets.APPLICATION_SECRET }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| # Gradle 캐싱 제거 -> 처음부터 빌드 | |
| - name: Cache Gradle packages | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}-v2 | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| - name: application-secret.yml 생성 | |
| run: | | |
| echo "$APPLICATION_SECRET" > src/main/resources/application-secret.yml | |
| - name: Docker Login | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Docker Buildx 설치 | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Start services for test | |
| run: docker compose up -d | |
| - name: Run tests with Gradle | |
| run: ./gradlew test | |
| - name: Stop services after test | |
| if: always() | |
| run: docker compose down | |
| # 테스트 실패 시 리포트를 아티팩트로 업로드하여 디버깅을 돕습니다. | |
| - name: Upload test reports | |
| if: failure() | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: test-reports | |
| path: backend/build/reports/tests/test/ | |
| # 2. 릴리스 : main/develop 브랜치로 Push될 때만 실행 | |
| makeTagAndRelease: | |
| name: Create Tag and Release | |
| # if: github.event_name == 'push' | |
| needs: backend-ci | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| outputs: | |
| tag_name: ${{ steps.create_tag.outputs.new_tag }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Create Tag | |
| id: create_tag | |
| uses: mathieudutour/github-tag-action@v6.2 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create Release | |
| id: create_release | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: ${{ steps.create_tag.outputs.new_tag }} | |
| release_name: Release ${{ steps.create_tag.outputs.new_tag }} | |
| body: ${{ steps.create_tag.outputs.changelog }} | |
| draft: false | |
| prerelease: false | |
| # 3. 빌드 및 배포: main/develop 브랜치로 Push될 때만 실행 | |
| buildImageAndPush: | |
| name: 도커 이미지 빌드와 푸시 | |
| if: github.event_name == 'push' | |
| needs: makeTagAndRelease | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: 빌드 컨텍스트 확인 (backend 폴더 내용) | |
| run: | | |
| echo "--- Docker 빌드에 사용될 backend 폴더의 전체 파일 목록입니다 ---" | |
| ls -laR ./backend | |
| - name: application-secret.yml 생성 | |
| env: | |
| APPLICATION_SECRET: ${{ secrets.APPLICATION_SECRET }} | |
| run: | | |
| mkdir -p ./backend/src/main/resources | |
| echo "$APPLICATION_SECRET" > ./backend/src/main/resources/application-secret.yml | |
| - name: Docker Buildx 설치 | |
| uses: docker/setup-buildx-action@v2 | |
| - name: 레지스트리 로그인 | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: set lower case owner name | |
| id: set_owner | |
| run: | | |
| echo "OWNER_LC=${OWNER,,}" >> ${GITHUB_ENV} | |
| env: | |
| OWNER: '${{ github.repository_owner }}' | |
| - name: 빌드 앤 푸시 | |
| uses: docker/build-push-action@v3 | |
| with: | |
| context: ./backend | |
| push: true | |
| # 이 옵션을 추가하여 Docker 빌드 캐시를 사용하지 않도록 강제합니다. | |
| no-cache: true | |
| tags: | | |
| ghcr.io/${{ env.OWNER_LC }}/catch-course:${{ needs.makeTagAndRelease.outputs.tag_name }} | |
| ghcr.io/${{ env.OWNER_LC }}/catch-course:latest |