This is the fourth day of my participation in the First Challenge 2022.

As a MacOS user, Homebrew is naturally indispensable, as it is the installation package management tool for MacOS. This article describes how you can install Go binaries using Brew Install if you publish a Go binary to Homebrew Tap.

This article also introduces two publishing methods, one is purely manual and the other is automatic publishing based on GitHub Actions + GoReleaser.

Manual release

Create a Formula

The Formula is created locally by command, followed by a link to the binaries we need to publish

The brew the create https://github.com/k8scat/Articli/releases/download/v0.2.2/acli-darwin-arm64.tar.gzCopy the code

Through this command, we can get a articli rb file in/usr/local/Homebrew/Library/Taps/Homebrew/Homebrew – core/Formula/directory.

Modified Formula

Here is the content of the original file

# Documentation: https://docs.brew.sh/Formula-Cookbook
# https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Articli < Formula
  desc "Manage content in multi platforms."
  homepage ""
  url "https://github.com/k8scat/Articli/releases/download/v0.2.2/acli-darwin-arm64.tar.gz"
  sha256 "c04aad6e2ca3ccf1d902d0f4f2bbc4422c67fa355a166cc3b0dac2fd9f420151"
  license "MIT"

  # depends_on "cmake" => :build

  def install
    # ENV.deparallelize # if your formula fails when building in parallel
    # Remove unrecognized options if warned by configure
    # https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
    system "./configure", *std_configure_args, "--disable-silent-rules"
    # system "cmake", "-S", ".", "-B", "build", *std_cmake_args
  end

  test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test Articli`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end
Copy the code

Simplify this file

# Documentation: https://docs.brew.sh/Formula-Cookbook
# https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Articli < Formula
  desc "Manage content in multi platforms."
  homepage "https://github.com/k8scat/Articli"
  url "https://github.com/k8scat/Articli/releases/download/v0.2.2/acli-darwin-arm64.tar.gz"
  sha256 "c04aad6e2ca3ccf1d902d0f4f2bbc4422c67fa355a166cc3b0dac2fd9f420151"
  license "MIT"
  version "0.2.2"

  def install
    bin.install "acli"
  end
end

Copy the code

Since you are downloading a binary package, you can simply insert bin.install “acli” into install.

Create a personal Homebrew Tap

Create a new public repository on GitHub called Homebrew-tap, which is k8SCat/Homebrew-tap. Then upload the modified articli.rb file above to the repository, creating our personal Homebrew Tap.

Use Homebrew Tap

#Add the Homebrew Tap
#K8scat /tap points to the personal Homebrew tap we just created: k8scat/ Homebrew-tap
brew tap k8scat/tap
#Installing binary packages
brew install articli

#A command installation, which automatically adds Homebrew Tap
brew install k8scat/tap/articli
Copy the code

The installation sequence

If packages of the same name appear on homebrew/ Core and personal Homebrew Tap, the packages on Homebrew/Core will be installed by default, but we can specify Homebrew Tap:

#Specifies Homebrew Tap for installation
brew install k8scat/tap/articli
Copy the code

There is another way to change the order: the brew pin command can be used to increase the priority of a person’s Homebrew Tap so that packages on that person’s Homebrew Tap will be installed first.

Automated publishing

The above brew create command to create Formula and manually modify the file, then upload to Homebrew Tap repository. This method can be automated with GitHub Actions + GoReleaser.

Add the.goreleaser. Yml file

The following configuration tells GoReleaser how to generate a formula

brews:
  - name: acli
    tap:
      owner: k8scat
      name: homebrew-tap
      token: "{{ .Env.HOMEBREW_TOKEN }}"

    url_template: "https://github.com/k8scat/articli/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
    # Git author used to commit to the repository.
    # Defaults are shown.
    commit_author:
      name: goreleaserbot
      email: [email protected]
    homepage: "https://github.com/k8scat/articli"
    description: "Manage content in multi platforms."

    # Packages your package depends on.
    dependencies:
      - name: git

    # So you can `brew test` your formula.
    # Default is empty.
    test: | system "#{bin}/acli version"    # Custom install script for brew.
    # Default is 'bin.install "program"'.
    install: | bin.install "acli" # Install bash completion output = Utils.safe_popen_read("#{bin}/acli", "completion", "bash") (bash_completion/"acli").write output # Install zsh completion output = Utils.safe_popen_read("#{bin}/acli", "completion", "zsh") (zsh_completion/"_acli").write outputCopy the code

GitHub Actions

The GitHub Token of Homebrew Tap repository needs to be set. Here we set HOMEBREW_TOKEN.

name: Release

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.17

      - name: Validates GO releaser config
        uses: goreleaser/goreleaser-action@v2
        with:
          args: check

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: The ${{ secrets.GITHUB_TOKEN }}
          # Token for homebrew-tap repo: https://github.com/k8scat/homebrew-tap
          HOMEBREW_TOKEN: The ${{ secrets.HOMEBREW_TOKEN }}
Copy the code

This way we can publish by creating a label and the newly published formula will be automatically added to the Homebrew Tap repository.

Refer to the project

Articli a command line tool that can manage content from multiple platforms.

The original link

The publish – go – package.