-
Notifications
You must be signed in to change notification settings - Fork 0
배포 스크립트 추가 #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
배포 스크립트 추가 #31
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| name: Deploy Prod Backend | ||
|
|
||
| concurrency: | ||
| group: be-prod | ||
| cancel-in-progress: true # Deploy 작업은 한번에 하나만 실행하도록 제한 | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - 'v*.*.*-be' | ||
|
|
||
| jobs: | ||
| build-and-push: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| outputs: | ||
| version: ${{ steps.get_version.outputs.VERSION }} | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 # 태그 검증을 위해 전체 Git 히스토리 필요 | ||
|
|
||
| - name: tag가 be/prod branch에 있는지 검증 | ||
| run: | | ||
| git fetch origin be/prod | ||
| if git merge-base --is-ancestor ${{ github.sha }} origin/be/prod; then | ||
| echo "이 태그는 be/prod branch에 포함되어 있습니다. 배포를 진행합니다." | ||
| else | ||
| echo "Error: 이 tag는 be/prod branch에 없습니다." | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Git tag에서 버전 정보 추출 | ||
| id: get_version | ||
| run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Docker Hub 로그인 | ||
| run: echo "${{ secrets.DOCKER_ACCESS_TOKEN }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin | ||
|
|
||
| - name: QEMU 설정 | ||
| uses: docker/setup-qemu-action@v3 | ||
|
|
||
| - name: Docker Buildx 설정 | ||
| uses: docker/setup-buildx-action@v3 | ||
|
|
||
| - name: Docker Image Build & Push | ||
| uses: docker/build-push-action@v6 | ||
| with: | ||
| context: ./ | ||
| file: ./Dockerfile | ||
| push: true | ||
| platforms: linux/arm64 | ||
| tags: | | ||
| ${{ secrets.PROD_IMAGE_NAME }}:${{ steps.get_version.outputs.VERSION }} | ||
| ${{ secrets.PROD_IMAGE_NAME }}:latest | ||
|
|
||
| deploy: | ||
| runs-on: [ self-hosted, recycle-study-server-prod ] | ||
| needs: build-and-push | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: .env file 생성 | ||
| run: | | ||
| cat <<EOF > .env | ||
| DOCKER_IMAGE=${{ secrets.PROD_IMAGE_NAME }} | ||
| DOCKER_TAG=${{ needs.build-and-push.outputs.version }} | ||
| ${{ secrets.ENV_PROD }} | ||
| EOF | ||
|
|
||
| - name: log directory 준비 | ||
| run: | | ||
| sudo mkdir -p /app/log | ||
| sudo chown -R 1001:1001 /app/log | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - name: 배포 시작 | ||
| run: | | ||
| docker compose -f docker-compose.prod.yaml pull | ||
| docker compose -f docker-compose.prod.yaml up -d | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| - name: 신규 deploy에 대한 health check | ||
| run: | | ||
| echo "==========================================" | ||
| echo "Health Check 시작" | ||
| echo "==========================================" | ||
|
|
||
| echo "애플리케이션 초기화 대기 중... (10초)" | ||
| sleep 10 | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # 최대 12번 시도 (총 60초) | ||
| for i in {1..12}; do | ||
| STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/actuator/health || echo "000") | ||
|
|
||
| if [ "$STATUS" -eq 200 ]; then | ||
| echo "==========================================" | ||
| echo "배포 성공!" | ||
| echo "==========================================" | ||
| docker ps --filter "name=recycle-study-server" | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "[$i/12] 헬스체크 재시도... (응답 코드: $STATUS)" | ||
| sleep 5 | ||
| done | ||
|
|
||
| # Health Check 실패 시 | ||
| echo "==========================================" | ||
| echo "Health Check 실패" | ||
| echo "==========================================" | ||
| echo "컨테이너 상태:" | ||
| docker ps -a --filter "name=recycle-study-server" | ||
| echo "" | ||
| echo "컨테이너 로그:" | ||
| docker logs recycle-study-server --tail 100 || true | ||
|
|
||
| exit 1 | ||
|
|
||
| - name: Docker resource 정리 | ||
| if: always() | ||
| run: | | ||
| docker image prune -a -f | ||
| docker builder prune -a -f | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Build stage | ||
| FROM amazoncorretto:21-alpine3.19-jdk AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY gradlew . | ||
| COPY gradle gradle | ||
| COPY build.gradle . | ||
| COPY settings.gradle . | ||
| COPY src src | ||
|
|
||
| RUN chmod +x ./gradlew | ||
| RUN ./gradlew bootJar --no-daemon | ||
|
|
||
| # Runtime stage | ||
| FROM amazoncorretto:21-alpine3.19 | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| WORKDIR /app | ||
|
|
||
| RUN apk add --no-cache curl tzdata && \ | ||
| cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \ | ||
| echo "Asia/Seoul" > /etc/timezone && \ | ||
| apk del tzdata | ||
|
|
||
| RUN addgroup -g 1001 appgroup && adduser -u 1001 -G appgroup -D appuser | ||
| RUN mkdir -p /app/log && chown -R appuser:appgroup /app | ||
|
|
||
| COPY --from=builder --chown=appuser:appgroup /app/build/libs/*.jar app.jar | ||
|
|
||
| USER appuser | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| ENTRYPOINT ["java", "-jar", "app.jar"] | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| services: | ||
| app: | ||
| container_name: recycle-study-server | ||
| image: ${DOCKER_IMAGE}:${DOCKER_TAG} | ||
| restart: unless-stopped | ||
| ports: | ||
| - "8080:8080" | ||
| env_file: | ||
| - .env | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| volumes: | ||
| - /app/log:/app/log | ||
jhan0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.