Skip to content

Instantly share code, notes, and snippets.

@cxsu
Forked from thomaspoignant/Makefile
Created February 16, 2021 15:22
Show Gist options
  • Save cxsu/e99daec7515bf13fc224c493fb962260 to your computer and use it in GitHub Desktop.
Save cxsu/e99daec7515bf13fc224c493fb962260 to your computer and use it in GitHub Desktop.

Revisions

  1. @thomaspoignant thomaspoignant revised this gist Feb 9, 2021. No changes.
  2. @thomaspoignant thomaspoignant revised this gist Feb 9, 2021. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ BINARY_NAME=example
    VERSION?=0.0.0
    SERVICE_PORT?=3000
    DOCKER_REGISTRY?= #if set it should finished by /
    EXPORT_LINT?=false # for CI please set EXPORT_LINT to true
    EXPORT_RESULT?=false # for CI please set EXPORT_RESULT to true

    GREEN := $(shell tput -Txterm setaf 2)
    YELLOW := $(shell tput -Txterm setaf 3)
    @@ -22,17 +22,17 @@ lint-dockerfile:
    # If dockerfile is present we lint it.
    ifeq ($(shell test -e ./Dockerfile && echo -n yes),yes)
    $(eval CONFIG_OPTION = $(shell [ -e $(shell pwd)/.hadolint.yaml ] && echo "-v $(shell pwd)/.hadolint.yaml:/root/.config/hadolint.yaml" || echo "" ))
    $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_LINT}" == "true" ] && echo "--format checkstyle" || echo "" ))
    $(eval OUTPUT_FILE = $(shell [ "${EXPORT_LINT}" == "true" ] && echo "| tee /dev/tty > checkstyle-report.xml" || echo "" ))
    $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "--format checkstyle" || echo "" ))
    $(eval OUTPUT_FILE = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "| tee /dev/tty > checkstyle-report.xml" || echo "" ))
    docker run --rm -i $(CONFIG_OPTION) hadolint/hadolint hadolint $(OUTPUT_OPTIONS) - < ./Dockerfile $(OUTPUT_FILE)
    endif

    lint-go:
    $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_LINT}" == "true" ] && echo "--out-format checkstyle ./... | tee /dev/tty > checkstyle-report.xml" || echo "" ))
    $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_RESULT}" == "true" ] && echo "--out-format checkstyle ./... | tee /dev/tty > checkstyle-report.xml" || echo "" ))
    docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:latest-alpine golangci-lint run --deadline=65s $(OUTPUT_OPTIONS)

    lint-yaml:
    ifeq ($(EXPORT_LINT), true)
    ifeq ($(EXPORT_RESULT), true)
    GO111MODULE=off go get -u github.com/thomaspoignant/yamllint-checkstyle
    $(eval OUTPUT_OPTIONS = | tee /dev/tty | yamllint-checkstyle > yamllint-checkstyle.xml)
    endif
    @@ -44,7 +44,7 @@ clean:
    rm -f ./junit-report.xml checkstyle-report.xml ./coverage.xml ./profile.cov yamllint-checkstyle.xml

    test:
    ifeq ($(EXPORT_LINT), true)
    ifeq ($(EXPORT_RESULT), true)
    GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
    $(eval OUTPUT_OPTIONS = | tee /dev/tty | go-junit-report -set-exit-code > junit-report.xml)
    endif
    @@ -53,7 +53,7 @@ endif
    coverage:
    $(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
    $(GOCMD) tool cover -func profile.cov
    ifeq ($(EXPORT_LINT), true)
    ifeq ($(EXPORT_RESULT), true)
    GO111MODULE=off go get -u github.com/AlekSi/gocov-xml
    GO111MODULE=off go get -u github.com/axw/gocov/gocov
    gocov convert profile.cov | gocov-xml > coverage.xml
  3. @thomaspoignant thomaspoignant revised this gist Feb 9, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -95,6 +95,7 @@ help:
    @echo " ${YELLOW}lint ${RESET} ${GREEN}Run all available linters${RESET}"
    @echo " ${YELLOW}lint-dockerfile ${RESET} ${GREEN}Lint your Dockerfile${RESET}"
    @echo " ${YELLOW}lint-go ${RESET} ${GREEN}Use golintci-lint on your project${RESET}"
    @echo " ${YELLOW}lint-yaml ${RESET} ${GREEN}Use yamllint on the yaml file of your projects${RESET}"
    @echo " ${YELLOW}test ${RESET} ${GREEN}Run the tests of the project${RESET}"
    @echo " ${YELLOW}vendor ${RESET} ${GREEN}Copy of all packages needed to support builds and tests in the vendor directory${RESET}"
    @echo " ${YELLOW}watch ${RESET} ${GREEN}Run the code with cosmtrek/air to have automatic reload on changes${RESET}"
  4. @thomaspoignant thomaspoignant created this gist Feb 9, 2021.
    100 changes: 100 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,100 @@
    GOCMD=go
    GOTEST=$(GOCMD) test
    GOVET=$(GOCMD) vet
    BINARY_NAME=example
    VERSION?=0.0.0
    SERVICE_PORT?=3000
    DOCKER_REGISTRY?= #if set it should finished by /
    EXPORT_LINT?=false # for CI please set EXPORT_LINT to true

    GREEN := $(shell tput -Txterm setaf 2)
    YELLOW := $(shell tput -Txterm setaf 3)
    WHITE := $(shell tput -Txterm setaf 7)
    RESET := $(shell tput -Txterm sgr0)

    .PHONY: all test build vendor

    all: help

    lint: lint-go lint-dockerfile lint-yaml

    lint-dockerfile:
    # If dockerfile is present we lint it.
    ifeq ($(shell test -e ./Dockerfile && echo -n yes),yes)
    $(eval CONFIG_OPTION = $(shell [ -e $(shell pwd)/.hadolint.yaml ] && echo "-v $(shell pwd)/.hadolint.yaml:/root/.config/hadolint.yaml" || echo "" ))
    $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_LINT}" == "true" ] && echo "--format checkstyle" || echo "" ))
    $(eval OUTPUT_FILE = $(shell [ "${EXPORT_LINT}" == "true" ] && echo "| tee /dev/tty > checkstyle-report.xml" || echo "" ))
    docker run --rm -i $(CONFIG_OPTION) hadolint/hadolint hadolint $(OUTPUT_OPTIONS) - < ./Dockerfile $(OUTPUT_FILE)
    endif

    lint-go:
    $(eval OUTPUT_OPTIONS = $(shell [ "${EXPORT_LINT}" == "true" ] && echo "--out-format checkstyle ./... | tee /dev/tty > checkstyle-report.xml" || echo "" ))
    docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:latest-alpine golangci-lint run --deadline=65s $(OUTPUT_OPTIONS)

    lint-yaml:
    ifeq ($(EXPORT_LINT), true)
    GO111MODULE=off go get -u github.com/thomaspoignant/yamllint-checkstyle
    $(eval OUTPUT_OPTIONS = | tee /dev/tty | yamllint-checkstyle > yamllint-checkstyle.xml)
    endif
    docker run --rm -it -v $(shell pwd):/data cytopia/yamllint -f parsable $(shell git ls-files '*.yml' '*.yaml') $(OUTPUT_OPTIONS)

    clean:
    rm -fr ./bin
    rm -fr ./out
    rm -f ./junit-report.xml checkstyle-report.xml ./coverage.xml ./profile.cov yamllint-checkstyle.xml

    test:
    ifeq ($(EXPORT_LINT), true)
    GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
    $(eval OUTPUT_OPTIONS = | tee /dev/tty | go-junit-report -set-exit-code > junit-report.xml)
    endif
    $(GOTEST) -v -race ./... $(OUTPUT_OPTIONS)

    coverage:
    $(GOTEST) -cover -covermode=count -coverprofile=profile.cov ./...
    $(GOCMD) tool cover -func profile.cov
    ifeq ($(EXPORT_LINT), true)
    GO111MODULE=off go get -u github.com/AlekSi/gocov-xml
    GO111MODULE=off go get -u github.com/axw/gocov/gocov
    gocov convert profile.cov | gocov-xml > coverage.xml
    endif

    vendor:
    $(GOCMD) mod vendor

    build:
    mkdir -p out/bin
    GO111MODULE=on $(GOCMD) build -mod vendor -o out/bin/$(BINARY_NAME) .

    docker-build:
    docker build --rm --tag $(BINARY_NAME) .

    docker-release:
    docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)$(BINARY_NAME):latest
    docker tag $(BINARY_NAME) $(DOCKER_REGISTRY)$(BINARY_NAME):$(VERSION)
    # Push the docker images
    docker push $(DOCKER_REGISTRY)$(BINARY_NAME):latest
    docker push $(DOCKER_REGISTRY)$(BINARY_NAME):$(VERSION)

    watch:
    $(eval PACKAGE_NAME=$(shell head -n 1 go.mod | cut -d ' ' -f2))
    docker run -it --rm -w /go/src/$(PACKAGE_NAME) -v $(shell pwd):/go/src/$(PACKAGE_NAME) -p $(SERVICE_PORT):$(SERVICE_PORT) cosmtrek/air

    help:
    @echo ''
    @echo 'Usage:'
    @echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
    @echo ''
    @echo 'Targets:'
    @echo " ${YELLOW}build ${RESET} ${GREEN}Build your project and put the output binary in out/bin/$(BINARY_NAME)${RESET}"
    @echo " ${YELLOW}clean ${RESET} ${GREEN}Remove build related file${RESET}"
    @echo " ${YELLOW}coverage ${RESET} ${GREEN}Run the tests of the project and export the coverage${RESET}"
    @echo " ${YELLOW}docker-build ${RESET} ${GREEN}Use the dockerfile to build the container (name: $(BINARY_NAME))${RESET}"
    @echo " ${YELLOW}docker-release ${RESET} ${GREEN}Release the container \"$(DOCKER_REGISTRY)$(BINARY_NAME)\" with tag latest and $(VERSION) ${RESET}"
    @echo " ${YELLOW}help ${RESET} ${GREEN}Show this help message${RESET}"
    @echo " ${YELLOW}lint ${RESET} ${GREEN}Run all available linters${RESET}"
    @echo " ${YELLOW}lint-dockerfile ${RESET} ${GREEN}Lint your Dockerfile${RESET}"
    @echo " ${YELLOW}lint-go ${RESET} ${GREEN}Use golintci-lint on your project${RESET}"
    @echo " ${YELLOW}test ${RESET} ${GREEN}Run the tests of the project${RESET}"
    @echo " ${YELLOW}vendor ${RESET} ${GREEN}Copy of all packages needed to support builds and tests in the vendor directory${RESET}"
    @echo " ${YELLOW}watch ${RESET} ${GREEN}Run the code with cosmtrek/air to have automatic reload on changes${RESET}"