Files
ZFScoresCheck/.github/workflows/main.yml
T
2024-02-15 12:08:49 +08:00

202 lines
6.9 KiB
YAML

name: CheckScores
run-name: ${{ github.workflow }} ${{ github.sha }}
on:
schedule:
- cron: "*/30 * * * *" # 每30分钟自动执行一次
workflow_dispatch: # 可以手动执行
inputs:
force_push_message:
description: "是否强制推送信息"
required: true
default: "False"
type: choice
options:
- "True"
- "False"
jobs:
SyncFork:
name: SyncFork
# 在最新版的Ubuntu系统上运行
runs-on: ubuntu-latest
# 定义作业SyncFork的输出变量
outputs:
MAIN_YML_FILES_INCONSISTENT: ${{ steps.compare_main_yml_files.outputs.MAIN_YML_FILES_INCONSISTENT }}
steps:
# 使用GitHub Actions提供的动作来检出代码库
- name: Checkout Repository
uses: actions/checkout@main
# 配置Git用户信息
- name: Configure Git
run: |
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
# 添加上游仓库作为远程仓库,并获取上游仓库的main分支的最新更改
- name: Fetch upstream changes
run: |
REPO_NAME="NianBroken/ZFCheckScores"
REPO_URL="https://github.com/$REPO_NAME.git"
echo "Repo Url: $REPO_URL"
git remote add upstream "$REPO_URL"
git fetch upstream main
# 对比当前分支的main.yml文件与上游分支的main.yml文件是否一致
- name: Compare main.yml files
id: compare_main_yml_files
run: |
if cmp -s .github/workflows/main.yml <(git show upstream/main:.github/workflows/main.yml); then
echo "main.yml file is consistent"
echo "MAIN_YML_FILES_INCONSISTENT=False" >> $GITHUB_OUTPUT
else
echo "main.yml file is inconsistent"
echo "MAIN_YML_FILES_INCONSISTENT=True" >> $GITHUB_OUTPUT
echo "::error title=main.yml file is inconsistent!::"\
"The contents of your current branch's 'main.yml' file "\
"do not match the contents of your upstream branch's "\
"'main.yml' file! You need to synchronize the upstream "\
"branch manually!"
fi
# 定义备份和还原的文件后缀列表
- name: Set file extensions
run: |
# 以空格分隔多个文件后缀
echo "FILE_EXTENSIONS=.txt .yml" >> $GITHUB_ENV
# 创建一个备份目录,并将当前目录及子目录中的所有指定后缀文件复制到备份目录,并保持路径
- name: Backup local files with specified extensions
run: |
mkdir -p backup
for EXT in $FILE_EXTENSIONS; do
rsync -av --include="*/" --include="*$EXT" --exclude="*" ./ backup/
done
# 列出备份文件夹的文件夹结构
- name: List Backup Folder Structure
run: |
echo "Backup Folder Structure:"
tree -a backup/
# 将上游main分支的更改强制推送到当前main分支
- name: Force push changes from upstream to current main branch
run: |
git checkout main
git reset --hard upstream/main
# 从备份目录将指定后缀文件还原到当前目录及子目录中,然后删除备份目录
- name: Restore local files with specified extensions and delete backup
run: |
for EXT in $FILE_EXTENSIONS; do
rsync -av --include="*/" --include="*$EXT" --exclude="*" backup/ ./
done
rm -rf backup
# 将更改强制推送到main分支
- name: Force push changes to main branch
run: |
git add .
git commit -m "Update branch" || echo "This branch is not behind the upstream"
git push origin main --force
CheckScores:
name: CheckScores
# SyncFork执行完成后运行
needs: SyncFork
# 在最新版的Ubuntu系统上运行
runs-on: ubuntu-latest
steps:
# 使用GitHub Actions提供的动作来检出代码库
- name: Checkout Repository
uses: actions/checkout@main
# 配置Python环境
- name: Set up Python
uses: actions/setup-python@main
with:
python-version: '*'
# 使用pip安装项目的依赖项
- name: Install dependencies
run: |
pip install requests rsa pyquery
# 配置Git用户信息
- name: Configure Git
run: |
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
# 运行主程序main.py
- name: Run main.py
env:
FORCE_PUSH_MESSAGE: ${{ github.event.inputs.force_push_message }}
MAIN_YML_FILES_INCONSISTENT: ${{ needs.SyncFork.outputs.MAIN_YML_FILES_INCONSISTENT }}
URL: ${{ secrets.URL }}
USERNAME: ${{ secrets.USERNAME }}
PASSWORD: ${{ secrets.PASSWORD }}
TOKEN: ${{ secrets.TOKEN }}
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_TRIGGERING_ACTOR: ${{ github.triggering_actor }}
REPOSITORY_NAME: ${{ github.repository }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_WORKFLOW: ${{ github.workflow }}
GITHUB_RUN_NUMBER: ${{ github.run_number }}
GITHUB_RUN_ID: ${{ github.run_id }}
run: |
if [ -z "${{ github.event.inputs.force_push_message }}" ]; then
export FORCE_PUSH_MESSAGE="False"
else
export FORCE_PUSH_MESSAGE="${{ github.event.inputs.force_push_message }}"
fi
export BEIJING_TIME="$(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M:%S:%3N')"
python main.py
echo "------"
# 判断环境变量是否为空
if [ -z "$URL" ]; then
echo "URL secret is empty!"
fi
if [ -z "$USERNAME" ]; then
echo "USERNAME secret is empty!"
fi
if [ -z "$PASSWORD" ]; then
echo "PASSWORD secret is empty!"
fi
if [ -z "$TOKEN" ]; then
echo "TOKEN secret is empty!"
fi
# 方便通过截图快速定位到用户
echo "Force Push Message: $FORCE_PUSH_MESSAGE"
echo "main.yml Files Inconsistent: $MAIN_YML_FILES_INCONSISTENT"
echo "Triggered By: $GITHUB_EVENT_NAME"
echo "Run By: $GITHUB_TRIGGERING_ACTOR"
echo "Repository Name: $REPOSITORY_NAME"
echo "Commit SHA: $GITHUB_SHA"
echo "Workflow Name: $GITHUB_WORKFLOW"
echo "Workflow Number: $GITHUB_RUN_NUMBER"
echo "Workflow ID: $GITHUB_RUN_ID"
echo "Beijing Time: $BEIJING_TIME"
# 删除__pycache__文件夹
- name: Delete __pycache__ folder
run: |
rm -rf __pycache__
# 将更改强制推送到main分支
- name: Force push changes to main branch
run: |
git add .
git commit -m "Update from GitHub Actions" || true
git push origin main --force