백준이나 프로그래머스 문제를 풀면서, 내 코드들을 따로 깃허브에 올릴려고 하니 손이 너무 많이 가서 시간낭비가 컸다.
그래서 이번기회에 깃허브 업로드를 자동화 한번 해보자! 싶어서 찾아봤더니
백준허브라는 좋은 플러그인이 있길래 사용하기로 했다.
플러그인을 등록했으면 그전에 풀었던 문제들을 옮겨보자!
나는 기존에 쓰던 repository가 있었지만, 이왕 하는거 새로 만들어서 했기에 전부 재업로드 해줘야 했다.
다음과 같이 우측 상단에 전체제출 업로드라는 버튼이 있을 것이다.
이 버튼을 클릭하면 어느정도 시간이 걸리지만 전부 깃허브로 옮겨 갈 수 있다.
프로그래머스는 아마도 풀었던 문제들을 다시 재제출 해야 깃허브에 업로드 되는 것 같다.
깃허브에서 내가 풀었던 문제들을 바로 확인할 수 있도록 메인 README파일에서 링크를 걸려고 한다.
내가 참고했던 블로그에서는 github actions를 먼저 생성했던것 같은데, workflow를 먼저 생성 후 upload 파일을 만드니 예상치 못한 에러가 생겨서 조금 헤멨다.
그래서 먼저 upload.py 를 생성해주도록 하자.
upload.py 파일은 readme.md 파일과 같은 경로상에서 생성하면 된다.
#!/usr/bin/env python
import os
from urllib import parse
HEADER="""#
# 백준, 프로그래머스 문제 풀이 목록
"""
def main():
content = ""
content += HEADER
directories = []
solveds = []
for root, dirs, files in os.walk("."):
dirs.sort()
if root == '.':
for dir in ('.git', '.github'):
try:
dirs.remove(dir)
except ValueError:
pass
continue
category = os.path.basename(root)
if category == 'images':
continue
directory = os.path.basename(os.path.dirname(root))
if directory == '.':
continue
if directory not in directories:
if directory in ["백준", "프로그래머스"]:
content += "## 📚 {}\n".format(directory)
else:
content += "### 🚀 {}\n".format(directory)
content += "| 문제번호 | 링크 |\n"
content += "| ----- | ----- |\n"
directories.append(directory)
for file in files:
if category not in solveds:
content += "|{}|[링크]({})|\n".format(category, parse.quote(os.path.join(root, file)))
solveds.append(category)
with open("README.md", "w") as fd:
fd.write(content)
if __name__ == "__main__":
main()
action 탭에서 python을 검색 후 왼쪽 맨 하단의 python package를 클릭한다.
파일 제목은 자유로 하고 코드는 다음과 같이 작성한다.
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
name: Update README
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install python-leetcode
- name: Run update.py
run: |
python update.py
- name: Commit changes
run: |
git config --global user.name 'github nickname'
git config --global user.email 'github email'
git add -A
git commit -am "auto update README.md"
- name: Push changes
run: |
git push
작성 후 커밋을 완료하면 action탭에서 빌드가 진행이 된다.
실패라고 뜨는데 이유는 실시간으로 계속 돌아가기 때문에 커밋할 내용이 없으면 다음과 같이 실패가 뜨니까 걱정하지 않아도 된다.
README를 확인해보면 다음과 같이 잘 업데이트 된 것을 확인할 수 있다.