til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

Build Precompiled Binaries For Your Go Executables

One of the reasons why I love open source, is that you can see what other people are doing, you can read, learn and adopt.

One of the projects I am following had the following PR. It was a proposal for building executables for multiple platforms and operating systems for your particular executable.

Since I am currently learning Go and I am building some small utilities, which happen to be executables. I decided to see if I could use this in my projects.

First file .github/workflows/release.yml:

name: goreleaser

on:
  push:
    tags:
      - 'v*'

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      -
        name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.14
      -
        name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release
        env:
          GITHUB_TOKEN: $

It checkouts the code when a repository is tagged and the tag is prefixed with v, meaning we can still do tags, which should not result in a build related to a release.

The GITHUB_TOKEN used by the build is auto-generated, so you do not have to do anything.

The workflow uses an action for goreleaser which is available on the GitHub Marketplace.

Do note the fetch-depth: 0 option, this is used to ensure that all commits are fetched, so the build can be based on the latest commit.

IMPORTANT: note the fetch-depth: 0 input in Checkout step. It is required for the changelog to work correctly.

REF: GitHub Marketplace: goreleaser

Next up is the configuration for goreleaser.

Second file .goreleaser.yml:

builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
archives:
  - format: binary
    replacements:
      darwin: Darwin
      linux: Linux
      windows: Windows
      386: i386
      amd64: x86_64
checksum:
  name_template: 'checksums.txt'

If you want to see the above put to use go to my repository, check the releases and see the impressive list.

Resources and References

  1. GitHub Marketplace: goreleaser
  2. GitHub Docs: Automatic Token Authentication