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
5
on:
  push:
    tags:
      - "v*"
  workflow_dispatch:

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
8
jobs:
  release:
    name: Release
    strategy:
      matrix:
        os: [ubuntu-latest]
        go: ["1.18"]
    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)"          
GoReleaser

GoReleaser1 take care of publishing artifacts into the release.

1
2
3
4
5
6
7
8
9
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          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:

go.yml

 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
name: go

on:
  push:
    tags:
      - "*"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  release:
    name: Release
    strategy:
      matrix:
        os: [ubuntu-latest]
        go: ["1.18"]
    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: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          distribution: goreleaser
          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.

John Siu

Update: 2022-05-10
comments powered by Disqus