In this article, I will cover how to publish a go package to Chocolatey. Chocolatey is one of the most popular package managers on windows and recently I wanted a windows build of my CLI published there but there were no online resources. After hours of digging through the docs, I was able to successfully publish here and in this guide I will be showing you how to publish yours as well.
Introduction
First we need to build a CLI to publish, if you have yours you can skip this. But to make this as simple as possible I will write a go program that prints the current system time. Create a main.go file and fill in these contents.
package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
fmt.Println("Current time:", currentTime.Format("2006-01-02 15:04:05"))
}
Then to aid us publishing, we are going to use a wonderful package called Goreleaser. It allows publishing to Homebrew, Chocolatey and several other package managers as easily as possible. First we install goreleaser by running:
go install github.com/goreleaser/goreleaser/v2@latest
Then we scaffold the config file by running:
goreleaser init
This will create a .goreleaser.yaml file in the current working directory.
Now take a look at the builds section. Currently it will look something like this:
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
This section is for customizing the build and state what os, cpu arch you will like to create binaries for. For the full documentation, check here. But for the purpose of this. I will just add the name of the CLI and build for just windows since Choco is windows-specific
builds:
-
binary: currentTime
env:
- CGO_ENABLED=0
goos:
- windows
We will also add a chocolateys section. Here we will add all choco specific details. The full list of options is here. But the base to get your package published and approved by maintainers is this:
chocolateys:
- name: currentTime
title: {{ Package Title }}
authors: {{ Your name }}}
project_url: {{ Github Repo to the package }}
url_template: "https://github.com/foo/bar/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
copyright: 2024
license_url: https://github.com/foo/bar/blob/main/LICENSE
project_source_url: https://github.com/foo/bar
docs_url: {{ Link to Docs }}
bug_tracker_url: https://github.com/foo/bar/issues
release_notes: "https://github.com/foo/bar/releases/tag/v{{ .Version }}"
api_key: "{{ .Env.CHOCOLATEY_API_KEY }}"
source_repo: "https://push.chocolatey.org/"
skip_publish: false
Fill these fields except url_template, source_repo, api_key(we will add this to a github action secret) and then we move on to the next step.
Creating a Github Action
Now we create a github action to publish this CLI to choco anytime we make a release. create a .github/workflows/release.yml file in your current working directory and add the following.
name: Build release binaries
on:
workflow_dispatch:
push:
tags:
- "*"
permissions:
contents: write
jobs:
goreleaser:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.19
- name: Verify Chocolatey installation
run: choco --version
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: ${{ env.GITHUB_REF_NAME }}
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CHOCOLATEY_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}
This workflow will be dispatched when a new tag is created on the repository.
- It runs on a windows action
- The windows action comes built with chocolatey, but we don't know if that can change in the future. so I added a step to verify that Choco is installed
- Then we run goreleaser using the goreleaser action. The two environment variables
GITHUB_TOKEN
andCHOCOLATEY_API_KEY
we will be adding in the next step.
Now push all your code to a github repository then let's proceed with the next step.
Adding Repository Secrets
Now our github workflow needs two secrets. GITHUB_TOKEN
and CHOCOLATEY_API_KEY
. github token is a github personal access token. You can get those in your settings > Developer settings on github. Then for the chocolatey api key. You go to the choco community page. sign up and then find your api key on the profile page. After that you will be adding them on the action secrets section of your repository settings page here.
For the next step, we will create a new github release.
- First we create a tag on new publish
- And then publish as a new release
- After publishing, check the actions for the status of the workflow. If the workflow is successful, check you choco packages section to see that the package is published.
Conclusion
Thank you for following along with this. Happy building!
Top comments (0)