John Siu Blog

Tech - Business Tool, Personal Toys

Github Actions for Golang

☰ Table of Content

A simple Github actions with GoReleaser.

Setup

Create following in swift repo to use Github Action:

1
2
mkdir .github/workflows
touch .github/workflows/go.yml

Following is the dir structure:

1
2
3
.github
└── workflows
    └── go.yml

go.yml is the workflow/actions configuration.

go.yml

The go.yml contain 3 main parts: name, on and jobs.

Name

name is the name of this workflow:

1
name: Go

On

on is the trigger condition of this workflow. In this case, the workflow will be triggered whenever a new tag is pushed to Github.

1
2
3
4
on:
  push:
    tags:
      - "*"

Jobs

jobs consists of different job section. Only one job section, release, in this case, which in turn contains strategy, runs-on, then the steps section.

1
2
3
4
5
6
7
jobs:
  release:
    strategy:
      matrix:
        os: [ubuntu-latest]
        go: ["1.16"]
    runs-on: ${{ matrix.os }}

strategy and runs-on here allow for multiple platform building.

Steps

steps contain multiple step sections, each with their own name.

Install Go

Install Go will install go version specified in matrix above for building the repository.

1
2
3
4
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}
Checkout

Checkout is standard across all workflow. This tell github compiling environment to checkout the repository.

1
2
      - name: Checkout
        uses: actions/checkout@v2
Unshallow

Unshallow the repo so GoReleaser can generate the changelog properly.

1
2
      - name: Unshallow
        run: git fetch --prune --unshallow
Vars

Prepare variables version_tag and go_cache for steps follow.

1
2
3
4
5
      - name: Vars
        id: vars
        run: |
          echo "::set-output name=version_tag::${GITHUB_REF/refs\/tags\//}"
          echo "::set-output name=go_cache::$(go env GOCACHE)"          
Cache

Caching dependencies and build outputs1.

1
2
3
4
5
6
7
      - name: Cache the build cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.vars.outputs.go_cache }}
          key: ${{ runner.os }}-go${{ matrix.go }}-release-${{ hashFiles('**/go.sum') }}
          restore-keys: |
                        ${{ runner.os }}-go${{ matrix.go }}-release
GoReleaser

GoReleaser2 take care of publishing artifacts into the release.

1
2
3
4
5
6
7
8
      - name: GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAG: ${{ steps.vars.outputs.version_tag }}

Complete Listing

Following is the complete listing:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
name: go

on:
  push:
    tags:
      - "*"
  repository_dispatch:
    types: manual

jobs:
  release:
    strategy:
      matrix:
        os: [ubuntu-latest]
        go: ["1.16"]
    runs-on: ${{ matrix.os }}

    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}

      - name: Checkout
        uses: actions/checkout@v2

      - name: Unshallow
        run: git fetch --prune --unshallow

      - name: Vars
        id: vars
        run: |
          echo "::set-output name=version_tag::${GITHUB_REF/refs\/tags\//}"
          echo "::set-output name=go_cache::$(go env GOCACHE)"          

      - name: Cache the build cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.vars.outputs.go_cache }}
          key: ${{ runner.os }}-go${{ matrix.go }}-release-${{ hashFiles('**/go.sum') }}
          restore-keys: |
                        ${{ runner.os }}-go${{ matrix.go }}-release

      - name: GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAG: ${{ steps.vars.outputs.version_tag }}

go-png2ico is Golang command line utility for creating ICO file from PNG files. It used the above workflow on GitHub.


  1. GitHub action/cache ↩︎

  2. GoReleaser ↩︎

John Siu

Update: 2021-04-15
comments powered by Disqus