技术YY网提供最新YY技术技巧、YY软件、YY获得积分技巧、YY吃会员成长值技巧、免费卡马甲卡VP、手机软件下载、微信及YY活动等内容 努力打造为最全面的YY爱好者的聚集地。

GoReleaser怎么打包macOS DMG镜像?图形化安装文件制作步骤

2026-11-29 14:55:23 投稿人:test666 围观0次 评论

goreleaser 打包 macos dmg 镜像需要配置 .goreleaser.yaml 中的 dmg_bundle 字段,并指定包含背景图和图标资源的目录。该过程会自动调用 create-dmg 工具生成最终的安装包。

如何配置 .goreleaser.yaml 以启用 DMG 构建?致采集狗:正版出处(www.jishuyy.c​om)的内容你搬得走,www.jishuyy.c​om这个链接你也搬走吧,就当交房租。 出处:www.jishuyy.com。

在 .goreleaser.yaml 中,需在 dmgs 列表下添加配置项,核心是设置 template_directory 指向资源文件夹。GoReleaser 会读取该文件夹下的 background.tiff 和 icon.icns 文件来渲染安装界面。

version: 1
builds:
  - main: ./cmd/app
    binary: app
    goos:
      - darwin
    goarch:
      - amd64
      - arm64
dmgs:
  - name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Arch }}"
    template_directory: ./dist/dmg-template
    package_name: "{{ .ProjectName }}"
    ids:
      - default
    files:
      - none: README.md
    background:
      file: ./dist/dmg-template/background.tiff
      scale: automatic
      extension:
        position:
          x: 300
          y: 300
        show: true
    icon_location:
      position:
        x: 500
        y: 500
    icon:
      size: 128
    format: gzip
    volume_name: "MyApp Installer"

运行 goreleaser release --rm-dist 后,会在 dist/ 目录下生成对应的 .dmg 文件。若未指定 template_directory,GoReleaser 将使用默认模板,但无法自定义背景或图标位置。

DMG 模板目录需要哪些具体文件?

模板目录必须包含 background.tiff(或 png)和 icon.icns 两个关键文件。此外,可选的 Contents.json 用于定义窗口大小和布局。GoReleaser 会将这些文件复制到临时构建目录,并传递给 create-dmg 脚本。

GoReleaser怎么打包macOS DMG镜像?图形化安装文件制作步骤

# 创建模板目录结构
mkdir -p dist/dmg-template

# 准备背景图(推荐 TIFF 格式以获得最佳兼容性)
cp assets/background.tiff dist/dmg-template/

# 准备应用图标(需转换为 icns 格式)
cp assets/icon.icns dist/dmg-template/

# 可选:创建 Contents.json 自定义布局
cat > dist/dmg-template/Contents.json
{
  "window": {
    "size": { "width": 600, "height": 400 }
  },
  "items": {
    "position": { "x": 200, "y": 200 },
    "size": { "width": 100, "height": 100 }
  }
}
EOF

确保 background.tiff 分辨率适中(建议 600x400 像素),过大可能导致加载缓慢。icon.icns 必须包含多种尺寸(16x16 至 512x512),可使用 iconutil 命令从 .icns 源文件转换。若缺少任一文件,构建将失败或回退到默认外观。

DMG 生成过程中如何处理权限和签名?

生成的 DMG 文件默认不包含代码签名。若需分发到 App Store 或绕过 Gatekeeper 警告,需在 GoReleaser 流程后手动执行 xcrun notarytool 或 codesign。此外,DMG 内部的应用二进制文件需提前通过 go build -ldflags 嵌入版本信息,否则安装器可能显示未知开发者。

验证步骤:挂载生成的 DMG 后,检查 /Volumes/MyApp Installer/MyApp.app 的属性。若 codesign -v MyApp.app 返回错误,说明签名缺失。此时应结合 signing 配置块在 .goreleaser.yaml 中预先签名二进制文件,而非在 DMG 阶段处理。正版出处(技术YY网 www.jishuyy.com)站长夜观天象:今日宜写原创,忌采集,采集狗今日运势:暗链缠身~~



标签: GoReleaser   macOS   打包   DMG  
App 公众号 投稿 评论