• Stars
    star
    1,025
  • Rank 44,923 (Top 0.9 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created about 5 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GitHub Actions as CI for Go

GitHub Actions for Go

GitHub Actions includes CI/CD for free for Open Source repositories. This document contains information on making it work well for Go. See them in action:

$ cat .github/workflows/test.yml
on: [push, pull_request]
name: Test
jobs:
  test:
    strategy:
      matrix:
        go-version: [1.20.x, 1.21.x]
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-go@v4
      with:
        go-version: ${{ matrix.go-version }}
    - run: go test ./...

Summary

Each workflow file has a number of jobs, which get run on specified events, and run concurrently with each other. You can have workflow status badges.

Each job runs on a configuration matrix. For example, we can test two major Go versions on three operating systems.

Each job has a number of steps, such as installing Go, or checking out the repository's code.

Note that name fields are optional.

FAQs

How do I set environment variables?

They can be set up via env for an entire workflow, a job, or for each step:

env:
  GOPROXY: "https://proxy.company.com"
jobs:
  [...]

How do I set environment variables at run-time?

You can use environment files to set environment variables or add an element to $PATH. For example:

steps:
- name: Set env vars
  run: |
      echo "CGO_ENABLED=0" >> $GITHUB_ENV
      echo "${HOME}/goroot/bin" >> $GITHUB_PATH

Note that these take effect for future steps in the job.

How do I set up caching between builds?

Since v4, actions/setup-go caches GOCACHE and GOMODCACHE automatically, using go.sum as the cache key. You can turn that off via cache: false, and then you may also use your own custom caching, for example to only keep GOMODCACHE:

- uses: actions/cache@v3
  with:
    path: ~/go/pkg/mod
    key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
    restore-keys: |
      ${{ runner.os }}-go-

See this guide for more details.

How do I run a step conditionally?

You can use if conditionals, using their custom expression language:

- if: github.event_name == 'push' && matrix.os == 'ubuntu-latest'
  run: go run ./endtoend

How do I set up a custom build matrix?

You can include extra matrix jobs, and you can exclude specific matrix jobs.

How do I run multiline scripts?

- name: Series of commands
  run: |
    go test ./...
    go test -race ./...

Should I use two workflows, or two jobs on one workflow?

The biggest difference is the UI; workflow results are shown separately. Grouping jobs in workflows can also be useful if one wants to customize the workflow triggers, or to set up dependencies via needs.

How do I set up a secret environment variable?

Follow these steps to set up the secret in the repo's settings. After adding a secret like FOO_SECRET, use it on a step as follows:

- run: some-command
  env:
    FOO_SECRET: ${{ secrets.FOO_SECRET }}

How do I install private modules?

It's possible to install modules from private GitHub repositories without using your own proxy. You'll need to add a personal access token as a secret environment variable, as well as configure GOPRIVATE.

- name: Configure git for private modules
  env:
    TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
  run: git config --global url."https://YOUR_GITHUB_USERNAME:${TOKEN}@github.com".insteadOf "https://github.com"
env:
  GOPRIVATE: "*.company.com"
jobs:
  [...]

How do I install Linux packages?

Use sudo apt, making sure to only run the step on Linux:

- if: matrix.os == 'ubuntu-latest'
  run: sudo apt update && sudo apt install -y --no-install-recommends mypackage

How do I set up a GOPATH build?

Declare GOPATH and clone inside of it:

jobs:
  test-gopath:
    env:
      GOPATH: ${{ github.workspace }}
      GO111MODULE: off
    defaults:
      run:
        working-directory: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}
    steps:
    - uses: actions/checkout@v3
      with:
        path: ${{ env.GOPATH }}/src/github.com/${{ github.repository }}

Caveats

git config core.autocrlf defaults to true, so be careful about CRLF endings in your plaintext testdata files on Windows. To work around this, set up the following .gitattributes:

* -text

os.TempDir on Windows will contain a short name, since %TEMP% also contains it. Note that case sensitivity doesn't matter, and that os.Open should still work; but some programs not treating short names might break.

> echo %USERPROFILE%
C:\Users\runneradmin
> echo %TEMP%
C:\Users\RUNNER~1\AppData\Local\Temp

More Repositories

1

sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
Go
7,112
star
2

gofumpt

A stricter gofmt
Go
3,184
star
3

xurls

Extract urls from text
Go
1,175
star
4

interfacer

A linter that suggests interface types
Go
690
star
5

unparam

Find unused parameters in Go
Go
527
star
6

gogrep

Search for Go code using syntax trees
Go
477
star
7

fdroidcl

F-Droid desktop client
Go
262
star
8

goreduce

Reduce Go programs
Go
216
star
9

bitw

Minimalist BitWarden client
Go
168
star
10

zstd

Zstandard implementation in Wuffs
C
112
star
11

corpus

A corpus of popular Go modules
Go
106
star
12

dockexec

Run Go tests inside a Docker image
Go
98
star
13

accesspoint

Manage wireless access points in Android (abandoned)
Java
73
star
14

benchinit

Benchmark the init cost of Go packages
Go
72
star
15

pastecat

Pastebin service (abandoned)
Go
36
star
16

git-picked

List merged and cherry-picked branches
Go
29
star
17

winup

Automate a Windows 10 VM setup for coding and testing
Go
21
star
18

unindent

Report code that is unnecessarily indented
Go
19
star
19

android-template

Android app template
Java
16
star
20

dotfiles

Here be dragons
Shell
14
star
21

talks

Collection of slides from talks
Go
9
star
22

go-concurrency-workshop

Go
9
star
23

editorconfig

EditorConfig support in Go
Go
8
star
24

lint

Common interfaces for Go code checkers
Go
6
star
25

macfuzzer

Android MAC changer and randomizer (abandoned)
Java
6
star
26

gexf

GEXF file format implementation
Go
3
star
27

mvdan.cc

Personal website
HTML
3
star
28

playtyk

This repo is a temporary home for a hack.
Go
3
star
29

responsefile

Support for response files in Go, to bypass argument length limits
Go
2
star
30

tor

Consensus diffs Tor GSoC project
C
1
star
31

route

portable Go package to obtain simple routing information
Go
1
star
32

nowt

Nothing extraordinary here
Go
1
star
33

basedir

Platform-specific base directories
Go
1
star