Skip to content

Commit

Permalink
update build system and lint
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Sep 18, 2022
1 parent 5ab2aef commit f459be1
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 41 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/test
/vendor
/azure-scheduledevents-exporter
/release-assets
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Run Gosec Security Scanner
uses: securego/gosec@master
- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
args: ./...
swap-size-gb: 12

- name: Run Golangci lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: -E exportloopref,gofmt --timeout=30m
args: --print-resources-usage

- name: Docker meta
id: docker_meta
Expand All @@ -39,6 +39,6 @@ jobs:
context: .
file: ./Dockerfile
push: false
platforms: linux/amd64,linux/arm,linux/arm64
platforms: linux/amd64,linux/arm64
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
34 changes: 34 additions & 0 deletions .github/workflows/release-assets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Release: assets"

on:
release:
types: [created]

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
swap-size-gb: 12

- uses: actions/setup-go@v2
with:
go-version: '1.19'
check-latest: true

- name: Build
run: |
make release-assets
- name: Upload assets to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ./release-assets/*
tag: ${{ github.ref }}
overwrite: true
file_glob: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ on:
tags:
- '*.*.*'


jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Run Gosec Security Scanner
uses: securego/gosec@master
- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
args: ./...
swap-size-gb: 12

- name: Run Golangci lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: -E exportloopref,gofmt --timeout=30m
args: --print-resources-usage

build:
needs: lint
Expand All @@ -47,6 +46,11 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
swap-size-gb: 12

- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v4
Expand Down Expand Up @@ -82,7 +86,7 @@ jobs:
with:
context: .
file: ./${{ matrix.Dockerfile }}
platforms: linux/amd64,linux/arm,linux/arm64,linux/ppc64le
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Run Gosec Security Scanner
uses: securego/gosec@master
- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
args: ./...
swap-size-gb: 12

- name: Run Golangci lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
args: -E exportloopref,gofmt --timeout=30m
args: --print-resources-usage

build:
needs: lint
Expand All @@ -43,6 +43,11 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
swap-size-gb: 12

- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v4
Expand Down Expand Up @@ -78,7 +83,7 @@ jobs:
with:
context: .
file: ./${{ matrix.Dockerfile }}
platforms: linux/amd64,linux/arm,linux/arm64,linux/ppc64le
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
/azure-scheduledevents-manager
/release-assets
26 changes: 26 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
run:
timeout: 120m

linters:
enable:
- asciicheck
- bidichk
- bodyclose
- errorlint
- exportloopref
- gofmt
- goimports
- gosec

linters-settings:
gosec:
excludes: []
confidence: low
config:
global:
audit: true

issues: {}

output:
sort-results: true
78 changes: 57 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,90 @@ LDFLAGS := -X "main.gitTag=$(GIT_TAG)" -X "main.gitCommit=$(GIT_COMMIT)" -ext

FIRST_GOPATH := $(firstword $(subst :, ,$(shell go env GOPATH)))
GOLANGCI_LINT_BIN := $(FIRST_GOPATH)/bin/golangci-lint
GOSEC_BIN := $(FIRST_GOPATH)/bin/gosec

.PHONY: all
all: build
all: vendor build

.PHONY: clean
clean:
git clean -Xfd .

.PHONY: build
build:
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -a -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) .
#######################################
# builds
#######################################

.PHONY: vendor
vendor:
go mod tidy
go mod vendor
go mod verify

.PHONY: build-all
build-all:
GOOS=linux GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME)' .
GOOS=darwin GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME).darwin' .
GOOS=windows GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o '$(PROJECT_NAME).exe' .

.PHONY: build
build:
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags '$(LDFLAGS)' -o $(PROJECT_NAME) .

.PHONY: image
image: build
image: image
docker build -t $(PROJECT_NAME):$(GIT_TAG) .

.PHONY: build-push-development
build-push-development:
docker buildx create --use
docker buildx build -t webdevops/$(PROJECT_NAME):development --platform linux/amd64,linux/arm,linux/arm64 --push .

.PHONY: test
test:
go test ./...
#######################################
# quality checks
#######################################

.PHONY: dependencies
dependencies:
go mod vendor
.PHONY: check
check: vendor lint test

.PHONY: check-release
check-release: vendor lint gosec test
.PHONY: test
test:
time go test ./...

.PHONY: lint
lint: $(GOLANGCI_LINT_BIN)
$(GOLANGCI_LINT_BIN) run -E exportloopref,gofmt --timeout=30m

.PHONY: gosec
gosec: $(GOSEC_BIN)
$(GOSEC_BIN) ./...
time $(GOLANGCI_LINT_BIN) run --verbose --print-resources-usage

$(GOLANGCI_LINT_BIN):
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(FIRST_GOPATH)/bin

$(GOSEC_BIN):
curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(FIRST_GOPATH)/bin
#######################################
# release assets
#######################################

RELEASE_ASSETS = \
$(foreach GOARCH,amd64 arm64,\
$(foreach GOOS,linux darwin windows,\
release-assets/$(GOOS).$(GOARCH))) \

word-dot = $(word $2,$(subst ., ,$1))

.PHONY: release-assets
release-assets: clean-release-assets vendor $(RELEASE_ASSETS)

.PHONY: clean-release-assets
clean-release-assets:
rm -rf ./release-assets
mkdir -p ./release-assets

release-assets/windows.%: $(SOURCE)
echo 'build release-assets for windows/$(call word-dot,$*,2)'
GOOS=windows \
GOARCH=$(call word-dot,$*,1) \
CGO_ENABLED=0 \
time go build -ldflags '$(LDFLAGS)' -o './release-assets/$(PROJECT_NAME).windows.$(call word-dot,$*,1).exe' .

release-assets/%: $(SOURCE)
echo 'build release-assets for $(call word-dot,$*,1)/$(call word-dot,$*,2)'
GOOS=$(call word-dot,$*,1) \
GOARCH=$(call word-dot,$*,2) \
CGO_ENABLED=0 \
time go build -ldflags '$(LDFLAGS)' -o './release-assets/$(PROJECT_NAME).$(call word-dot,$*,1).$(call word-dot,$*,2)' .
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/containrrr/shoutrrr v0.6.1
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/jessevdk/go-flags v1.5.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.19.0 // indirect
Expand All @@ -20,7 +19,10 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
)

require github.com/go-resty/resty/v2 v2.7.0
require (
github.com/go-resty/resty/v2 v2.7.0
github.com/jessevdk/go-flags v1.5.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"net/http"
"net/url"
Expand All @@ -10,7 +11,7 @@ import (
"strings"
"sync/atomic"

"github.com/jessevdk/go-flags"
flags "github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -116,7 +117,8 @@ func initArgparser() {

// check if there is an parse error
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
var flagsErr *flags.Error
if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
Expand Down

0 comments on commit f459be1

Please sign in to comment.