build(ci): 更新自动更新脚本和配置,使用 Python 处理文件下载
This commit is contained in:
@@ -15,40 +15,17 @@ stages:
|
|||||||
auto_update:
|
auto_update:
|
||||||
# 需要配置 REPO_TOKEN 和 THE_REPO
|
# 需要配置 REPO_TOKEN 和 THE_REPO
|
||||||
stage: auto-update
|
stage: auto-update
|
||||||
image: ubuntu:latest
|
image: python:3.9
|
||||||
rules:
|
rules:
|
||||||
- if: $CI_PIPELINE_SOURCE == "schedule" # 通过定时任务触发
|
- if: $CI_PIPELINE_SOURCE == "schedule" # 通过定时任务触发
|
||||||
before_script:
|
before_script:
|
||||||
# 配置阿里云镜像源
|
# 创建 pip 配置目录并写入阿里云镜像配置
|
||||||
- sed -i 's|http://archive.ubuntu.com/ubuntu/|http://mirrors.aliyun.com/ubuntu/|g' /etc/apt/sources.list
|
- mkdir -p ~/.pip
|
||||||
- apt-get update
|
- echo "[global]" > ~/.pip/pip.conf
|
||||||
- apt-get install -y git openssh-client curl yq
|
- echo "index-url = https://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf
|
||||||
script: |
|
- echo "trusted-host = mirrors.aliyun.com" >> ~/.pip/pip.conf
|
||||||
echo "🛠️ 配置Git身份"
|
script:
|
||||||
git config --global user.name "CI/CD"
|
# 安装 PyYAML 用于解析 list.yaml
|
||||||
git config --global user.email "cicd@sugarscat.cn"
|
- pip install pyyaml
|
||||||
echo "🔑 配置Git认证"
|
# 运行更新脚本
|
||||||
git remote remove origin || true
|
- python update_files.py
|
||||||
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
|
|
||||||
25
list.yaml
25
list.yaml
@@ -1,14 +1,13 @@
|
|||||||
# 需要自动更新的文件列表
|
# 需要自动更新的文件列表
|
||||||
list:
|
- file: /docker/install.sh
|
||||||
- file: /docker/install.sh
|
source: https://raw.githubusercontent.com/docker/docker-install/master/install.sh
|
||||||
source: https://raw.githubusercontent.com/docker/docker-install/master/install.sh
|
- file: /docker/debian/gpg
|
||||||
- file: /docker/debian/gpg
|
source: https://download.docker.com/linux/debian/gpg
|
||||||
source: https://download.docker.com/linux/debian/gpg
|
- file: /docker/ubuntu/gpg
|
||||||
- file: /docker/ubuntu/gpg
|
source: https://download.docker.com/linux/ubuntu/gpg
|
||||||
source: https://download.docker.com/linux/ubuntu/gpg
|
- file: /docker/raspbian/gpg
|
||||||
- file: /docker/raspbian/gpg
|
source: https://download.docker.com/linux/raspbian/gpg
|
||||||
source: https://download.docker.com/linux/raspbian/gpg
|
- file: /docker/fedora/docker-ce.repo
|
||||||
- file: /docker/fedora/docker-ce.repo
|
source: https://download.docker.com/linux/fedora/docker-ce.repo
|
||||||
source: https://download.docker.com/linux/fedora/docker-ce.repo
|
- file: /docker/centos/docker-ce.repo
|
||||||
- file: /docker/centos/docker-ce.repo
|
source: https://download.docker.com/linux/centos/docker-ce.repo
|
||||||
source: https://download.docker.com/linux/centos/docker-ce.repo
|
|
||||||
77
update_files.py
Normal file
77
update_files.py
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user