DEV Community

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.15
      -
        name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

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:
      - '*'
Enter fullscreen mode Exit fullscreen mode

Or with a condition on GoReleaser step:

      -
        
Enter fullscreen mode Exit fullscreen mode

↑ 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! 😉")
}
Enter fullscreen mode Exit fullscreen mode

And the repository structure will be as simple as that:

.
├── .github
│   └── workflows
│       └── release_build.yml  # <-- GitHub Action
└── main.go                    # <-- main function
Enter fullscreen mode Exit fullscreen mode

↑ 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 }}
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

↑ 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

✨ A powerful CLI for the Create Go App project. Create a new production-ready project with backend, frontend and deploy automation by running one CLI command!


Create Go App CLI

Create a new production-ready project with backend (Golang), frontend (JavaScript, TypeScript)
and deploy automation (Ansible, Docker) by running one CLI command.

Focus on writing code and thinking of business-logic! The CLI will take care of the rest.

cli version go version go cover go report license

⚡️ Quick start

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

Installation is done by using the go install command and rename installed binary in $GOPATH/bin:

go install -ldflags="-s -w" github.com/create-go-app/cli && mv $GOPATH/bin/cli $GOPATH/bin/cgapp
Enter fullscreen mode Exit fullscreen mode

Also, macOS and GNU/Linux users available way to install via Homebrew:

# Tap a new formula:
brew tap create-go-app/cli
# Installation:
brew install create-go-app/cli/cgapp
Enter fullscreen mode Exit fullscreen mode

Let's create a new project via interactive console UI (or CUI for short) into current folder:

cgapp create
Enter fullscreen mode Exit fullscreen mode

Okay, it works! Now, you can run this project on your local machine or deploy to a remote server

↑ 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! 😘

Discussion (5)

pic
Editor guide
Collapse
arnested profile image
Arne Jørgensen

[...] 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?

Collapse
koddr profile image
Vic Shóstak Author

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

Collapse
bryanbraun profile image
Bryan Braun

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).

Collapse
mariusty profile image
mariusty

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

Collapse
koddr profile image
Vic Shóstak Author

Wow! Glad to hear it. 😘