goreleaser 使用

Feb 21, 2023

首先,先在工程中下载安装 goreleaser

go install github.com/goreleaser/goreleaser@latest`

随后我们在工程目录下初始化 goreleaser 程序,使其生成配置文件

goreleaser init

随后修改目录下生成的 .goreleaser.yaml 文件
内容大概如下:

# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
  hooks:
   # You may remove this if you don't use go modules.
   #- go mod tidy
   # you may remove this if you don't need go generate
   #- go generate ./...
builds:
  - env:
   - CGO_ENABLED=0
   goos:
   - linux
   - windows
   - darwin
archives:
  - replacements:
      darwin: Darwin
      linux: Linux
      windows: Windows
      386: i386
      amd64: x86_64
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ incpatch .Version }}-next"
changelog:
  sort: asc
  filters:
    exclude:
      - '^docs:'
      - '^test:' 

我们需要添加一些字段,让工程可以正常编译.

# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
project_name: cloud_port_scan
before:
  hooks:
    # You may remove this if you don't use go modules.
    #- go mod tidy
    # you may remove this if you don't need go generate
    #- go generate ./...
builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
    goarch:
      - amd64
      - arm
      - arm64
archives:
  - format: tar.gz
    # this name template makes the OS and Arch compatible with the results of uname.
    name_template: >-
      {{ .ProjectName }}_
      {{- title .Os }}_
      {{- if eq .Arch "amd64" }}x86_64
      {{- else if eq .Arch "386" }}i386
      {{- else }}{{ .Arch }}{{ end }}
      {{- if .Arm }}v{{ .Arm }}{{ end }}
    # use zip for windows archives
    format_overrides:
    - goos: windows
      format: zip
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ incpatch .Version }}-next"
changelog:
  sort: asc
  filters:
    exclude:
      - '^docs:'
      - '^test:'

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

主要添加了

project_name: main

builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
    goarch:
      - amd64
      - arm
      - arm64

让其可以正常的选中工程名与对应平台

本地打包编译命令为:

goreleaser --snapshot --skip-publish --rm-dist