Github的Actions定时任务
通过Github的Actions创建定时执行脚本,实现文件同步下载及长期副本保留。




示例代码:
name: Download and Archive Wallpapers
on:
schedule:
- cron: '12 7 * * *' #北京时间 15:12
- cron: '12 16 * * *' #北京时间 00:12(次日)
- cron: '12 0 * * *' #北京时间 08:12
workflow_dispatch: # 允许手动触发
jobs:
download-and-archive:
runs-on: ubuntu-latest
permissions:
contents: write # 授予写入权限
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: true # 保持推送凭证
- name: Create archive directories
run: |
mkdir -p archive/cn
mkdir -p archive/en
- name: Download Chinese version
run: |
# 下载中文版文件
curl -s -o bing-wallpaper-cn.md https://raw.githubusercontent.com/niumoo/bing-wallpaper/refs/heads/main/zh-cn/bing-wallpaper.md
# 创建带日期的中文版副本
TODAY=$(date +'%Y-%m-%d')
cp bing-wallpaper-cn.md "archive/cn/bing-wallpaper-cn-$TODAY.md"
- name: Download English version
run: |
# 下载英文版文件
curl -s -o bing-wallpaper-en.md https://raw.githubusercontent.com/niumoo/bing-wallpaper/refs/heads/main/bing-wallpaper.md
# 创建带日期的英文版副本
TODAY=$(date +'%Y-%m-%d')
cp bing-wallpaper-en.md "archive/en/bing-wallpaper-en-$TODAY.md"
- name: Clean up old archives
run: |
# 删除180天前的中文归档
find archive/cn/ -name "bing-wallpaper-cn-*.md" -type f -mtime +180 -delete
# 删除180天前的英文归档
find archive/en/ -name "bing-wallpaper-en-*.md" -type f -mtime +180 -delete
- name: Commit and push changes
env:
GH_TOKEN: ${{ secrets.PAT }} # 需在项目设置中添加名PAT的TOKEN
run: |
git config --local user.email "your account mail address" # 需改成自己的
git config --local user.name "GitHub Actions Bot"
git add bing-wallpaper-*.md archive/
git commit -m "Auto-update: Latest wallpapers (CN+EN) + Archives" || echo "No changes to commit"
git push https://${{ env.GH_TOKEN }}@github.com/yourname/yourproject.git # 需改成自己的用户名及项目名
