codetree 깃허브 연동 시 기존 레포의 폴더안으로 연동하기

·2025년 4월 8일
0
post-thumbnail

Git Subtree 방식으로 코드트리 문제를 알고리즘 레포에 깔끔하게 정리하기

코드트리는 자체적으로 GitHub 연동 기능을 제공합니다.
GitHub 연동을 하면, 지정한 레포지토리의 루트 디렉토리에 날짜별로 폴더가 생성됩니다.

코드트리 자동 푸시 예시

하지만 위처럼 루트에 직접 폴더가 생성되기 때문에,
백준, 프로그래머스 등 다양한 플랫폼의 문제 풀이를 하나의 레포지토리에서 정리하는 사용자에게는 다소 아쉬울 수 있습니다.

이를 해결하기 위해 저는 Git Subtree를 사용해 codetree/ 폴더 아래로 문제를 정리해보았습니다.

1️⃣ 코드트리 전용 레포지토리 만들기

먼저 코드트리의 GitHub 연동 시 사용할 전용 레포지토리를 하나 생성합니다.

codetree-solutions 레포 생성

레포 생성 시 README.md 또는 초기 커밋이 꼭 있어야 main 브랜치가 자동 생성됩니다.


2️⃣ 알고리즘 통합 레포에 코드트리 레포 연결

이제 자신이 사용하는 알고리즘 레포에서, 코드트리 레포를 서브트리로 연결합니다.

# 1. 코드트리 레포를 remote 등록
git remote add codetree https://github.com/your-username/codetree-solutions.git

# 2. 원격 저장소의 내용을 가져옴
git fetch codetree

# 3. 코드트리의 main 브랜치를 codetree/ 폴더로 가져옴
git subtree add --prefix=codetree codetree main
# squash 옵션을 추가하면 외부 커밋이 하나로 압축되어 로그가 깔끔해지지만 저는 기존 커밋을 유지하기를 원하기때문에 패스
git subtree add --prefix=codetree codetree main --squash

그러면 기존 레포지토리의 구조에서 codetree 폴더가 생성됩니다.


3️⃣ 코드트리 레포 변경사항 반영 (수동)

  • 코드트리는 보통 매일 대략 자정쯤 자동 커밋이 됩니다.
  • 코드트리 레포에 새 문제가 올라오면, 아래 명령어로 codetree/ 폴더를 업데이트할 수 있습니다.
# 1. 코드트리 최신 내용 가져오기
git fetch codetree

# 2. codetree/main 브랜치를 codetree/ 폴더에 병합
git subtree pull --prefix=codetree codetree main

4️⃣ 코드트리 자동 동기화 (GitHub Actions)

  • 매일 새벽 1시에 자동으로 코드트리 내용을 반영하도록 GitHub Actions를 설정해보겠습니다.

✅ 워크플로 파일 생성

  • .github/workflows/codetree-sync.yml 파일을 아래 내용으로 생성합니다:
name: Sync Codetree Subtree

on:
  schedule:
    - cron: '0 16 * * *'  # 매일 KST 1시 (UTC 기준 16시)
  workflow_dispatch:      # 수동 실행 버튼도 추가

jobs:
  sync:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout algorithm repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # subtree 병합을 위해 전체 커밋 기록이 필요함

      - name: Set Git config
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Add codetree remote
        run: |
          git remote add codetree https://github.com/your-username/codetree-solutions.git
          git fetch codetree

      - name: Pull codetree subtree
        run: |
          git subtree pull --prefix=codetree codetree main -m "chore(subtree): sync codetree"

      - name: Push updated subtree
        run: |
          git push origin main

✅ 권한 설정 확인

  • GitHub 레포지토리에서 Settings > Actions > General > Workflow permissions 로 이동 후, Read and write permissions 로 설정되어 있어야 합니다.

✅ 마무리

  • 아래와 같이 기존 구조에 맞게 폴더가 유지되면서 코드트리의 깃허브 연동 기능을 사용할 수 있습니다.
  • Git Subtree를 활용하면 외부 레포의 코드(코드트리 풀이)를 내부 디렉토리처럼 관리할 수 있어, 플랫폼별로 문제를 폴더별로 정리하고 싶은 분들에게 매우 유용합니다.
  • 코드트리처럼 자동으로 커밋되는 구조라면, 위 방식으로 깔끔하게 자동 정리해보세요!

0개의 댓글