Skip to content

Commit

Permalink
ci: add default CI setup for Go library
Browse files Browse the repository at this point in the history
Makefile and GitHub Actions pipeline with build, lint, test and
releases.
  • Loading branch information
odsod committed Dec 26, 2020
1 parent 0d391cb commit 31e4812
Show file tree
Hide file tree
Showing 21 changed files with 272 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @odsod
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
52 changes: 52 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- "*"

jobs:
make:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ^1.15

- name: Setup Node
uses: actions/setup-node@v2-beta
with:
node-version: 12

- name: Make
run: make

release:
needs: [make]
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ^1.15

- name: Setup Node
uses: actions/setup-node@v2-beta
with:
node-version: 12

- name: Run semantic-release
run: make semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
tools/*/*/
node_modules/
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
run:
timeout: 5m

linters:
enable-all: true
disable:
- dupl # allow duplication
- funlen # allow long functions
- gomnd # allow some magic numbers
- wsl # unwanted amount of whitespace
- godox # allow TODOs
- interfacer # deprecated by the author for having too many false positives
- gocognit # allow higher cognitive complexity
- testpackage # unwanted convention
- nestif # allow deep nesting
- unparam # allow constant parameters
- goerr113 # allow "dynamic" errors
- dogsled # allow blank identifiers
- gocyclo # allow complex functions
- nlreturn # allow return/break without whitespace
- exhaustivestruct # don't require exhaustive fields in struct initializers
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SHELL := /bin/bash

all: \
commitlint \
prettier-markdown \
go-lint \
go-review \
go-test \
go-mod-tidy \
git-verify-nodiff

include tools/commitlint/rules.mk
include tools/git-verify-nodiff/rules.mk
include tools/golangci-lint/rules.mk
include tools/goreview/rules.mk
include tools/prettier/rules.mk
include tools/semantic-release/rules.mk

.PHONY: go-mod-tidy
go-mod-tidy:
$(info [$@] tidying Go module files...)
@go mod tidy -v

.PHONY: go-test
go-test:
$(info [$@] running Go tests...)
@go test -count 1 -cover -race ./...
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# AIP Go

Go SDK for implementing [Google API Improvement Proposals][google-aip] (AIP).

[google-aip]: https://google.aip.dev/
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package aip provides primitives for implementing API Improvement Proposals (AIP).
package aip
8 changes: 8 additions & 0 deletions tools/commitlint/.commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
// Treat as warning until Dependabot supports commitlint.
// https://github.com/dependabot/dependabot-core/issues/2445
"body-max-line-length": [1, "always", 100],
}
};
6 changes: 6 additions & 0 deletions tools/commitlint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0"
}
}
16 changes: 16 additions & 0 deletions tools/commitlint/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
commitlint_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
commitlint := $(commitlint_cwd)/node_modules/.bin/commitlint

$(commitlint): $(commitlint_cwd)/package.json
$(info [commitlint] installing package...)
@cd $(commitlint_cwd) && npm install --no-save --no-audit &> /dev/null
@touch $@

.PHONY: commitlint
commitlint: $(commitlint_cwd)/.commitlintrc.js $(commitlint)
$(info [$@] linting commit messages...)
@git fetch --tags
@NODE_PATH=$(commitlint_cwd)/node_modules $(commitlint) \
--config $< \
--from origin/master \
--to HEAD
9 changes: 9 additions & 0 deletions tools/git-verify-nodiff/git-verify-nodiff.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -euo pipefail

if [[ -n $(git status --porcelain) ]]; then
echo "Staging area is dirty, please add all files created by the build to .gitignore"
git diff --patch
exit 1
fi
7 changes: 7 additions & 0 deletions tools/git-verify-nodiff/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
git_verify_nodiff_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
git_verify_nodiff := $(git_verify_nodiff_cwd)/git-verify-nodiff.bash

.PHONY: git-verify-nodiff
git-verify-nodiff:
$(info [$@] verifying that git has no diff...)
@$(git_verify_nodiff)
24 changes: 24 additions & 0 deletions tools/golangci-lint/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
golangci_lint_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
golangci_lint_version := 1.33.0
golangci_lint := $(golangci_lint_cwd)/$(golangci_lint_version)/golangci-lint

ifeq ($(shell uname),Linux)
golangci_lint_archive_url := https://github.com/golangci/golangci-lint/releases/download/v${golangci_lint_version}/golangci-lint-${golangci_lint_version}-linux-amd64.tar.gz
else ifeq ($(shell uname),Darwin)
golangci_lint_archive_url := https://github.com/golangci/golangci-lint/releases/download/v${golangci_lint_version}/golangci-lint-${golangci_lint_version}-darwin-amd64.tar.gz
else
$(error unsupported OS: $(shell uname))
endif

$(golangci_lint):
$(info [golangci-lint] fetching version $(golangci_lint) binary...)
@mkdir -p $(dir $@)
@curl -sSL $(golangci_lint_archive_url) -o - | \
tar -xz --directory $(dir $@) --strip-components 1
@chmod +x $@
@touch $@

.PHONY: go-lint
go-lint: $(golangci_lint)
$(info [$@] linting Go code...)
@$(golangci_lint) run
18 changes: 18 additions & 0 deletions tools/goreview/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
goreview_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
goreview_version := 0.16.0
goreview := $(goreview_cwd)/$(goreview_version)/goreview

goreview_archive_url := https://github.com/einride/goreview/releases/download/v$(goreview_version)/goreview_$(goreview_version)_$(shell uname)_$(shell uname -m).tar.gz

$(goreview): $(goreview_cwd)/rules.mk
$(info [go-review] fetching $(goreview_version) binary...)
@mkdir -p $(dir $@)
@curl -sSL $(goreview_archive_url) -o - | tar -xz --directory $(dir $@)
@chmod +x $@
@touch $@

# go-review: review Go code for Einride-specific conventions
.PHONY: go-review
go-review: $(goreview)
$(info [$@] reviewing Go code for Einride-specific conventions...)
@$(goreview) -c 1 ./...
5 changes: 5 additions & 0 deletions tools/prettier/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# paths are relative to this file

../
../vendor
../node_modules
5 changes: 5 additions & 0 deletions tools/prettier/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"prettier": "^2.2.1"
}
}
17 changes: 17 additions & 0 deletions tools/prettier/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
prettier_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
prettier := $(prettier_cwd)/node_modules/.bin/prettier

$(prettier): $(prettier_cwd)/package.json
$(info [prettier] installing...)
@cd $(prettier_cwd) && npm install --no-save --no-audit &> /dev/null
@touch $@

.PHONY: prettier-markdown
prettier-markdown: $(prettier_cwd)/.prettierignore $(prettier)
$(info [$@] formatting Markdown files...)
@$(prettier) \
--loglevel warn \
--ignore-path $< \
--parser markdown \
--prose-wrap always \
--write **/*.md
19 changes: 19 additions & 0 deletions tools/semantic-release/.releaserc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins:
- - "@semantic-release/commit-analyzer"
- preset: "conventionalcommits"
releaseRules:
# Given Go v2+ conventions we disable major releases on
# breaking changes and leave it up to the developer
# to make major releases
- breaking: true
release: "minor"
- "@semantic-release/release-notes-generator"
- "@semantic-release/github"

branches: ["master"]
# github plugin is the only one running this step and we're not interested
# in its updates to PR and issues
success: false
# github plugin is the only one running this step and we're not interested
# the issues it creates due to failed releases
fail: false
8 changes: 8 additions & 0 deletions tools/semantic-release/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"devDependencies": {
"semantic-release": "^17.3.0",
"@semantic-release/github": "^7.2.0",
"@semantic-release/release-notes-generator": "^9.0.1",
"conventional-changelog-conventionalcommits": "^4.5.0"
}
}
12 changes: 12 additions & 0 deletions tools/semantic-release/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
semantic_release_cwd := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
semantic_release := $(semantic_release_cwd)/node_modules/.bin/semantic-release

$(semantic_release): $(semantic_release_cwd)/package.json
$(info [semantic-release] installing packages...)
@cd $(semantic_release_cwd) && npm install --no-save --no-audit --ignore-scripts &> /dev/null
@touch $@

.PHONY: semantic-release
semantic-release: $(semantic_release_cwd)/.releaserc.yaml $(semantic_release)
$(info [$@] creating release...)
@cd $(semantic_release_cwd) && $(semantic_release)

0 comments on commit 31e4812

Please sign in to comment.