跳转至

Gitea Actions 镜像配置

问题背景

Gitea Actions 执行时需要从 GitHub 拉取代码或依赖,可能遇到以下问题:

  • 网络不通或速度慢
  • GitHub 访问限制
  • 频繁拉取导致 IP 被限流

解决方案概览

方案 难度 适用场景
Gitea 仓库镜像 ⭐ 简单 所有场景,推荐
Actions 缓存 ⭐ 简单 加速依赖下载
本地 Actions 仓库 ⭐⭐ 中等 完全离线环境
Git 代理服务 ⭐⭐⭐ 复杂 大规模部署

方案一:Gitea 仓库镜像 ⭐ 推荐

原理

Gitea 内置仓库镜像功能,自动从 GitHub 同步代码到本地 Gitea,Actions 使用本地副本。

Web 界面配置

  1. 新建镜像仓库
Gitea Web → 右上角 + → 新建迁移
  1. 填写配置
配置项 说明
仓库来源 选择 GitHub
仓库地址 https://github.com/用户名/项目名.git
认证 (私有仓库需要)GitHub Token
镜像方向 Pull(从 GitHub 拉取)
同步间隔 建议 1-8 小时
仓库名称 自定义,如 mirrored-xxx
  1. 查看镜像状态
仓库页面 → 设置 → 仓库镜像

可以看到最后同步时间和状态。

API 批量导入

批量创建镜像仓库的脚本:

#!/bin/bash
# scripts/mirror-github-repos.sh

repos=(
  "actions/checkout"
  "actions/setup-go"
  "actions/setup-node"
  "actions/setup-python"
  "actions/cache"
  "actions/upload-artifact"
)

GITEA_URL="https://your-gitea.com"
GITEA_TOKEN="your_gitea_token"
MIRROR_USER="your_username"

for repo in "${repos[@]}"; do
  repo_name=$(echo $repo | sed 's/\//-/g')
  echo "Mirroring $repo to $repo_name"

  curl -X POST "$GITEA_URL/api/v1/repos/migrate" \
    -H "Authorization: token $GITEA_TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
      \"clone_addr\": \"https://github.com/$repo.git\",
      \"repo_name\": \"$repo_name\",
      \"uid\": $MIRROR_USER,
      \"mirror\": true,
      \"private\": false,
      \"auth_username\": \"\",
      \"auth_password\": \"\",
      \"mirror_interval\": \"8h\"
    }"

  echo "✓ $repo mirrored"
  sleep 2
done

Actions 中使用镜像仓库

name: Test

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # 原来的方式(从 GitHub 拉取)
      # - uses: actions/checkout@v4

      # 改用本地镜像仓库
      - uses: gitea_username/actions-checkout@v4
        # 或者使用完整的仓库路径
      - uses: ./.gitea/actions/checkout

方案二:Actions 缓存配置

缓存依赖包

加速依赖下载,减少重复拉取:

name: Build

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # 缓存 Go Modules
      - name: Cache Go modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      # 缓存 Node Modules
      - name: Cache Node modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.npm
            node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      # 缓存 Docker 镜像
      - name: Cache Docker layers
        uses: actions/cache@v3
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

Gitea Actions 内置缓存

Gitea 1.19+ 支持 Actions 缓存:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # 使用内置缓存
      - name: Restore cache
        uses: https://gitea.com/actions/cache/restore@v1
        with:
          path: path/to/cache
          key: ${{ runner.os }}-${{ github.sha }}

      # ... 构建步骤 ...

      - name: Save cache
        uses: https://gitea.com/actions/cache/save@v1
        with:
          path: path/to/cache
          key: ${{ runner.os }}-${{ github.sha }}

方案三:本地 Actions 仓库

原理

将常用 GitHub Actions 下载到本地仓库,完全离线使用。

预下载脚本

#!/bin/bash
# scripts/download-actions.sh

ACTION_DIR=".gitea/actions"
mkdir -p "$ACTION_DIR"

actions=(
  "https://github.com/actions/checkout"
  "https://github.com/actions/setup-go"
  "https://github.com/actions/setup-node"
  "https://github.com/actions/setup-python"
  "https://github.com/actions/cache"
  "https://github.com/actions/upload-artifact"
  "https://github.com/actions/download-artifact"
)

for url in "${actions[@]}"; do
  name=$(basename "$url")
  echo "Downloading $name..."

  if [ ! -d "$ACTION_DIR/$name" ]; then
    git clone --depth 1 "$url" "$ACTION_DIR/$name"
  else
    cd "$ACTION_DIR/$name" && git pull
  fi

  echo "✓ $name downloaded"
done

echo "All actions downloaded to $ACTION_DIR"

Actions 中使用本地仓库

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # 使用本地 Actions 仓库
      - uses: ./.gitea/actions/checkout
        with:
          fetch-depth: 0

      - uses: ./.gitea/actions/setup-go
        with:
          go-version: '1.21'

定期更新脚本

#!/bin/bash
# scripts/update-actions.sh

ACTION_DIR=".gitea/actions"

for dir in "$ACTION_DIR"/*; do
  if [ -d "$dir/.git" ]; then
    echo "Updating $(basename $dir)..."
    cd "$dir" && git pull origin main
  fi
done

echo "All actions updated"

添加到 crontab 定期执行:

# 每周更新一次
0 0 * * 0 /path/to/update-actions.sh >> /var/log/actions-update.log 2>&1

方案四:Git 代理服务

git-daemon(轻量级)

适合内网环境,提供 Git 协议访问:

# 安装
sudo apt install git-daemon-sysvinit

# 创建镜像目录
sudo mkdir -p /srv/git
cd /srv/git

# 镜像常用仓库
git clone --mirror https://github.com/actions/checkout.git
git clone --mirror https://github.com/actions/setup-go.git

# 启动守护进程
git daemon --base-path=/srv/git --export-all --reuseaddr --enable=receive-pack --detach

# 定期更新镜像
cat > /etc/cron.d/git-mirror << EOF
# 每小时更新一次
0 * * * * root cd /srv/git/actions/checkout.git && git fetch --all
0 * * * * root cd /srv/git/actions/setup-go.git && git fetch --all
EOF

使用方式:

# 使用本地 Git 代理
git clone git://your-server/checkout.git

推荐组合方案

根据不同场景选择合适的组合:

个人/小团队

┌─────────────────────────────────────────────────┐
│  1. Gitea 仓库镜像(主要)                       │
│     - 常用 GitHub 项目 → Gitea 内部仓库         │
│     - 定时自动同步(如每 8 小时)                │
├─────────────────────────────────────────────────┤
│  2. Actions 缓存(辅助)                         │
│     - 依赖包缓存                                │
│     - 构建产物缓存                              │
└─────────────────────────────────────────────────┘

企业/离线环境

┌─────────────────────────────────────────────────┐
│  1. 本地 Actions 仓库                            │
│     - 完全离线,定期更新                         │
├─────────────────────────────────────────────────┤
│  2. Git 代理服务                                 │
│     - 内网 Git 协议访问                         │
├─────────────────────────────────────────────────┤
│  3. 依赖包镜像(如 npm、go)                     │
│     - 私有 npm registry                         │
│     - Go proxy                                  │
└─────────────────────────────────────────────────┘

故障排查

镜像同步失败

# 检查 Gitea 日志
journalctl -u gitea -f

# 手动测试同步
cd /path/to/gitea/repositories/mirrored-repo.git
git fetch --all --verbose

Actions 仍从 GitHub 拉取

检查 workflow 文件:

# 确保使用的是本地路径或本地镜像仓库
# 错误示例
- uses: actions/checkout@v4  # 仍然从 GitHub

# 正确示例
- uses: gitea_username/actions-checkout@v4
# 或
- uses: ./.gitea/actions/checkout

缓存未生效

# 确保缓存 key 正确
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

# 检查缓存路径是否存在
- name: Check cache
  run: ls -la ~/.cache/go-build

相关文档