简介

GoReleaser 采用 Golang 开发,是一款用于 Golang 项目的自动发布工具。无需太多配置,只需要几行命令就可以轻松实现跨平台的包编译、打包和发布到 Github、Gitlab 等版本仓库种。

安装

MacOS

# 最新版本
$ brew install goreleaser/tap/goreleaser

# 可能不是最新版本
$ brew install goreleaser

从 Docker 运行

$ docker run --rm --privileged \
  -v $PWD:/go/src/github.com/user/repo \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -w /go/src/github.com/user/repo \
  -e GITHUB_TOKEN \
  -e DOCKER_USERNAME \
  -e DOCKER_PASSWORD \
  -e DOCKER_REGISTRY \
  goreleaser/goreleaser release

从源码编译

# 克隆到 GOPATH
$ git clone https://github.com/goreleaser/goreleaser
$ cd goreleaser

# 需要支持 go module 的 go 1.11 及以上版本
$ go get ./...

# 编译
$ o build -o goreleaser .

# 运行
$ ./goreleaser --version

快速开始

初始化

$ 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: "{{ .Tag }}-next"
changelog:
  sort: asc
  filters:
    exclude:
    - '^docs:'
    - '^test:'

你可以根据需要自行修改。

获取 Github Token

访问 Settings / Developer Settings / Personal access tokens,点击 Generate new token 按钮,生成一个新的 Token,将 Token 保存到 ~/.config/goreleaser/github_token 文件中。

为项目打上标签

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

执行自动发布流程

$ goreleaser --rm-dist

如果项目目录没有 dist 目录,可以不加 --rm-dist 参数,执行完成后将生成如下文件结构

dist/
├── CHANGELOG.md
├── checksums.txt
├── config.yaml
├── ects_0.5.1_Darwin_i386.tar.gz
├── ects_0.5.1_Darwin_x86_64.tar.gz
├── ects_0.5.1_Linux_i386.tar.gz
├── ects_0.5.1_Linux_x86_64.tar.gz
├── ects_darwin_386
│   └── ects
├── ects_darwin_amd64
│   └── ects
├── ects_linux_386
│   └── ects
└── ects_linux_amd64
    └── ects

4 directories, 11 files

并开始自动发布到 Github 的 Release 页面。

George:ects George$ goreleaser --rm-dist

   • releasing using goreleaser 0.117.1...
   • loading config file       file=.goreleaser.yml
   • RUNNING BEFORE HOOKS
      • running go mod tidy
      • running go generate ./...
   • LOADING ENVIRONMENT VARIABLES
   • GETTING AND VALIDATING GIT STATE
      • releasing v0.5.1, commit 6d1fc054405884ba88bbc3b8101bfb0491a5b9e2
   • 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      
      • pipe skipped              error=not a snapshot
   • CHECKING ./DIST  
      • --rm-dist is set, cleaning it up
   • WRITING EFFECTIVE CONFIG FILE
      • writing                   config=dist/config.yaml
   • GENERATING CHANGELOG
      • writing                   changelog=dist/CHANGELOG.md
   • BUILDING BINARIES
      • building                  binary=dist/ects_darwin_386/ects
      • building                  binary=dist/ects_darwin_amd64/ects
      • building                  binary=dist/ects_linux_amd64/ects
      • building                  binary=dist/ects_linux_386/ects
   • ARCHIVES         
      • creating                  archive=dist/ects_0.5.1_Linux_x86_64.tar.gz
      • creating                  archive=dist/ects_0.5.1_Darwin_x86_64.tar.gz
      • creating                  archive=dist/ects_0.5.1_Darwin_i386.tar.gz
      • creating                  archive=dist/ects_0.5.1_Linux_i386.tar.gz
   • LINUX PACKAGES WITH NFPM
      • pipe skipped              error=no output formats configured
   • SNAPCRAFT PACKAGES
      • pipe skipped              error=no summary nor description were provided
   • CALCULATING CHECKSUMS
      • checksumming              file=ects_0.5.1_Darwin_x86_64.tar.gz
      • checksumming              file=ects_0.5.1_Darwin_i386.tar.gz
      • checksumming              file=ects_0.5.1_Linux_i386.tar.gz
      • checksumming              file=ects_0.5.1_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       
      • S3               
         • pipe skipped              error=s3 section is not configured
      • BLOB             
         • pipe skipped              error=Blob section is not configured
      • HTTP PUT         
         • pipe skipped              error=put section is not configured
      • ARTIFACTORY      
         • pipe skipped              error=artifactory section is not configured
      • DOCKER IMAGES    
      • SNAPCRAFT PACKAGES
      • GITHUB/GITLAB/GITEA RELEASES
         • creating or updating release repo=betterde/ects tag=v0.5.1
         • release updated           url=https://github.com/betterde/ects/releases/tag/v0.5.1
         • uploading to release      file=dist/checksums.txt name=checksums.txt
         • uploading to release      file=dist/ects_0.5.1_Linux_x86_64.tar.gz name=ects_0.5.1_Linux_x86_64.tar.gz
         • uploading to release      file=dist/ects_0.5.1_Darwin_x86_64.tar.gz name=ects_0.5.1_Darwin_x86_64.tar.gz
         • uploading to release      file=dist/ects_0.5.1_Darwin_i386.tar.gz name=ects_0.5.1_Darwin_i386.tar.gz
         • uploading to release      file=dist/ects_0.5.1_Linux_i386.tar.gz name=ects_0.5.1_Linux_i386.tar.gz
      • HOMEBREW TAP FORMULA
         • pipe skipped              error=brew section is not configured
      • SCOOP MANIFEST   
         • pipe skipped              error=scoop section is not configured
   • release succeeded after 12.93s

当看到 release succeeded after 12.93s 时说明以及发布成功了,现在可以访问你的项目查看是否发布成功了。

Release

更多

更多详细配置请参考官方文档

I hope this is helpful, Happy hacking…