diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 63f2950..41da2d3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -15,40 +15,17 @@ stages: auto_update: # 需要配置 REPO_TOKEN 和 THE_REPO stage: auto-update - image: ubuntu:latest + image: python:3.9 rules: - if: $CI_PIPELINE_SOURCE == "schedule" # 通过定时任务触发 before_script: - # 配置阿里云镜像源 - - sed -i 's|http://archive.ubuntu.com/ubuntu/|http://mirrors.aliyun.com/ubuntu/|g' /etc/apt/sources.list - - apt-get update - - apt-get install -y git openssh-client curl yq - script: | - echo "🛠️ 配置Git身份" - git config --global user.name "CI/CD" - git config --global user.email "cicd@sugarscat.cn" - echo "🔑 配置Git认证" - git remote remove origin || true - git remote add origin "https://oauth2:${REPO_TOKEN}@${THE_REPO}" - git fetch origin - git checkout -B main - - echo "🔁 Starting auto update..." - set +e # 忽略后续命令的错误 - if [ -f "list.yaml" ]; then - for item in $(yq e '.list[]' list.yaml); do - file=$(echo $item | yq e '.file' -) - source=$(echo $item | yq e '.source' -) - echo "Downloading $source to $file" - curl -o "$file" "$source" || echo "❌️ Failed to download $source" - done - else - echo "❌ list.yaml file not found!" - fi - set -e # 恢复错误处理 - echo "Auto update completed." - - echo "🚀 推送回源仓库" - git add . - git commit -m "Auto update files" || echo "No changes to commit" - git push origin main \ No newline at end of file + # 创建 pip 配置目录并写入阿里云镜像配置 + - mkdir -p ~/.pip + - echo "[global]" > ~/.pip/pip.conf + - echo "index-url = https://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf + - echo "trusted-host = mirrors.aliyun.com" >> ~/.pip/pip.conf + script: + # 安装 PyYAML 用于解析 list.yaml + - pip install pyyaml + # 运行更新脚本 + - python update_files.py \ No newline at end of file diff --git a/list.yaml b/list.yaml index f5866e1..51dedd9 100644 --- a/list.yaml +++ b/list.yaml @@ -1,14 +1,13 @@ # 需要自动更新的文件列表 -list: - - file: /docker/install.sh - source: https://raw.githubusercontent.com/docker/docker-install/master/install.sh - - file: /docker/debian/gpg - source: https://download.docker.com/linux/debian/gpg - - file: /docker/ubuntu/gpg - source: https://download.docker.com/linux/ubuntu/gpg - - file: /docker/raspbian/gpg - source: https://download.docker.com/linux/raspbian/gpg - - file: /docker/fedora/docker-ce.repo - source: https://download.docker.com/linux/fedora/docker-ce.repo - - file: /docker/centos/docker-ce.repo - source: https://download.docker.com/linux/centos/docker-ce.repo \ No newline at end of file +- file: /docker/install.sh + source: https://raw.githubusercontent.com/docker/docker-install/master/install.sh +- file: /docker/debian/gpg + source: https://download.docker.com/linux/debian/gpg +- file: /docker/ubuntu/gpg + source: https://download.docker.com/linux/ubuntu/gpg +- file: /docker/raspbian/gpg + source: https://download.docker.com/linux/raspbian/gpg +- file: /docker/fedora/docker-ce.repo + source: https://download.docker.com/linux/fedora/docker-ce.repo +- file: /docker/centos/docker-ce.repo + source: https://download.docker.com/linux/centos/docker-ce.repo \ No newline at end of file diff --git a/update_files.py b/update_files.py new file mode 100644 index 0000000..09ce487 --- /dev/null +++ b/update_files.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +import os +import yaml +import subprocess +import sys + +def download_file(url): + """使用 curl 下载文件内容""" + result = subprocess.run(["curl", "-s", url], capture_output=True, text=True) + if result.returncode != 0: + print(f"下载 {url} 时出错,跳过。") + return None + return result.stdout + +def main(): + # 读取 list.yaml + try: + with open("list.yaml", "r", encoding="utf-8") as f: + items = yaml.safe_load(f) + except FileNotFoundError: + print("list.yaml 文件不存在。") + sys.exit(0) + + modified = False + + for item in items: + file_path = item.get("file") + source = item.get("source") + if not file_path or not source: + print("条目缺少 file 或 source 字段,跳过。") + continue + + print(f"处理 {file_path} 从 {source}") + new_content = download_file(source) + if new_content is None: + # 如果下载失败,继续处理下一个文件 + continue + + # 去掉前导斜杠(如果希望相对于仓库根目录存放,也可以自行调整目录结构) + rel_path = file_path.lstrip("/") + # 创建必要目录 + os.makedirs(os.path.dirname(rel_path), exist_ok=True) + + # 如果文件存在,检查内容是否已更新 + if os.path.exists(rel_path): + with open(rel_path, "r", encoding="utf-8") as f: + old_content = f.read() + if old_content == new_content: + print(f"{rel_path} 内容未改变。") + continue + + with open(rel_path, "w", encoding="utf-8") as f: + f.write(new_content) + print(f"更新了 {rel_path}") + modified = True + + # 如果有修改则提交到仓库 + if modified: + # 配置 git 用户信息 + subprocess.run(["git", "config", "--global", "user.email", "cicd@sugarscat.cn"], check=True) + subprocess.run(["git", "config", "--global", "user.name", "CI/CD"], check=True) + + repo_url = os.environ.get("THE_REPO") + # 将 https:// 替换为 https://oauth2:token@ 形式 + token = os.environ.get("REPO_TOKEN") + new_repo_url = repo_url.replace("https://", f"https://oauth2:{token}@") + subprocess.run(["git", "remote", "set-url", "origin", new_repo_url], check=True) + subprocess.run(["git", "add", "."], check=True) + commit_message = "自动更新:从 list.yaml 下载最新文件" + # 如果没有变更,则 git commit 会返回非零状态,可用 || true 处理 + subprocess.run(["git", "commit", "-m", commit_message], check=False) + subprocess.run(["git", "push", "origin", "HEAD"], check=True) + else: + print("没有需要更新的文件。") + +if __name__ == "__main__": + main()