Giter Club home page Giter Club logo

github-actions-golang's Introduction

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. You can also directly used the token provided by GitHub in the workflow. You can define anything as username in the URL, it is not taken into account by GitHub.

- name: Configure git for private modules
  env:
    TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
  run: git config --global url."https://user:${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 }}

Quick links

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

github-actions-golang's People

Contributors

amwolff avatar antham avatar bouk avatar ferhatelmas avatar filosottile avatar ghostsquad avatar mvdan avatar pellared avatar wayneashleyberry avatar wusatosi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

github-actions-golang's Issues

Github Action Failing even tho the Tests passed

When I run this Github Action:

on: [push, pull_request]
name: Test
jobs:
  test:
    strategy:
      matrix:
        go-version: [1.16.x]
        os: [ubuntu-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go-version }}
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Setup Redis
        # You may pin to the exact commit or the version.
        # uses: zhulik/redis-action@765216a54a1114f109ce90a1de4775080be16ea5
        uses: zhulik/[email protected]
      - name: Setup PostgreSQL
        # You may pin to the exact commit or the version.
        # uses: Harmon758/postgresql-action@0be19fa37850b22cb4c9bbf28a03abbf44abd863
        uses: Harmon758/[email protected]
        with:
          # POSTGRES_DB - name for the default database that is created
          postgresql db: shoppinglistboom
          # POSTGRES_USER - create the specified user with superuser power
          postgresql user: shoppinglist
          # POSTGRES_PASSWORD - superuser password
          postgresql password: postgres
      - name: Build
        working-directory: ./backend
        run: go build ./...
      - name: Test
        working-directory: ./backend
        run: go test ./...
        env:
          DB_HOST: localhost
          DB_USER: shoppinglist
          DB_DATABASE: shoppinglistboom
          DB_PASSWORD: postgres
          REDIS_HOST: localhost
          REDIS_PORT: 6379
          DATABASE_DSN: host=localhost user=shoppinglist password=postgres dbname=shoppinglistboom sslmode=disable
          TESTING: true

it fails every time due to the Tests. But when I look at the logs it says that the Tests passed.
grafik

It gives me the exit code 2 but I dont know what to do with that.

Cache keys

This is an awesome resource, thank you!

I noticed you use ${{ hashFiles('**/go.sum') }} as a cache key for the module cache. I am not sure that does what it is intended for, since the go.sum file does not contain a hash of the module itself, and it might be missing from a module with no dependencies. If hashFiles includes the file names in the key, you could just use the go.mod files instead, but even that would exclude modules that did not update.

How to use shields with github actions ?

I have some projects using travis and this allowed me to have shields when the project passed.

How can we do this with github actions ?

BTW, thank you very much for your helpful project.

Warn about Pwsh quoting of Go commands arguments on Windows

I had the bad experience that this doesn't work on Windows:

      - name: Run coverage
        run: go test -v -race -coverprofile=coverage.out -covermode=atomic ./...

Output:

no required module provides package .out; to add it:
	go get .out
Error: Process completed with exit code 1.

But this works:

      - name: Run coverage
        run: go test -v -race -coverprofile coverage.out -covermode atomic ./...

The only difference is that = is replaced with space.

I suspect this is a PowerShell quoting issue.

Private modules checkout failed with actions/checkout@v2

After configuring the ci.yml to this:

env:
  GOPRIVATE: "github.com/company"
jobs:
.....
  - name: Check out code into the Go module directory
     uses: actions/checkout@v2

  - 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"

I am still saw following error:

go: github.com/company/[email protected]: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /home/runner/go/pkg/mod/cache/vcs/4902ad5423d70bf8a819e419fc1d6e0667f5a8f1542bd02ff6253926ac8fcb35: exit status 128:
	remote: Repository not found.
	fatal: repository 'https://github.com/company/repo/' not found
Error: Process completed with exit code 1.

If I change uses: actions/checkout@v2 to uses: actions/checkout@v1 seems solve the issue

Is there additional setup required for checkout@v2?

Adding GOBIN to PATH

First, I just wanted to say, huge thanks for this repo/README, it helped me a ton. I've been working on setting up github actions for Caddy.

In your README, you note:

The setup-go action doesn't set PATH, so currently it's not possible to go install a program and run it directly. Until that's fixed, consider absolute paths like $(go env GOPATH)/bin/program.

I just wanted to point out that I figured out that you can just do this on any step after setup-go, pretty much entirely smoothes that issue over:

echo "::add-path::$(go env GOPATH)/bin"

Testing for Go v1 apps without go mod

Is it possible to test for Go v1 apps that's not having/using go mod?

This is what I'm getting --
https://github.com/suntong/shuttlebot/runs/2701444155?check_suite_focus=true

Run go test ./...
main.go:16:2: cannot find package "github.com/caarlos0/env" in any of:
	/opt/hostedtoolcache/go/1.15.12/x64/src/github.com/caarlos0/env (from $GOROOT)
	/home/runner/go/src/github.com/caarlos0/env (from $GOPATH)
logging.go:8:2: cannot find package "github.com/go-kit/kit/log" in any of:
	/opt/hostedtoolcache/go/1.15.12/x64/src/github.com/go-kit/kit/log (from $GOROOT)
	/home/runner/go/src/github.com/go-kit/kit/log (from $GOPATH)
. . .

Would it be possible that I set my own GOPATH and do go get myself?

$HOME/.cache/go-build caching

The readme says for How do I set up caching between builds? the path can be ~/go/pkg/mod.
However it seems that's only for source files to be cached.

If you are like me and you do cross-compilation of binaries to 10+ architectures, you probably need to cache ~/.cache/go-build/ directory as well. Otherwise you're waiting for all Go stdlibs (and deps cached in ~/go/pkg/mod) to recompile ––which is the majority of the time spent.

Thoughts?

GOPATH: ${{ runner.workspace }} Do not work with actions

I used your template for set GOPATH env variable, but there is an error.

- Your workflow file was invalid: The pipeline is not valid. .github/workflows/go.yml (Line: 9, Col: 15): Unrecognized named-value: 'runner'. Located at position 1 within expression: runner.workspace,.github/workflows/go.yml (Line: 26, Col: 7): Unexpected value 'with'

My line 9 has:
GOPATH: ${{ runner.workspace }}

Caching test results across action runs

I've found this repo very helpful, thank you! I've noticed that using the actions-cache as demonstrated in the repo doesn't result in tests being cached. I know on my local machine when I rerun tests for packages that haven't changed the results are cached, but when I run on GitHub Actions even with all the cache directories setup the tests aren't cached.

It'd help speed up tests for a lot of Go projects if we could figure that out.

Private modules

The private modules section mentions how to make the repositories reachable, but not how to bypass the proxy. Shouldn't it include a GOPRIVATE env?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.