feat: 알고리즘_문제해결 #51
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: Create Blog Post | |
| on: | |
| push: | |
| branches: [main] | |
| paths: | |
| - 'src/ver2/**' | |
| jobs: | |
| create-blog-post: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: code checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 2 | |
| - name: setup for Korean filenames | |
| run: | | |
| git config --global core.quotepath false | |
| echo "LANG=C.UTF-8" >> $GITHUB_ENV | |
| echo "LC_ALL=C.UTF-8" >> $GITHUB_ENV | |
| - name: get latest problem info | |
| id: problem_info | |
| run: | | |
| python3 << 'EOF' | |
| import os | |
| import re | |
| import subprocess | |
| # 가장 최근 feat 커밋 찾기 | |
| result = subprocess.run( | |
| ['git', 'log', '--grep=^feat:', '--format=%H', '-1'], | |
| capture_output=True, | |
| text=True | |
| ) | |
| commit_hash = result.stdout.strip() | |
| if not commit_hash: | |
| print("❌ No feat commit found") | |
| exit(0) | |
| print(f"✅ feat commit: {commit_hash}") | |
| # 해당 커밋의 파일 목록 가져오기 | |
| result = subprocess.run( | |
| ['git', 'show', '--name-only', '--format=', commit_hash], | |
| capture_output=True, | |
| text=True, | |
| encoding='utf-8' | |
| ) | |
| files = result.stdout.strip().split('\n') | |
| # README 찾기 | |
| readme_path = None | |
| for f in files: | |
| if 'src/ver2/' in f and 'README.md' in f and f != 'README.md': | |
| readme_path = f.strip('"') # 따옴표 제거 | |
| break | |
| if not readme_path: | |
| print("❌ No README found") | |
| exit(0) | |
| print(f"✅ Found README: {readme_path}") | |
| # README 읽기 | |
| try: | |
| with open(readme_path, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| except: | |
| print(f"❌ Cannot read file: {readme_path}") | |
| exit(0) | |
| # 제목 추출 | |
| title_match = re.search(r'^# (.+)$', content, re.MULTILINE) | |
| title = title_match.group(1) if title_match else '' | |
| # 카테고리 추출 | |
| cat_match = re.search(r'## 카테고리\s*\n([^\n#]+)', content) | |
| if cat_match: | |
| categories = re.findall(r'`([^`]+)`', cat_match.group(1)) | |
| first_category = categories[0] if categories else '기타' | |
| all_categories = '\n'.join(categories) | |
| else: | |
| first_category = '기타' | |
| all_categories = '기타' | |
| # README 저장 | |
| with open('/tmp/problem_readme.md', 'w', encoding='utf-8') as f: | |
| f.write(content) | |
| # GitHub Output | |
| with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf-8') as f: | |
| f.write(f"problem_name={title}\n") | |
| f.write(f"first_category={first_category}\n") | |
| f.write(f"all_categories<<EOF\n{all_categories}\nEOF\n") | |
| f.write("readme_exists=true\n") | |
| print(f"✅ problem_name={title}") | |
| print(f"✅ first_category={first_category}") | |
| EOF | |
| - name: checkout blog repo | |
| if: steps.problem_info.outputs.readme_exists == 'true' | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: choijw1004/choijw1004.github.io | |
| token: ${{ secrets.BLOG_PAT }} | |
| path: blog-repo | |
| - name: create blog post | |
| if: steps.problem_info.outputs.readme_exists == 'true' | |
| run: | | |
| DATE=$(date +%Y-%m-%d) | |
| DATETIME=$(date +"%Y-%m-%d %H:%M:%S +0900") | |
| cd blog-repo/_posts | |
| LAST_NUM=$(ls ${DATE}-*.md 2>/dev/null | \ | |
| sed "s/${DATE}-\([0-9]*\)\.md/\1/" | \ | |
| sort -n | \ | |
| tail -1) | |
| if [ -z "$LAST_NUM" ]; then | |
| NEXT_NUM=0 | |
| else | |
| NEXT_NUM=$((LAST_NUM + 1)) | |
| fi | |
| FILENAME="${DATE}-${NEXT_NUM}.md" | |
| cd ../.. | |
| cat > blog-repo/_posts/${FILENAME} << EOF | |
| --- | |
| layout: post | |
| title: "[Algorithm] ${{ steps.problem_info.outputs.problem_name }}" | |
| description: "${{ steps.problem_info.outputs.problem_name }} 접근방식" | |
| date: ${DATETIME} | |
| tags: | |
| - Algorithm | |
| EOF | |
| echo "${{ steps.problem_info.outputs.all_categories }}" | while IFS= read -r category; do | |
| if [ ! -z "$category" ]; then | |
| echo " - ${category}" >> blog-repo/_posts/${FILENAME} | |
| fi | |
| done | |
| cat >> blog-repo/_posts/${FILENAME} << EOF | |
| author: choijangwoo | |
| toc: true | |
| published: true | |
| categories: [Algorithm, ${{ steps.problem_info.outputs.first_category }}] | |
| --- | |
| EOF | |
| cat /tmp/problem_readme.md >> blog-repo/_posts/${FILENAME} | |
| echo "✅ Created: ${FILENAME}" | |
| - name: commit to blog repo | |
| if: steps.problem_info.outputs.readme_exists == 'true' | |
| run: | | |
| cd blog-repo | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add . | |
| git diff --staged --quiet || git commit -m "docs: add algorithm problem post" | |
| git pull --rebase --autostash || git pull --no-rebase | |
| git push |