loading...
Cover image for πŸš€ GitHub Action for release your Go projects as fast and easily as possible

πŸš€ GitHub Action for release your Go projects as fast and easily as possible

koddr profile image Vic ShΓ³stak ・4 min read

Introduction

Hello, DEV World! πŸ˜‰ Continuing the series of articles on automating routine using the excellent CI/CD tool β€” GitHub Actions.

It's time to automate the releases of your applications written on Go!

πŸ“ Table of contents

πŸ€” What will we automate?

If you've ever faced the challenge of releasing a project written on Go for different platforms/OS, you already know what kind of problems you may come across. If not, I will bring some of them:

  • You need to constantly monitor the features of each OS (for example, specific slashes in the paths).
  • You need to create archives manually and upload them to the release page on GitHub.
  • You need to look at all the commits to make CHANGELOG for a new version.
  • ...and many more!

So, what if I told you that there is a tool that will allow you to do all this (and more) automatically after creating a tag in your repository?

I'm not kidding, it allows you to do all this with GoReleaser GitHub action πŸ‘‡

GitHub logo goreleaser / goreleaser-action

:octocat: GitHub Action for GoReleaser

GoReleaser Logo

GoReleaser Action

GitHub Action for GoReleaser

GitHub release GitHub marketplace Test workflow Codecov Become a sponsor


GoRelease Action

Usage

Workflow

name: goreleaser
on
  pull_request:
  push:

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
        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 --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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

Run on new tag

If you want to run GoReleaser only on new tag, you can use this event:

on:
  push:
    tags:
      - '*'

Or with a condition on GoReleaser step:

      -
        name: Run GoReleaser
        uses
…

↑ Table of contents

πŸ‘€ Repository structure

For a short, project will be contain very simple function:

package main

import "fmt"

func main() {
    fmt.Println("Hi, DEV World! πŸ˜‰")
}

And the repository structure will be as simple as that:

.
β”œβ”€β”€ .github
β”‚   └── workflows
β”‚       └── release_build.yml  # <-- GitHub Action
└── main.go                    # <-- main function

↑ Table of contents

πŸ€– Configure GitHub Action

Let's move on to the main character of our story. Look at the listing of the file release_build.yml, which already has a lot of familiar elements (if you did read my previous articles about GitHub Action 😏):

name: Release Go project

on:
  push:
    tags:
      - "*" # triggers only if push new tag version, like `0.8.4` or else

jobs:
  build:
    name: GoReleaser build
    runs-on: ubuntu-latest

    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v2
        with:
          fetch-depth: 0 # See: https://goreleaser.com/ci/actions/

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

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@master
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }}

Yes, GitHub automatically creates a GITHUB_TOKEN secret to use in your workflow, but I recommend to create your own token with repo scopes here:

GITHUB_TOKEN

And now, create new GitHub secret (GO_RELEASER_GITHUB_TOKEN in my case):

GitHub secret

↑ Table of contents

βœ… Troubleshooting

If your file with main() function is not in the root directory of your repository, GoReleaser will generate an error. Therefore, you will need to specify the correct directory in the step settings, for example like this:

# ...

    steps:

      # ...

      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@master
        with:
          version: latest
          args: release --rm-dist
          workdir: ./cmd/myapp # <-- path to directory with main() func
        env:
          GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }}

↑ Table of contents

πŸš€ Time to release!

Create new release with tag (in my case, it's 0.8.4) and go to Actions page in order to follow up on the job:

go releaser job

After successfully completing the job, go to release page to see awesome result: CHANGELOG with all of your commits (between two versions), checksums.txt file and uploaded archives with compiled apps for all platforms!

release page

That's it! Just like that! πŸŽ‰

↑ Table of contents

🎯 Real-life example

If the screenshots above are not enough, I invite you to look at the one of my projects, where I use GoReleaser action in real-life:

GitHub logo create-go-app / cli

✨ Powerful CLI for Create Go App. Set up a new full stack app by running only one command!


Create Go App CLI

Set up a new Go (Golang) full stack app by running one CLI command!

cli versionΒ go versionΒ go coverΒ go reportΒ lisense


⚑️ Quick start

Let's create a new app into ./app folder with Fiber as backend and Preact as frontend:

cgapp create -p ./app -b fiber -f preact

That's all you need to start! πŸ˜‰

βš™οΈ Installation

First of all, download and install Go. Version 1.11 or higher is required.

Installation is done by using the go build command with $GOPATH/bin:

go build -i -o $GOPATH/bin/cgapp github.com/create-go-app/cli

Check, that the CLI is installed correctly by the --version (or -v) command:

cgapp --version 
# cgapp version X.X.X

~ Alternative installations

We're using a GoReleaser project for shipping standalone Create Go App CLI version to all major desktop platforms: Apple macOS, GNU/Linux, MS Windows. By default, for amd64 (x86_64) architecture.

If you need this version, please go to the repository…

↑ Table of contents

πŸ’¬ Questions for better understanding

  1. Which parameter in the GitHub Actions config is triggering job?
  2. Why we need to define fetch-depth: 0 for actions/checkout action?
  3. What does the -rm-dist argument mean in Run GoReleaser step?

↑ Table of contents

✏️ Exercises for independent execution

  • Repeat everything you have seen in the article, but with your own Golang project. Please, write about your result & link to your shorter website in the comments below!
  • Change the triggering value so that it only works for major versions. Please, read more about software versioning here.

↑ Table of contents

Photos/Images by

  • GoReleaser Action repository & website (links: 1, 2)
  • GitHub repository settings (link)

P.S.

If you want more β€” write a comment below & follow me. Thx! 😘

Posted on by:

koddr profile

Vic ShΓ³stak

@koddr

Hey! πŸ‘‹ I'm founder and full stack web developer (Go, JavaScript, Docker & automation) at True web artisans. Golang lover, UX evangelist, DX philosopher & UI Dreamer with over 12+ years of experience.

Discussion

pic
Editor guide
 

[...] GitHub automatically creates a GITHUB_TOKEN secret to use in your workflow, but I recommend to create your own token [...]

I'm curious why you recommend that?

 

Hi! Thank you for asking.

Now I've reread this place in my article and I don't really understand either :)

Usually, I use several secret variables for various manipulations of the repository. So, I am already used to making a new key.

If you don't need it, you may not follow my recommendation, but I wrote this "as for myself from the past" :D

 

I was looking into this and I noticed that the README for goreleaser-action says that you may need to generate your own token if you want to publish to an external repo (which you'd need to do if you are publishing your project to homebrew, for example).

 

Just awesome detailed instructions, love them
I wish you had this blog when i was studying in uni :D

 

Wow! Glad to hear it. 😘