提交工单

香港服务器Gitea Actions完整配置:内置CI/CD无需额外部署Woodpecker

文档更新于

前面部署了 Woodpecker CI 作为独立的 CI/CD 服务。但从 Gitea 1.19 开始,Gitea 内置了 Gitea Actions——与 GitHub Actions 完全兼容的 CI/CD 系统,只需注册一个 act Runner,无需额外部署 Woodpecker,现有的 GitHub Actions 工作流文件直接在 Gitea 上运行。


一、启用 Gitea Actions

<code"># /etc/gitea/app.ini
[actions]
ENABLED = true   # 启用 Actions 功能

# 重启 Gitea
systemctl restart gitea

# 在仓库设置中启用:
# 仓库 → Settings → General → Features → Gitea Actions ✅

二、安装 act Runner

<code"># 在执行 CI 任务的服务器上安装 act Runner
# act Runner 是执行 Actions 工作流的代理

# 下载 act Runner
wget https://gitea.com/gitea/act_runner/releases/download/v0.2.11/act_runner-0.2.11-linux-amd64
chmod +x act_runner-* && mv act_runner-* /usr/local/bin/act_runner

# 注册 Runner(在 Gitea 管理后台获取注册 Token)
# 路径:Gitea 管理后台 → Actions → Runners → Create new Runner
act_runner register \
  --no-interactive \
  --instance https://git.yourdomain.com \
  --token YOUR_RUNNER_TOKEN \
  --name hk-runner \
  --labels ubuntu-latest:docker://node:18-bullseye

# 创建 systemd 服务
cat > /etc/systemd/system/act_runner.service << 'EOF'
[Unit]
Description=Gitea act Runner
After=docker.service

[Service]
WorkingDirectory=/opt/act_runner
ExecStart=/usr/local/bin/act_runner daemon
Restart=always
User=root

[Install]
WantedBy=multi-user.target
EOF

systemctl enable --now act_runner

三、工作流文件(GitHub Actions 兼容语法)

<code"># .gitea/workflows/ci.yml(注意是 .gitea/ 目录,不是 .github/)
# 语法与 GitHub Actions 完全相同

name: CI/CD Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest    # 对应 Runner 的 label
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt

      - name: Run tests
        run: pytest tests/ -v

  build-and-deploy:
    needs: test   # 依赖 test job 成功
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: |
          docker build -t registry.yourdomain.com/myapp:${{ gitea.sha }} .

      - name: Login to Harbor
        run: |
          echo "${{ secrets.HARBOR_PASSWORD }}" | docker login \
            registry.yourdomain.com \
            -u ${{ secrets.HARBOR_USER }} \
            --password-stdin

      - name: Push image
        run: docker push registry.yourdomain.com/myapp:${{ gitea.sha }}

      - name: Deploy
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ secrets.PROD_HOST }}
          username: deploy
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            docker pull registry.yourdomain.com/myapp:${{ gitea.sha }}
            docker stop myapp || true && docker rm myapp || true
            docker run -d --name myapp \
              -p 127.0.0.1:8000:8000 \
              --env-file /opt/myapp/.env \
              registry.yourdomain.com/myapp:${{ gitea.sha }}
            curl -sf http://localhost:8000/health

四、Gitea Actions vs Woodpecker CI 选型

维度 Gitea Actions Woodpecker CI
GitHub Actions 兼容 ✅ 完全兼容 ❌(Drone 语法)
额外部署 无需(内置) 需要单独部署
Runner 资源隔离 基于 Docker 基于 Docker
矩阵构建 ✅ ✅
缓存支持 ✅(actions/cache) 需要手动配置
市场(预制 Action) ✅(兼容 GitHub Marketplace) 有限
适合场景 已有 GitHub Actions 经验的团队 习惯 Drone 语法的团队

五、从 GitHub Actions 迁移

<code">
迁移步骤(通常5分钟内完成):

1. 将 .github/workflows/ 目录复制为 .gitea/workflows/

2. 替换以下差异:
   ${{ github.sha }} → ${{ gitea.sha }}
   ${{ github.ref }} → ${{ gitea.ref }}
   ${{ github.actor }} → ${{ gitea.actor }}

3. 确认使用的 Actions 在 Gitea 可用
   (大多数 actions/checkout、actions/setup-python 等都可用)

4. 在 Gitea 仓库 Secrets 中添加对应的环境变量

5. Push 代码触发第一次 Actions 运行

六、总结

Gitea Actions 是自托管 Git 服务走向完整 DevOps 平台的重要一步——对于已有 GitHub Actions 经验的团队,迁移到 Gitea Actions 几乎零学习成本,现有工作流文件只需微小调整即可直接运行。相比单独部署 Woodpecker CI,Gitea Actions 减少了一个需要维护的服务,是「尽量少的服务、足够的功能」原则的体现。

需要服务器?

香港 / 美国 / 日本服务器与 VPS,工程师 7×24 在线,工单平均 10 分钟内响应。