go

goreleaser

一键跨平台编译

Posted by Liangjf on March 30, 2020

goreleaser一键跨平台编译

gorelease用于Go项目的发布自动化工具。目标是简化构建发布发布步骤,同时为所有步骤提供不同的自定义选项。GoReleaser是为CI工具而构建的。

deb/rpm安装

curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh sh

在当前生成一个bin目录,里面包含了gorelease二进制文件,将其复制到/user/local/bin即可。

goreleaser init初始化

goreleaser init

生成 .goreleaser.yml 文件

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

before:
      hooks:

如果是不需要编译前执行的hook,go mod tidy等命令,那么可以去掉。

跨平台编译(跳过快照,跳过push到github)

goreleaser release –snapshot –skip-publish –rm-dist

   • releasing using goreleaser 0.124.1...
   • loading config file       file=.goreleaser.yml
   • RUNNING BEFORE HOOKS
      • running go mod tidy
      • running go generate ./...
   • LOADING ENVIRONMENT VARIABLES
      • pipe skipped              error=publishing is disabled
   • 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
      • releasing v0.0.0, commit 616546933f7c309b642f530bb2be015cc1afd0c2
      • pipe skipped              error=disabled during snapshot mode
   • PARSING TAG      
   • SETTING DEFAULTS 
      • LOADING ENVIRONMENT VARIABLES
      • SNAPSHOTING      
      • GITHUB/GITLAB/GITEA RELEASES
      • PROJECT NAME     
      • BUILDING BINARIES
      • ARCHIVES         
      • LINUX PACKAGES WITH NFPM
      • SNAPCRAFT PACKAGES
      • CALCULATING CHECKSUMS
      • SIGNING ARTIFACTS
      • DOCKER IMAGES    
      • ARTIFACTORY      
      • S3               
      • BLOB             
      • HOMEBREW TAP FORMULA
         • optimistically guessing `brew[0].installs`, double check
      • SCOOP MANIFEST   
   • SNAPSHOTING      
   • CHECKING ./DIST  
      • --rm-dist is set, cleaning it up
   • WRITING EFFECTIVE CONFIG FILE
      • writing                   config=dist/config.yaml
   • GENERATING CHANGELOG
      • pipe skipped              error=not available for snapshots
   • BUILDING BINARIES
      • building                  binary=dist/wxBot4g_darwin_386/wxBot4g
      • building                  binary=dist/wxBot4g_darwin_amd64/wxBot4g
      • building                  binary=dist/wxBot4g_linux_amd64/wxBot4g
      • building                  binary=dist/wxBot4g_linux_386/wxBot4g
   • ARCHIVES         
      • creating                  archive=dist/wxBot4g_v0.0.0-next_Linux_x86_64.tar.gz
      • creating                  archive=dist/wxBot4g_v0.0.0-next_Linux_i386.tar.gz
      • creating                  archive=dist/wxBot4g_v0.0.0-next_Darwin_i386.tar.gz
      • creating                  archive=dist/wxBot4g_v0.0.0-next_Darwin_x86_64.tar.gz
   • LINUX PACKAGES WITH NFPM
   • SNAPCRAFT PACKAGES
   • CALCULATING CHECKSUMS
      • checksumming              file=wxBot4g_v0.0.0-next_Darwin_x86_64.tar.gz
      • checksumming              file=wxBot4g_v0.0.0-next_Darwin_i386.tar.gz
      • checksumming              file=wxBot4g_v0.0.0-next_Linux_i386.tar.gz
      • checksumming              file=wxBot4g_v0.0.0-next_Linux_x86_64.tar.gz
   • SIGNING ARTIFACTS
      • pipe skipped              error=artifact signing is disabled
   • DOCKER IMAGES    
      • pipe skipped              error=docker section is not configured
   • PUBLISHING       
      • pipe skipped              error=publishing is disabled
   • release succeeded after 4.79s

在当前目录生成dist目录

├── dist
│   ├── checksums.txt
│   ├── config.yaml
│   ├── wxBot4g_darwin_386
│   │   └── wxBot4g
│   ├── wxBot4g_darwin_amd64
│   │   └── wxBot4g
│   ├── wxBot4g_linux_386
│   │   └── wxBot4g
│   ├── wxBot4g_linux_amd64
│   │   └── wxBot4g
│   ├── wxBot4g_v0.0.0-next_Darwin_i386.tar.gz
│   ├── wxBot4g_v0.0.0-next_Darwin_x86_64.tar.gz
│   ├── wxBot4g_v0.0.0-next_Linux_i386.tar.gz
│   └── wxBot4g_v0.0.0-next_Linux_x86_64.tar.gz

可以看到,为我们生成了不同平台的可执行文件,并为每个OS/Arch对创建一个存档,并将其存档到一个单独的文件中。如果我们不需要那个平台的文件,那么可以在 .goreleaser.yml 文件中注释掉。

当然也可以修改 .goreleaser.yml 文件的内容来定制化编译生成可执行文件。

创建标签

GoReleaser将使用您存储库的最新Git标签。创建一个标签并将其推送到GitHub:

$ git tag -a v0.1.0 -m "First release"
$ git push origin v0.1.0

如果不想创建标签,可以使用–snapshot基于最新提交来创建发行版。(注意:只有GitHub支持快照)

发布前测试

如果要在“真正”发布之前测试,可以使用–skip-publish,仅会构建和打包操作:

goreleaser release –skip-publish

接入github和gitlab

设置环境变量

export GITHUB_TOKEN=`YOUR_GH_TOKEN`
export GITLAB_TOKEN=`YOUR_GL_TOKEN`

这两个token是用于将发布部署到GitHub/GitLab,将会有权限触发push到github和调用gitlab的api和ci能力。

goreleaser

直接构建,打包,并推送到github