You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker化Gitea:容器外执行脚本或扩展容器运行环境的方案

两种Gitea Docker部署post-receive钩子的实现方案

方案一:宿主机git用户执行钩子脚本

这种方式利用宿主机环境执行部署流程,无需修改Gitea镜像,适合依赖宿主机特定环境的场景。

  • 步骤1:挂载仓库钩子目录到宿主机
    修改你的docker-compose.yml,在Gitea服务的volumes中添加钩子目录的挂载,将容器内的仓库钩子目录映射到宿主机:

    services:
      gitea:
        image: gitea/gitea:latest
        volumes:
          - ./gitea-data:/data
          - ./gitea-repos/your-user/your-site/hooks:/data/gitea/repos/your-user/your-site/hooks
        # 其他端口、环境变量等配置保持不变
    

    替换your-user和your-site为实际用户名和仓库名,提前在宿主机创建对应目录并设置权限:

    sudo mkdir -p ./gitea-repos/your-user/your-site/hooks && sudo chown -R git:git ./gitea-repos
    
  • 步骤2:编写post-receive钩子脚本
    在宿主机挂载的钩子目录下创建post-receive文件,内容如下(根据实际需求调整路径和命令):

    #!/bin/bash
    
    # 切换到宿主机的部署目录
    cd /var/www/your-site-deploy
    
    # 拉取最新代码(以git用户身份执行)
    sudo -u git git pull origin main
    
    # 执行构建命令
    make build
    
    # 运行Python处理脚本
    sudo -u git python3 /path/to/your-processing-script.py
    
    # 上传构建产物到目标服务
    curl -X POST -F "artifact=@./dist/site-package.tar.gz" https://your-upload-endpoint.com/upload
    
  • 步骤3:设置脚本权限
    确保脚本可执行且归属git用户:

    sudo chmod +x ./gitea-repos/your-user/your-site/hooks/post-receive
    sudo chown git:git ./gitea-repos/your-user/your-site/hooks/post-receive
    

    同时在/etc/sudoers中配置git用户免密执行必要命令,避免脚本执行时需要输入密码:

    git ALL=(ALL) NOPASSWD: /usr/bin/git, /usr/bin/make, /usr/bin/python3, /usr/bin/curl
    

方案二:容器内安装依赖执行钩子

这种方式在Gitea容器内安装所需工具,整个部署流程在容器内完成,适合环境隔离需求。

  • 步骤1:构建自定义Gitea镜像
    创建Dockerfile文件,基于官方Gitea镜像安装git、make、Python及所需模块:

    FROM gitea/gitea:latest
    
    # 更新包列表并安装基础工具
    RUN apt-get update && apt-get install -y --no-install-recommends \
        git \
        make \
        python3 \
        python3-pip \
        curl \
        && rm -rf /var/lib/apt/lists/*
    
    # 安装Python额外依赖(示例:requests库)
    RUN pip3 install --no-cache-dir requests
    
  • 步骤2:修改docker-compose.yml使用自定义镜像
    更新docker-compose.yml,将Gitea服务的image改为build方式:

    services:
      gitea:
        build: ./gitea-custom  # 指向存放Dockerfile的目录
        volumes:
          - ./gitea-data:/data
          - ./gitea-scripts:/data/gitea/scripts  # 挂载自定义脚本目录到容器
        # 其他配置保持不变
    

    构建镜像:

    docker-compose build gitea
    
  • 步骤3:编写容器内的post-receive钩子
    在容器内的/data/gitea/repos/your-user/your-site/hooks目录下创建post-receive脚本(或通过宿主机挂载的钩子目录编辑):

    #!/bin/bash
    
    # 创建临时构建目录
    BUILD_DIR=/tmp/site-build
    mkdir -p $BUILD_DIR
    cd $BUILD_DIR
    
    # 从本地仓库检出代码
    git clone /data/gitea/repos/your-user/your-site.git .
    
    # 执行构建
    make build
    
    # 运行Python处理脚本(脚本放在宿主机挂载的gitea-scripts目录中)
    python3 /data/gitea/scripts/your-processing-script.py
    
    # 上传构建产物
    curl -X POST -F "artifact=@./dist/site-package.tar.gz" https://your-upload-endpoint.com/upload
    
    # 清理临时目录
    rm -rf $BUILD_DIR
    
  • 步骤4:设置脚本权限
    进入容器执行以下命令(或在宿主机挂载的目录中设置):

    docker exec -it gitea bash
    chmod +x /data/gitea/repos/your-user/your-site/hooks/post-receive
    chown git:git /data/gitea/repos/your-user/your-site/hooks/post-receive
    

内容的提问来源于stack exchange,提问作者baumschubser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:23:17