简化 Debian Golang 打包的实操方案
一、推荐路径 dh-golang 最小化流程
二、最小化配置文件示例
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,确保文件落位与运行正常。
三、常见简化技巧与避坑
四、一键化脚本模板
使用说明:在项目根目录执行,自动安装工具、生成骨架、写入最小 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
if [ ! -f “$ORIG” ]; then
git archive --prefix=${PKG}-${VER}/ HEAD | gzip -9 > “$ORIG”
fi
dh_make -f “$ORIG” -p ${PKG}_${FULLVER} -s
cat > debian/rules <<‘EOF’
#!/usr/bin/make -f
%:
dh $@ --with golang
EOF
chmod +x debian/rules
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,在目标环境安装并回归测试。