Skip to content

Commit

Permalink
chore: update makefile (#2330)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramizpolic authored Nov 13, 2023
1 parent f230571 commit 7a5ba69
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 130 deletions.
25 changes: 21 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,24 @@ jobs:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Set up Go
uses: actions/setup-go@v4
- name: Set up Nix
uses: cachix/install-nix-action@6a9a9e84a173d90b3ffb42c5ddaf9ea033fad011 # v23
with:
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Set up magic Nix cache
uses: DeterminateSystems/magic-nix-cache-action@8a218f9e264e9c3803c9a1ee1c30d8e4ab55be63 # v2

- name: Set up Go cache
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
with:
go-version: '1.20'
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ github.job }}-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ github.job }}-${{ runner.os }}-go-
- name: Setup test dependencies
run: |
Expand All @@ -201,8 +215,11 @@ jobs:
sudo pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so --keypairgen --key-type rsa:2048 --pin banzai --token-label bank-vaults --label bank-vaults
sudo chown -R runner:docker /etc/softhsm /var/lib/softhsm
- name: Prepare Nix shell
run: nix develop --impure .#ci

- name: Run verification
run: make check
run: nix develop --impure .#ci -c make check
env:
VAULT_TOKEN: 227e1cce-6bf7-30bb-2d2a-acc854318caf

Expand Down
259 changes: 135 additions & 124 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,64 +1,10 @@
# A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html

export PATH := $(abspath bin/):${PATH}

CONTAINER_IMAGE_REF = ghcr.io/bank-vaults/bank-vaults:dev

# Dependency versions
GOLANGCI_VERSION = 1.53.3
LICENSEI_VERSION = 0.8.0

.PHONY: build
build: ## Build binary
@mkdir -p build
go build -race -o build/ ./cmd/bank-vaults

.PHONY: gen-docs
gen-docs: ## Generate CLI documentation
@mkdir -p "build/docs"
go run -tags=gen_docs ./cmd/bank-vaults gen-docs "build/docs"

.PHONY: lint
lint: lint-go lint-docker lint-yaml
lint: ## Run linters

.PHONY: lint-go
lint-go:
golangci-lint run $(if ${CI},--out-format github-actions,)

.PHONY: lint-docker
lint-docker:
hadolint Dockerfile

.PHONY: lint-yaml
lint-yaml:
yamllint $(if ${CI},-f github,) --no-warnings .

.PHONY: fmt
fmt: ## Format code
golangci-lint run --fix

.PHONY: license-check
license-check: ## Run license check
licensei check
licensei header

deps: bin/golangci-lint bin/licensei
deps: ## Install dependencies

bin/golangci-lint:
@mkdir -p bin
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v${GOLANGCI_VERSION}

bin/licensei:
@mkdir -p bin
curl -sfL https://raw.githubusercontent.com/goph/licensei/master/install.sh | bash -s -- v${LICENSEI_VERSION}




OS = $(shell uname)
export PATH := $(abspath bin/):${PATH}

# Docker variables
DOCKER_TAG ?= ${VERSION}
DOCKER_BUILD_EXTRA_ARGS ?=
# Export HOST_NETWORK=1 if you want to build the docker images with host network (useful when using some VPNs)
ifeq (${HOST_NETWORK}, 1)
Expand All @@ -71,6 +17,7 @@ BINARY_NAME ?= bank-vaults
DOCKER_REGISTRY ?= ghcr.io/bank-vaults
DOCKER_IMAGE = ${DOCKER_REGISTRY}/bank-vaults
WEBHOOK_DOCKER_IMAGE = ${DOCKER_REGISTRY}/vault-secrets-webhook
CONTAINER_IMAGE_REF = ghcr.io/bank-vaults/bank-vaults:dev

# Build variables
BUILD_DIR ?= build
Expand All @@ -85,14 +32,33 @@ ifeq (${VERBOSE}, 1)
GOARGS += -v
endif

# Docker variables
DOCKER_TAG ?= ${VERSION}
############################

# Dependency versions
GOTESTSUM_VERSION = 0.4.0
##@ General

GOLANG_VERSION = 1.21.1
# Targets commented with ## will be visible in "make help" info.
# Comments marked with ##@ will be used as categories for a group of targets.

.PHONY: help
default: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Build

.PHONY: build
build: ## Build binary
@mkdir -p build
go build -race -o build/ ./cmd/bank-vaults

.PHONY: build-debug
build-debug: GOARGS += -gcflags "-N -l"
build-debug: BINARY_NAME := ${BINARY_NAME}-debug
build-debug: build ## Build binary in debug mode

.PHONY: build-docker
build-docker: ## Build binary using Docker
docker run -it -v $(PWD):/go/src/${PACKAGE} -w /go/src/${PACKAGE} golang:${GOLANG_VERSION}-alpine go build -o ${BINARY_NAME}_linux ${BUILD_PACKAGE}

.PHONY: docker
docker: ## Build a Docker image
Expand All @@ -101,79 +67,60 @@ ifeq (${DOCKER_LATEST}, 1)
docker tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_IMAGE}:latest
endif

.PHONY: image
image: ## Build an OCI image with buildah
buildah bud -t ${DOCKER_IMAGE}:${DOCKER_TAG} -f Dockerfile .
ifeq (${IMAGE_LATEST}, 1)
buildah tag ${DOCKER_IMAGE}:${DOCKER_TAG} ${DOCKER_IMAGE}:latest
endif

.PHONY: docker-push
docker-push: ## Push a Docker image
docker push ${DOCKER_IMAGE}:${DOCKER_TAG}
ifeq (${DOCKER_LATEST}, 1)
docker push ${DOCKER_IMAGE}:latest
endif

.PHONY: test-%
test-%: ## Run a specific test suite
@${MAKE} VERBOSE=0 GOTAGS=$* test


release-%: ## Release a new version
git tag -m 'Release $*' $*

@echo "Version updated to $*!"
@echo
@echo "To push the changes execute the following:"
@echo
@echo "git push; git push origin $*"
.PHONY: artifacts
artifacts: ## Build binary and Docker image
artifacts: build docker

.PHONY: patch
patch: ## Release a new patch version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1"."$$2"."$$3+1}')

.PHONY: minor
minor: ## Release a new minor version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1"."$$2+1".0"}')
.PHONY: clean
clean: ## Clear the working area and the project
rm -rf ${BUILD_DIR}/ bin/ vendor/

.PHONY: major
major: ## Release a new major version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1+1".0.0"}')
##@ Checks

.PHONY: clean
clean: ## Clean builds
rm -rf ${BUILD_DIR}/
.PHONY: check
check: lint test-integration ## Run tests and linters

.PHONY: clear
clear: ## Clear the working area and the project
rm -rf bin/ vendor/
.PHONY: fmt
fmt: ## Format code
$(GOLANGCI_LINT_BIN) run --fix

.PHONY: lint
lint: lint-go lint-docker lint-yaml
lint: ## Run linters

.PHONY: docker-build
docker-build: ## Builds go binary in docker image
docker run -it -v $(PWD):/go/src/${PACKAGE} -w /go/src/${PACKAGE} golang:${GOLANG_VERSION}-alpine go build -o ${BINARY_NAME}_linux ${BUILD_PACKAGE}
.PHONY: lint-go
lint-go:
$(GOLANGCI_LINT_BIN) run $(if ${CI},--out-format github-actions,)

.PHONY: debug
debug: GOARGS += -gcflags "-N -l"
debug: BINARY_NAME := ${BINARY_NAME}-debug
debug: build ## Builds binary package
.PHONY: lint-docker
lint-docker:
$(HADOLINT_BIN) Dockerfile

.PHONY: debug-docker
debug-docker: debug ## Builds binary package
docker build -t ghcr.io/bank-vaults/${BINARY_NAME}:debug -f Dockerfile.dev .
.PHONY: lint-yaml
lint-yaml:
$(YAMLLINT_BIN) $(if ${CI},-f github,) --no-warnings .

.PHONY: check
check: test-integration ## Run tests and linters
.PHONY: license-check
license-check: ## Run license check
$(LICENSEI_BIN) check
$(LICENSEI_BIN) header

bin/gotestsum: bin/gotestsum-${GOTESTSUM_VERSION}
@ln -sf gotestsum-${GOTESTSUM_VERSION} bin/gotestsum
bin/gotestsum-${GOTESTSUM_VERSION}:
@mkdir -p bin
curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION}
##@ Tests

TEST_PKGS ?= ./...
TEST_REPORT_NAME ?= results.xml

# Dependency versions
GOTESTSUM_VERSION = 0.4.0
GOLANG_VERSION = 1.21.1

.PHONY: test
test: TEST_REPORT ?= main
test: TEST_FORMAT ?= short
Expand All @@ -182,6 +129,10 @@ test: bin/gotestsum ## Run tests
@mkdir -p ${BUILD_DIR}/test_results/${TEST_REPORT}
bin/gotestsum --no-summary=skipped --junitfile ${BUILD_DIR}/test_results/${TEST_REPORT}/${TEST_REPORT_NAME} --format ${TEST_FORMAT} -- $(filter-out -v,${GOARGS}) $(if ${TEST_PKGS},${TEST_PKGS},./...)

.PHONY: test-%
test-%: ## Run a specific test suite
@${MAKE} VERBOSE=0 GOTAGS=$* test

.PHONY: test-all
test-all: ## Run all tests
@${MAKE} GOARGS="${GOARGS} -run .\*" TEST_REPORT=all test
Expand All @@ -190,8 +141,16 @@ test-all: ## Run all tests
test-integration: ## Run integration tests
@${MAKE} GOARGS="${GOARGS} -tags=integration" TEST_REPORT=integration test

bin/gotestsum: bin/gotestsum-${GOTESTSUM_VERSION}
@ln -sf gotestsum-${GOTESTSUM_VERSION} bin/gotestsum

bin/gotestsum-${GOTESTSUM_VERSION}:
@mkdir -p bin
curl -L https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_amd64.tar.gz | tar -zOxf - gotestsum > ./bin/gotestsum-${GOTESTSUM_VERSION} && chmod +x ./bin/gotestsum-${GOTESTSUM_VERSION}

bin/jq: bin/jq-${JQ_VERSION}
@ln -sf jq-${JQ_VERSION} bin/jq

bin/jq-${JQ_VERSION}:
@mkdir -p bin
ifeq (${OS}, Darwin)
Expand All @@ -201,15 +160,67 @@ ifeq (${OS}, Linux)
curl -L https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 > ./bin/jq-${JQ_VERSION} && chmod +x ./bin/jq-${JQ_VERSION}
endif

.PHONY: list
list: ## List all make targets
@$(MAKE) -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort

.PHONY: help
.DEFAULT_GOAL := help
help:
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

# Variable outputting/exporting rules
var-%: ; @echo $($*)
varexport-%: ; @echo $*=$($*)

##@ Release

release-%: ## Tag a new release
git tag -m 'Release $*' $*

@echo "Version updated to $*!"
@echo
@echo "To push the changes execute the following:"
@echo
@echo "git push; git push origin $*"

.PHONY: patch
patch: ## Tag a new patch version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1"."$$2"."$$3+1}')

.PHONY: minor
minor: ## Tag a new minor version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1"."$$2+1".0"}')

.PHONY: major
major: ## Tag a new major version
@${MAKE} release-$(shell git describe --abbrev=0 --tags | awk -F'[ .]' '{print $$1+1".0.0"}')

##@ Autogeneration

.PHONY: gen-docs
gen-docs: ## Generate CLI documentation
@mkdir -p "build/docs"
go run -tags=gen_docs ./cmd/bank-vaults gen-docs "build/docs"

##@ Dependencies

deps: bin/golangci-lint bin/licensei
deps: ## Install dependencies

# Dependency versions
GOLANGCI_VERSION = 1.53.3
LICENSEI_VERSION = 0.8.0

# Dependency binaries
GOLANGCI_LINT_BIN := golangci-lint
LICENSEI_BIN := licensei

# TODO: add support for hadolint and yamllint dependencies
HADOLINT_BIN := hadolint
YAMLLINT_BIN := yamllint

# If we have "bin" dir, use those binaries instead
ifneq ($(wildcard ./bin/.),)
GOLANGCI_LINT_BIN := bin/$(GOLANGCI_LINT_BIN)
LICENSEI_BIN := bin/$(LICENSEI_BIN)
endif

bin/golangci-lint:
@mkdir -p bin
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- v${GOLANGCI_VERSION}

bin/licensei:
@mkdir -p bin
curl -sfL https://raw.githubusercontent.com/goph/licensei/master/install.sh | bash -s -- v${LICENSEI_VERSION}
Loading

0 comments on commit 7a5ba69

Please sign in to comment.