环境说明

  • 开发环境:windows 10
  • 生产环境:Ubuntu 18.04.6 LTS
  • goreleaser v1.5.0
  • go version go1.17.6 windows/amd64

步骤说明

安装

参看 Goreleaser Install

可以找到相应系统的安装方式,我这里用的是

go install

go install github.com/goreleaser/goreleaser@latest

  04/03/2022   14:07.32   /home/mobaxterm  goreleaser -v
goreleaser version dev
goos: windows
goarch: amd64
module version: v1.5.0, checksum: h1:l4BxBt2Rrk5Tngmzyrcp7H/CszXSA0guzL4mPo4peAU=

https://goreleaser.com

配置

参考Goreleaser Quick Start

执行 goreleaser init ,会生成一个.goreleaser.yml配置

goreleaser init
• Generating .goreleaser.yaml file
• config created; please edit accordingly to your needs file=.goreleaser.yaml

自行按下文中GOOS&GOARCH 参照表配置

以下为我的配置

# 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
goarch:
- amd64
- arm
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:'

打包

执行

goreleaser release --snapshot --rm-dist

公司的小垃机Cpu直接100%…

成功

• release succeeded after 8.02s

问题解决

release failed after 7.30s error=failed to build for linux_386: exit status 2: # github.com/pingcap/parser/mysql

goreleaser release --snapshot --rm-dist
• releasing...
• loading config file file=.goreleaser.yaml
• loading environment variables
• getting and validating git state
• ignoring errors because this is a snapshot error=git doesn't contain any tags. Either add a tag or use --snapshot
• building... commit=ca535cb8fe0bc89594b98ec81381a44bb8a9a395 latest tag=v0.0.0
• pipe skipped error=disabled during snapshot mode
• parsing tag
• running before hooks
• running hook=go mod tidy
• setting defaults
• snapshotting
• building snapshot... version=0.0.1-next
• checking distribution directory
• --rm-dist is set, cleaning it up
• loading go mod information
• build prerequisites
• writing effective config file
• writing config=dist\config.yaml
• building binaries
• building binary=dist\bsc-scan_linux_386\bsc-scan
• building binary=dist\bsc-scan_linux_amd64\bsc-scan
• building binary=dist\bsc-scan_linux_arm64\bsc-scan
⨯ release failed after 7.30s error=failed to build for linux_386: exit status 2: # github.com/pingcap/parser/mysql
C:\Users\axin\go\pkg\mod\github.com\pingcap\parser@v0.0.0-20210415081931-48e7f467fd74\mysql\const.go:421:2: constant 2147483648 overflows SQLMode
C:\Users\axin\go\pkg\mod\github.com\pingcap\parser@v0.0.0-20210415081931-48e7f467fd74\mysql\const.go:422:2: constant 4294967296 overflows SQLMode
C:\Users\axin\go\pkg\mod\github.com\pingcap\parser@v0.0.0-20210415081931-48e7f467fd74\mysql\util.go:44:19: constant 4294967295 overflows int
C:\Users\axin\go\pkg\mod\github.com\pingcap\parser@v0.0.0-20210415081931-48e7f467fd74\mysql\util.go:45:19: constant 4294967295 overflows int

参看:SQLMode should be int64 or uint64; similarly for lengthAndDecimal fields #364

这是因为 github.com/pingcap/parser/mysql 常量配置溢出,我这把386配置删除了,只打包amd64 & arm

上线运行

打包好的文件上传到Ubuntu服务器

执行脚本

#!/bin/bash
nohup ./main > ./logs/main.log 2>&1 &

赋予权限

chmod +x ./main

运行

./start.sh

参考文献

文档

goreleaser Github

goreleaser Documentation

GOOS&GOARCH 参照表

GOOS GOARCH
aix ppc64
android 386
android amd64
android arm
android arm64
darwin 386
darwin amd64
darwin arm
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
illumos amd64
js wasm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
linux s390x
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
openbsd arm64
plan9 386
plan9 amd64
plan9 arm
solaris amd64
windows 386
windows amd64