Skip to content

Commit

Permalink
chore: Setup CI & release pipeline
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Toh <[email protected]>
  • Loading branch information
tohjustin committed Sep 20, 2021
1 parent 296c41c commit 1623ad7
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 4 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# This workflow is triggered on push or pull request for the master branch.
# It runs tests and various checks to validate that the proposed changes
# will not introduce any regression after merging the code to the master branch.
name: Build
on:
push:
branches:
- master
paths-ignore:
- '*.md'
pull_request:
branches:
- master
paths-ignore:
- '*.md'
env:
GO_VERSION: "1.16"
jobs:
lint:
name: Run linter
runs-on: ubuntu-18.04
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run linter
run: make lint
test:
name: Run tests
runs-on: ubuntu-18.04
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
run: make test
release-snapshot:
name: Release unversioned snapshot
needs:
- lint
- test
runs-on: ubuntu-18.04
permissions:
contents: read
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Release
run: make release-snapshot
75 changes: 75 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This workflow is triggered on new tags.
# It runs tests to validate that the code is working before publishing a new
# version for plugin "lineage" in krew-index.
name: Release
on:
push:
tags:
- 'v*.*.*'
env:
GO_VERSION: "1.16"
jobs:
lint:
name: Run linter
runs-on: ubuntu-18.04
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run linter
run: make lint
test:
name: Run tests
runs-on: ubuntu-18.04
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
run: make test
release:
name: Release
runs-on: ubuntu-18.04
permissions:
contents: write
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Release
run: make release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Output of the GoReleaser tool
dist/

# Binaries for make targets, downloaded via fetch.sh script
bin/

# GoLand IDEA
/.idea/
*.iml
Expand All @@ -25,6 +31,3 @@
# Miscellaneous files
*.sw[op]
*.DS_Store

# Dependency directories (remove the comment below to include it)
# vendor/
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
deadline: 1m
deadline: 5m
linters:
enable-all: true
disable:
Expand Down
48 changes: 48 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
project_name: kubectl-lineage
before:
hooks:
- go mod tidy
builds:
- binary: kubectl-lineage
main: ./cmd/kubectl-lineage
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
ignore:
- goos: windows
goarch: arm64
env:
- CGO_ENABLED=0
ldflags:
- -s -w
- -X github.com/tohjustin/kubectl-lineage/internal/version.gitMajor={{ .Env.GIT_VERSION_MAJOR }}
- -X github.com/tohjustin/kubectl-lineage/internal/version.gitMinor={{ .Env.GIT_VERSION_MINOR }}
- -X github.com/tohjustin/kubectl-lineage/internal/version.gitVersion={{ .Env.GIT_VERSION }}
- -X github.com/tohjustin/kubectl-lineage/internal/version.gitCommit={{ .Env.GIT_COMMIT }}
- -X github.com/tohjustin/kubectl-lineage/internal/version.gitTreeState={{ .Env.GIT_TREE_STATE }}
- -X github.com/tohjustin/kubectl-lineage/internal/version.buildDate={{ .Env.BUILD_DATE }}
archives:
- files:
- LICENSE.md
- README.md
format_overrides:
- goos: windows
format: zip
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
wrap_in_directory: false
checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- "^chore:"
- "^docs:"
- "^refactor:"
- "^test:"
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
SHELL:=/bin/bash

GO_VERSION = "1.16"
GOLANGCI_LINT_VERSION = "1.41.1"
GORELEASER_VERSION = "0.179.0"

export BUILD_DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
export GIT_COMMIT = $(shell git rev-parse HEAD)
export GIT_TREE_STATE = $(shell if [ -z "`git status --porcelain`" ]; then echo "clean" ; else echo "dirty"; fi)
export GIT_VERSION = $(shell git describe --tags --always | grep -E '^v\d+\.\d+\.\d+(-.*)?')
export GIT_VERSION_MAJOR = $(shell if [[ "${GIT_VERSION}" ]]; then echo ${GIT_VERSION} | cut -d 'v' -f 2 | cut -d "." -f 1 ; fi)
export GIT_VERSION_MINOR = $(shell if [[ "${GIT_VERSION}" ]]; then echo ${GIT_VERSION} | cut -d 'v' -f 2 | cut -d "." -f 2 ; fi)
export CGO_ENABLED = 1

REPO = $(shell go list -m)
GO_BUILD_ARGS = \
-gcflags "all=-trimpath=$(shell dirname $(shell pwd))" \
-asmflags "all=-trimpath=$(shell dirname $(shell pwd))" \
-ldflags " \
-s \
-w \
-X '$(REPO)/internal/version.buildDate=$(BUILD_DATE)' \
-X '$(REPO)/internal/version.gitCommit=$(GIT_COMMIT)' \
-X '$(REPO)/internal/version.gitTreeState=$(GIT_TREE_STATE)' \
-X '$(REPO)/internal/version.gitVersion=$(GIT_VERSION)' \
-X '$(REPO)/internal/version.gitVersionMajor=$(GIT_VERSION_MAJOR)' \
-X '$(REPO)/internal/version.gitVersionMinor=$(GIT_VERSION_MINOR)' \
" \

.PHONY: all
all: install

.PHONY: clean
clean:
rm -rf bin dist

.PHONY: lint
lint:
source ./scripts/fetch.sh; fetch golangci-lint $(GOLANGCI_LINT_VERSION) && ./bin/golangci-lint run

.PHONY: test
test:
go test ./...

.PHONY: build
build:
go build $(GO_BUILD_ARGS) -o bin/kubectl-lineage ./cmd/kubectl-lineage

.PHONY: install
install: build
install bin/kubectl-lineage $(shell go env GOPATH)/bin

.PHONY: release
RELEASE_ARGS?=release --rm-dist
release:
source ./scripts/fetch.sh; fetch goreleaser $(GORELEASER_VERSION) && ./bin/goreleaser $(RELEASE_ARGS)

.PHONY: release-snapshot
RELEASE_SNAPSHOT_ARGS?=release --rm-dist --skip-publish --snapshot
release-snapshot:
source ./scripts/fetch.sh; fetch goreleaser $(GORELEASER_VERSION) && ./bin/goreleaser $(RELEASE_SNAPSHOT_ARGS)
30 changes: 30 additions & 0 deletions scripts/fetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

ROOT=$(git rev-parse --show-toplevel)

fetch() {
local tool=$1; shift
local ver=$1; shift

local ver_cmd=""
local tool_fetch_cmd=""
case "$tool" in
"golangci-lint")
ver_cmd="${ROOT}/bin/golangci-lint --version 2>/dev/null | cut -d\" \" -f4"
fetch_cmd="curl -sSfL \"https://raw.githubusercontent.com/golangci/golangci-lint/v${ver}/install.sh\" | sh -s -- -b \"${ROOT}/bin\" \"v${ver}\""
;;
"goreleaser")
ver_cmd="${ROOT}/bin/goreleaser --version 2>/dev/null | grep 'goreleaser version' | cut -d' ' -f3"
fetch_cmd="curl -sSfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- -b \"${ROOT}/bin\" -d \"v${ver}\""
;;
*)
echo "unknown tool $tool"
return 1
;;
esac

if [[ "${ver}" != "$(eval ${ver_cmd})" ]]; then
echo "${tool} missing or not version '${ver}', downloading..."
eval ${fetch_cmd}
fi
}

0 comments on commit 1623ad7

Please sign in to comment.