blob: 9d06f6422792e9365af310a7e5da80d23fc393d0 [file] [log] [blame]
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +00001# Copyrigh/ 2019-present Open Networking Foundation
Dinesh Belwalkar01217962019-05-23 21:51:16 +00002#
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
16SHELL = bash -eu -o pipefail
17
18# Variables
19VERSION ?= $(shell cat ./VERSION)
20CONTAINER_NAME ?= $(notdir $(abspath .))
21
22## Docker related
23DOCKER_REGISTRY ?=
24DOCKER_REPOSITORY ?=
25DOCKER_BUILD_ARGS ?=
26DOCKER_TAG ?= ${VERSION}
27DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${CONTAINER_NAME}:${DOCKER_TAG}
28
29## Docker labels. Only set ref and commit date if committed
30DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
31DOCKER_LABEL_VCS_REF ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown")
32DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
33DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")
34
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000035help:
36 @echo "Usage: make [<target>]"
37 @echo "where available targets are:"
38 @echo
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000039 @echo "proto/importer.pb.go : Build importer.pb.go for go build ./.."
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000040 @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 Belwalkare63f7f92019-11-22 23:11:16 +000046 @echo "lint-sanity : Verify that 'go vet' doesn't report any issues"
47 @echo "lint-mod : Verify the integrity of the 'mod' files"
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000048 @echo "lint : Shorthand for lint-style & lint-sanity"
49 @echo "test : Generate reports for all go tests"
50 @echo
51
Dinesh Belwalkar01217962019-05-23 21:51:16 +000052all: test
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000053proto/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 Belwalkar0bc4ace2019-11-14 21:41:50 +000060build: docker-build
61
62
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000063docker-build:
Dinesh Belwalkar01217962019-05-23 21:51:16 +000064 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 .
72docker-push:
73 docker push ${DOCKER_IMAGENAME}
74
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000075PATH:=$(GOPATH)/bin:$(PATH)
76HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
77
78lint-dockerfile:
79ifeq (,$(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
83endif
84 @echo "Running Dockerfile lint check ..."
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +000085 @hadolint $$(find . -type f -not -path "./vendor/*" -name "Dockerfile")
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000086 @echo "Dockerfile lint check OK"
87
88lint-style:
89ifeq (,$(shell which gofmt))
90 go get -u github.com/golang/go/src/cmd/gofmt
91endif
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 Belwalkara6ba07d2020-01-10 23:22:34 +0000101lint-sanity:proto/importer.pb.go
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000102 @echo "Running sanity check..."
103 @go vet -mod=vendor ./...
104 @echo "Sanity check OK"
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +0000105
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000106lint-mod:
107 @echo "Running dependency check..."
108 @go mod verify
109 @echo "Dependency check OK"
110lint: lint-style lint-dockerfile lint-sanity lint-mod
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +0000111
112# Rules to automatically install golangci-lint
113GOLANGCI_LINT_TOOL?=$(shell which golangci-lint)
114ifeq (,$(GOLANGCI_LINT_TOOL))
115GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint
116golangci_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
120else
121golangci_lint_tool_install:
122endif
123
124sca: 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 Belwalkar01217962019-05-23 21:51:16 +0000129test: docker-build
130
131clean:
132 @echo "No cleanup available"