Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 1 | # Copyrigh/ 2019-present Open Networking Foundation |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # Configure shell |
| 16 | SHELL = bash -eu -o pipefail |
| 17 | |
| 18 | # Variables |
| 19 | VERSION ?= $(shell cat ./VERSION) |
| 20 | CONTAINER_NAME ?= $(notdir $(abspath .)) |
| 21 | |
| 22 | ## Docker related |
| 23 | DOCKER_REGISTRY ?= |
| 24 | DOCKER_REPOSITORY ?= |
| 25 | DOCKER_BUILD_ARGS ?= |
| 26 | DOCKER_TAG ?= ${VERSION} |
| 27 | DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${CONTAINER_NAME}:${DOCKER_TAG} |
| 28 | |
| 29 | ## Docker labels. Only set ref and commit date if committed |
| 30 | DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote)) |
| 31 | DOCKER_LABEL_VCS_REF ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown") |
| 32 | DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" ) |
| 33 | DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ") |
| 34 | |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 35 | help: |
| 36 | @echo "Usage: make [<target>]" |
| 37 | @echo "where available targets are:" |
| 38 | @echo |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 39 | @echo "proto/importer.pb.go : Build importer.pb.go for go build ./.." |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 40 | @echo "build : Build the docker images." |
| 41 | @echo " - If this is the first time you are building, choose 'make build' option." |
| 42 | @echo "clean : Remove files created by the build and tests" |
| 43 | @echo "docker-push : Push the docker images to an external repository" |
| 44 | @echo "lint-dockerfile : Perform static analysis on Dockerfiles" |
| 45 | @echo "lint-style : Verify code is properly gofmt-ed" |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 46 | @echo "lint-sanity : Verify that 'go vet' doesn't report any issues" |
| 47 | @echo "lint-mod : Verify the integrity of the 'mod' files" |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 48 | @echo "lint : Shorthand for lint-style & lint-sanity" |
| 49 | @echo "test : Generate reports for all go tests" |
| 50 | @echo |
| 51 | |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 52 | all: test |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 53 | proto/importer.pb.go: proto/importer.proto |
| 54 | mkdir -p proto |
| 55 | protoc --proto_path=proto \ |
| 56 | -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \ |
| 57 | --go_out=plugins=grpc:proto/ \ |
| 58 | proto/importer.proto |
| 59 | |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 60 | build: docker-build |
| 61 | |
| 62 | |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 63 | docker-build: |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 64 | docker build $(DOCKER_BUILD_ARGS) \ |
| 65 | -t ${DOCKER_IMAGENAME} \ |
| 66 | --build-arg org_label_schema_version="${VERSION}" \ |
| 67 | --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \ |
| 68 | --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \ |
| 69 | --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \ |
| 70 | --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \ |
| 71 | -f Dockerfile . |
| 72 | docker-push: |
| 73 | docker push ${DOCKER_IMAGENAME} |
| 74 | |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 75 | PATH:=$(GOPATH)/bin:$(PATH) |
| 76 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 77 | |
| 78 | lint-dockerfile: |
| 79 | ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint)) |
| 80 | mkdir -p $(GOPATH)/bin |
| 81 | curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m) |
| 82 | chmod 755 $(GOPATH)/bin/hadolint |
| 83 | endif |
| 84 | @echo "Running Dockerfile lint check ..." |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 85 | @hadolint $$(find . -type f -not -path "./vendor/*" -name "Dockerfile") |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 86 | @echo "Dockerfile lint check OK" |
| 87 | |
| 88 | lint-style: |
| 89 | ifeq (,$(shell which gofmt)) |
| 90 | go get -u github.com/golang/go/src/cmd/gofmt |
| 91 | endif |
| 92 | @echo "Running style check..." |
| 93 | @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\ |
| 94 | if [ ! -z "$$gofmt_out" ]; then \ |
| 95 | echo "$$gofmt_out" ;\ |
| 96 | echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\ |
| 97 | exit 1 ;\ |
| 98 | fi |
| 99 | @echo "Style check OK" |
| 100 | |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 101 | lint-sanity:proto/importer.pb.go |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 102 | @echo "Running sanity check..." |
| 103 | @go vet -mod=vendor ./... |
| 104 | @echo "Sanity check OK" |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 105 | |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 106 | lint-mod: |
| 107 | @echo "Running dependency check..." |
| 108 | @go mod verify |
| 109 | @echo "Dependency check OK" |
| 110 | lint: lint-style lint-dockerfile lint-sanity lint-mod |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame] | 111 | |
| 112 | # Rules to automatically install golangci-lint |
| 113 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
| 114 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
| 115 | GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint |
| 116 | golangci_lint_tool_install: |
| 117 | # Same version as installed by Jenkins ci-management |
| 118 | # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint |
| 119 | curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0 |
| 120 | else |
| 121 | golangci_lint_tool_install: |
| 122 | endif |
| 123 | |
| 124 | sca: golangci_lint_tool_install |
| 125 | rm -rf ./sca-report |
| 126 | @mkdir -p ./sca-report |
| 127 | $(GOLANGCI_LINT_TOOL) run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml |
| 128 | |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 129 | test: docker-build |
| 130 | |
| 131 | clean: |
| 132 | @echo "No cleanup available" |