Skip to content

Instantly share code, notes, and snippets.

@elithrar
Last active September 13, 2020 21:29
Show Gist options
  • Save elithrar/4fa799c66b2c9932ac33f450f0787a58 to your computer and use it in GitHub Desktop.
Save elithrar/4fa799c66b2c9932ac33f450f0787a58 to your computer and use it in GitHub Desktop.
CircleCI vs. TravisCI for gorilla/mux - comparing maintainability, verbosity, DRY, etc. Ref: https://twitter.com/elithrar/status/1140342082768261120 & https://blog.questionable.services/article/building-go-projects-on-circle-ci/
language: go
matrix:
include:
- go: 1.7.x
- go: 1.8.x
- go: 1.9.x
- go: 1.10.x
- go: 1.11.x
- go: 1.x
env: LATEST=true
- go: tip
allow_failures:
- go: tip
install:
- # Skip
script:
- go get -t -v ./...
- diff -u <(echo -n) <(gofmt -d .)
- if [[ "$LATEST" = true ]]; then go vet .; fi
- go test -v -race ./...
version: 2.1
jobs:
"test":
parameters:
version:
type: string
default: "latest"
golint:
type: boolean
default: true
modules:
type: boolean
default: true
goproxy:
type: string
default: ""
docker:
- image: "circleci/golang:<< parameters.version >>"
working_directory: /go/src/github.com/gorilla/mux
environment:
GO111MODULE: "on"
GOPROXY: "<< parameters.goproxy >>"
steps:
- checkout
- run:
name: "Print the Go version"
command: >
go version
- run:
name: "Fetch dependencies"
command: >
if [[ << parameters.modules >> = true ]]; then
go mod download
export GO111MODULE=on
else
go get -v ./...
fi
# Only run gofmt, vet & lint against the latest Go version
- run:
name: "Run golint"
command: >
if [ << parameters.version >> = "latest" ] && [ << parameters.golint >> = true ]; then
go get -u golang.org/x/lint/golint
golint ./...
fi
- run:
name: "Run gofmt"
command: >
if [[ << parameters.version >> = "latest" ]]; then
diff -u <(echo -n) <(gofmt -d -e .)
fi
- run:
name: "Run go vet"
command: >
if [[ << parameters.version >> = "latest" ]]; then
go vet -v ./...
fi
- run:
name: "Run go test (+ race detector)"
command: >
go test -v -race ./...
workflows:
tests:
jobs:
- test:
matrix:
parameters:
version: ["latest", "1.15", "1.14", "1.13", "1.12", "1.11"]
@elithrar
Copy link
Author

elithrar commented Jun 17, 2019

A variant that uses Parameters in v2.1 to re-use more code.

  • Pros: only need to write the version once (important, as version numbers tend to be error prone, and CI configs are hard to test on their own), overall readability is maintained
  • Cons: every test is called "test-$n" in the workflow UI, which makes it harder to debug workflow and far less clear to someone submitting a PR (and seeing the status update).

image

version: "2.1"

jobs:
  # Base test configuration for Go library tests Each distinct version should
  # inherit this base, and override (at least) the container image used.
  test:
    parameters:
      v:
        type: string
        default: "latest"
      latest:
        type: boolean
        default: false
    docker:
      - image: "circleci/golang:<< parameters.v >>"
    working_directory: /go/src/github.com/gorilla/mux
    steps:
      - checkout
      - run: go version
      - run: go get -t -v ./...
      - run: diff -u <(echo -n) <(gofmt -d .)
      - run: if [[ "$LATEST" = true ]]; then go vet -v .; fi
      - run: go test -v -race ./...

workflows:
  workflow:
    jobs:
      - test:
          v: "latest"
          latest: true
      - test:
          v: "1.12"
      - test:
          v: "1.11"
      - test:
          v: "1.10"
      - test:
          v: "1.9"
      - test:
          v: "1.8"
      - test:
          v: "1.7"

@elithrar
Copy link
Author

elithrar commented Sep 13, 2020

Updated to use matrix in CircleCI:

version: 2.1

jobs:
  "test":
    parameters:
      version:
        type: string
        default: "latest"
      golint:
        type: boolean
        default: true
      modules:
        type: boolean
        default: true
      goproxy:
        type: string
        default: ""
    docker:
      - image: "circleci/golang:<< parameters.version >>"
    working_directory: /go/src/github.com/gorilla/mux
    environment:
      GO111MODULE: "on"
      GOPROXY: "<< parameters.goproxy >>"
    steps:
      - checkout
      - run:
          name: "Print the Go version"
          command: >
            go version
      - run:
          name: "Fetch dependencies"
          command: >
            if [[ << parameters.modules >> = true ]]; then
              go mod download
              export GO111MODULE=on
            else
              go get -v ./...
            fi
      # Only run gofmt, vet & lint against the latest Go version
      - run:
          name: "Run golint"
          command: >
            if [ << parameters.version >> = "latest" ] && [ << parameters.golint >> = true ]; then
              go get -u golang.org/x/lint/golint
              golint ./...
            fi
      - run:
          name: "Run gofmt"
          command: >
            if [[ << parameters.version >> = "latest" ]]; then
              diff -u <(echo -n) <(gofmt -d -e .)
            fi
      - run:
          name: "Run go vet"
          command: >
            if [[ << parameters.version >> = "latest" ]]; then
              go vet -v ./...
            fi
      - run:
          name: "Run go test (+ race detector)"
          command: >
            go test -v -race ./...

workflows:
  tests:
    jobs:
      - test:
          matrix:
            parameters:
              version: ["latest", "1.15", "1.14", "1.13", "1.12", "1.11"]

2E93CD88-6B0A-4DEE-8385-3B319773D79F

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment