Run Article Data To Yml Script #2900
This file contains 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: Run Article Data To Yml Script | |
on: | |
schedule: | |
- cron: '*/5 * * * *' # 每5分钟运行一次 | |
workflow_dispatch: # 允许手动触发 | |
jobs: | |
run-script: | |
runs-on: ubuntu-latest # 选择 GitHub 提供的运行环境 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
# 设置 Python 版本 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.8' | |
# 安装依赖包 | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
working-directory: ./script | |
# 运行 Python 脚本 | |
- name: Run Python script | |
run: | | |
python article_data_to_yml.py | |
env: | |
ARTICLE_API: ${{ secrets.ARTICLE_API }} | |
ARTICLE_TOKEN: ${{ secrets.ARTICLE_TOKEN }} | |
working-directory: ./script | |
# 在运行脚本后,列出 data 目录中的所有文件 | |
- name: List all files in data directory | |
run: | | |
echo "Listing files in data directory:" | |
ls -l | |
working-directory: ./data | |
# 在 articles.yml 有更新时,提交并推送更改,使用 GitHub Token 进行认证 | |
- name: Commit updated article.yml and push to repository | |
run: | | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git config --local user.name "github-actions[bot]" | |
git add data/articles.yml | |
if git diff-index --quiet HEAD; then | |
echo "No changes to commit" | |
else | |
echo "Need update article.yml" | |
export TZ="Asia/Shanghai" | |
current_time=$(date '+%Y-%m-%d %H:%M:%S') | |
git commit -m "Auto update article at ${current_time}" | |
git push origin HEAD:main | |
curl -X POST \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
--data '{"event_type": "update-article", "client_payload": {"message": "Article YML file has been updated"}}' \ | |
https://api.github.com/repos/${{ github.repository }}/dispatches | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |