
๐ Reference
โ velog์ github ์ฐ๋ : ๋ฒจ๋ก๊ทธ ๊ธ์ฐ๊ณ ์๋์ฌ๊ธฐ ๐ฑ
โ velog ๊ธ ์์ฑ ์, ์๋์ผ๋ก github์ ์ปค๋ฐํ๊ธฐ
โ Permission denied to github-actions[bot]
๐ public์ผ๋ก ์์ฑ

๐.githubํด๋ ์์ workflows/update_blog.yml ์์ฑ
velog_hub
โโ .github
| โโ workflows
| โโ update_blog.yml
โโ scripts
โโ update_blog.py

update_blog.yml ํ์ผ ์์ฑ ๐ permissions ๊ผญ ์์ฑํด์ฃผ๊ธฐ!! ์ธ์ฆ๋ฌธ์ ํด๊ฒฐ๋ฒ ๋ฐ๋ก๊ฐ๊ธฐ>>>
๋งค์ผ ์์ / push ๋ ๋ ํ์ด์ฌ ์คํฌ๋ฆฝํธ์คํ
name: Update Blog Posts
on:
push:
branches:
- main # ๋๋ ์ํฌํ๋ก์ฐ๋ฅผ ํธ๋ฆฌ๊ฑฐํ๊ณ ์ถ์ ๋ธ๋์น ์ด๋ฆ
schedule:
- cron: "0 0 * * *" # ๋งค์ผ ์์ ์ ์คํ
jobs:
update_blog:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Push changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git push https://<username>${{ secrets.GH_PAT }}@github.com/<username>/velog_hub.git
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.x"
- name: Install dependencies
run: |
pip install feedparser gitpython
- name: Run script
run: python scripts/update_blog.py
# ์ฌ์ฉ์ ์ธ์ฆ
permissions:
contents: write
update_blog.py ํ์ผ ์์ฑimport feedparser
import git
import os
# ๋ฒจ๋ก๊ทธ RSS ํผ๋ URL
# example : rss_url = 'https://api.velog.io/rss/@soozi'
rss_url = 'https://api.velog.io/rss/@<velog_์์ด๋>'
# ๊นํ๋ธ ๋ ํฌ์งํ ๋ฆฌ ๊ฒฝ๋ก
repo_path = '.'
# 'velog-posts' ํด๋ ๊ฒฝ๋ก
posts_dir = os.path.join(repo_path, 'velog-posts')
# 'velog-posts' ํด๋๊ฐ ์๋ค๋ฉด ์์ฑ
if not os.path.exists(posts_dir):
os.makedirs(posts_dir)
# ๋ ํฌ์งํ ๋ฆฌ ๋ก๋
repo = git.Repo(repo_path)
# RSS ํผ๋ ํ์ฑ
feed = feedparser.parse(rss_url)
# ๊ฐ ๊ธ์ ํ์ผ๋ก ์ ์ฅํ๊ณ ์ปค๋ฐ
for entry in feed.entries:
# ํ์ผ ์ด๋ฆ์์ ์ ํจํ์ง ์์ ๋ฌธ์ ์ ๊ฑฐ ๋๋ ๋์ฒด
file_name = entry.title
file_name = file_name.replace('/', '-') # ์ฌ๋์๋ฅผ ๋์๋ก ๋์ฒด
file_name = file_name.replace('\\', '-') # ๋ฐฑ์ฌ๋์๋ฅผ ๋์๋ก ๋์ฒด
# ํ์์ ๋ฐ๋ผ ์ถ๊ฐ ๋ฌธ์ ๋์ฒด
file_name += '.md'
file_path = os.path.join(posts_dir, file_name)
# ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ฑ
if not os.path.exists(file_path):
with open(file_path, 'w', encoding='utf-8') as file:
file.write(entry.description) # ๊ธ ๋ด์ฉ์ ํ์ผ์ ์์ฑ
# ๊นํ๋ธ ์ปค๋ฐ
repo.git.add(file_path)
repo.git.commit('-m', f':sparkles: new post: {entry.title}')
# ๋ณ๊ฒฝ ์ฌํญ์ ๊นํ๋ธ์ ํธ์
repo.git.push()
๐ ๋ฐ์ token ๋ณต์ฌ (๋ฑ ํ ๋ฒ๋ง ๋ณผ ์ ์์ผ๋ ๋ฐ๋์ ๋ณต์ฌ!!!)
github ๊ณ์ ->Settings->Developer Settings
->Personal access tokens (classic)->Generate New Token
->Note์์ฑ ->repo, workflowํด๋ฆญ ->Generate new token
velog_hub->Settings->Actions secrets and variables->Actions->New Repository Secret
โ Name : GH_PAT
โ Secret : [2๋ฒ์์ ๋ฐ๊ธ๋ฐ์ ํ ํฐ]


๐ ๊ถํ ์ธ์ฆํ์ - ์๊ฒฉ ์ ์ฅ์ URL์ ์ฌ๋ฐ๋ฅด๊ฒ ๋ณ๊ฒฝํด์ผํจ!!
$ git remote set-url origin https://
"user-name"@github.com/"user-name"/"repository-name".git

update_blog.yml ํ์ผ์์ github ์ฃผ์ ์์ run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git push https://<USERNAME>${{ secrets.GH_PAT }}@github.com/<USERNAME>/velog_hub.git
๐คข 6-2, 6-3์ ์ํํ ๊ฒฐ๊ณผ -> ์ ๋ ์คํจ์ผ~ ๋ Permission ์๋ฌ

(fix: ์ฌ์ฉ์ ์ธ์ฆ ์ปค๋ฐ์ ๊ทธ๋ฅ ์ฃผ์๋ง ์ถ๊ฐํ ๊ฒ)
remote: Permission to USERNAME/velog_hub.git denied to github-actions[bot]
๐
github-actions[bot]์ดUSERNAME/velog_hub.git์ ์ ๊ทผํ ๊ถํ์ด ์๋ค!!
=> ๊ทธ๋ ๋ค๋ฉดgithub-actions[bot]์ด ์ ๊ทผํ ๊ถํ์ ์ค์ ํด์ค์ผ๊ฒ ๋ค?
๐ Settings -> Actions -> General ์์ Workflow Permissions ์ค์ ํ๊ธฐ


๐ update_blog.yml ํ์ผ์ permissions ์์ฑ ์ถ๊ฐํ๊ธฐ
jobs:
update_blog:
...
# ์๋ ๋ด์ฉ ์ถ๊ฐ!!!
permissions:
contents: write
contents: write: ์ํฌํ๋ก์ฐ๊ฐ ๋ฆฌํฌ์งํ ๋ฆฌ์ ์ฝํ ์ธ ๋ฅผ ์์ ํ๊ณ ์ปค๋ฐ/ํธ์ํ ์ ์๋ ๊ถํ์ ๊ฐ์ง๊ฒ ๋จ!!
Workflow Permissions ์ค์ | permissions ์์ฑ ์ถ๊ฐ |
|---|---|
| ๋ ํฌ์งํ ๋ฆฌ์ ๋ชจ๋ ์ํฌํ๋ก์ฐ์ ๋ํด ์ผ๊ด์ ์ผ๋ก ์ ์ฉ | ์ํฌํ๋ก์ฐ ํ์ผ ๋ด์์ ๊ฐ ์ํฌํ๋ก์ฐ์ ๋ํด ํ์ํ ์ต์ํ์ ๊ถํ์ ๊ฐ๋ณ์ ์ผ๋ก ์ค์ . ๋ฐ๋ผ์ ํ์ํ์ง ์์ ๊ถํ์ ๋ถ์ฌํ์ง ์์์ผ๋ก์จ ๋ณด์ ๊ฐํ |

๋ค์ํฌ์คํ ์ ๊ณ์...