Run API and Auto-commit #791
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: Run API and Auto-commit | |
| on: | |
| schedule: | |
| # 每2小时运行一次,使用UTC时间 | |
| - cron: '0 */2 * * *' | |
| # 允许手动触发 | |
| workflow_dispatch: | |
| jobs: | |
| run-api-and-commit: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # 需要写入权限来推送更改 | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # 获取所有历史记录,方便后续操作 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.x' | |
| - name: Install dependencies (如果有requirements.txt) | |
| run: | | |
| if [ -f "requirements.txt" ]; then | |
| pip install -r requirements.txt | |
| fi | |
| - name: Run API script | |
| run: python3 api.py | |
| - name: Check for changes | |
| id: check_changes | |
| run: | | |
| # 检查是否有文件被修改 | |
| if git diff --quiet; then | |
| echo "has_changes=false" >> $GITHUB_OUTPUT | |
| echo "没有检测到更改" | |
| else | |
| echo "has_changes=true" >> $GITHUB_OUTPUT | |
| echo "检测到更改" | |
| fi | |
| - name: Configure Git | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| run: | | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| - name: Add and commit changes | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| run: | | |
| git add -A | |
| git commit -m "自动更新: $(date +'%Y-%m-%d %H:%M:%S UTC')" || echo "没有需要提交的更改" | |
| - name: Push changes | |
| if: steps.check_changes.outputs.has_changes == 'true' | |
| uses: ad-m/github-push-action@master | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| branch: ${{ github.ref_name }} |