温馨提示×

如何简化Debian Golang的打包步骤

小樊
44
2025-12-09 00:55:00
栏目: 编程语言

简化 Debian Golang 打包的实操方案

一、推荐路径 dh-golang 最小化流程

  • 安装工具链:sudo apt install dh-golang golang-go debhelper-compat(compat 建议用 13)。
  • 在项目根目录生成最小 debian 骨架:dh_make -f …/your-app_1.0.0.orig.tar.gz -p your-app_1.0.0,类型选 single binary
  • 精简关键文件:保留并最小化编辑 debian/controldebian/rulesdebian/changelogdebian/copyrightdebian/compat
  • 最简 debian/rules(使用 dh-golang 插件):
    #!/usr/bin/make -f
    %:
    dh $@ --with golang
  • 构建与签名:debuild -us -uc(或 dpkg-buildpackage -us -uc -b)。
  • 说明:dh-golang 会自动处理 Go 模块依赖、构建与安装,适合绝大多数基于 gc 编译器 的 Go 应用,能显著减少手写规则与维护成本。

二、最小化配置文件示例

  • debian/control(单二进制示例):
    Source: your-go-app
    Section: utils
    Priority: optional
    Maintainer: Your Name you@example.com
    Build-Depends: debhelper-compat (= 13), dh-golang, golang-go
    Standards-Version: 4.6.0
    Homepage: https://github.com/your/go-app
    Vcs-Browser: https://github.com/your/go-app
    Vcs-Git: https://github.com/your/go-app.git

    Package: your-go-app
    Architecture: any
    Depends: ${shlibs:Depends}, ${misc:Depends}
    Description: Short description
    Long description of your application.

  • debian/rules(最简):
    #!/usr/bin/make -f
    %:
    dh $@ --with golang

  • 如需安装额外文件(可选):创建 debian/install
    your-go-app /usr/bin/
    config.toml /etc/your-go-app/
    static/ /usr/share/your-go-app/static/

  • 要点:保持包内容简洁,仅打包二进制与必要资源;debian/copyright 需完整覆盖应用与依赖许可证;构建前在目标环境安装并实测 .deb,确保文件落位与运行正常。

三、常见简化技巧与避坑

  • 静态链接与 lintian:Go 默认静态链接,可能触发某些 lintian 告警。优先用 dh-golang 的标准流程;若仅为告警且已评估无风险,可使用 lintian override 或阶段性用 dpkg-buildpackage -us -uc -b 绕过自动检查(不建议长期绕过)。
  • 资源与安装:除二进制外,用 debian/install 精确声明配置文件、静态资源等安装路径,避免把 整个模块缓存或源码 打进包。
  • 编译器选择:日常应用优先 gc + dh-golang;若需与系统库深度动态链接或进入官方仓库的严格策略,可考虑 gcc-go(生成动态链接二进制,复杂度更高)。

四、一键化脚本模板

  • 使用说明:在项目根目录执行,自动安装工具、生成骨架、写入最小 rules/control,并构建 .deb。
    #!/usr/bin/env bash
    set -e
    PKG=your-go-app
    VER=1.0.0
    DEBVER=1
    FULLVER=${VER}-${DEBVER}
    ORIG=…/${PKG}_${VER}.orig.tar.gz

    sudo apt update && sudo apt install -y dh-golang golang-go debhelper-compat

    生成 orig.tar.gz(若不存在)

    if [ ! -f “$ORIG” ]; then
    git archive --prefix=${PKG}-${VER}/ HEAD | gzip -9 > “$ORIG”
    fi

    生成 debian 骨架

    dh_make -f “$ORIG” -p ${PKG}_${FULLVER} -s

    写入最小 rules

    cat > debian/rules <<‘EOF’
    #!/usr/bin/make -f
    %:
    dh $@ --with golang
    EOF
    chmod +x debian/rules

    写入最小 control(示例,按需调整)

    cat > debian/control <<EOF
    Source: ${PKG}
    Section: utils
    Priority: optional
    Maintainer: Your Name you@example.com
    Build-Depends: debhelper-compat (= 13), dh-golang, golang-go
    Standards-Version: 4.6.0
    Homepage: https://github.com/your/go-app
    Vcs-Browser: https://github.com/your/go-app
    Vcs-Git: https://github.com/your/go-app.git

    Package: ${PKG}
    Architecture: any
    Depends: ${shlibs:Depends}, ${misc:Depends}
    Description: Short description
    Long description.
    EOF

    构建

    debuild -us -uc

  • 提示:构建成功后在上层目录得到 ${PKG}_${FULLVER}_amd64.deb,在目标环境安装并回归测试。

0