blob: 1fe2b9f040017fdaa1eb4d2579a29c1e3c6e44f9 [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
39 @echo "build : Build the docker images."
40 @echo " - If this is the first time you are building, choose 'make build' option."
41 @echo "clean : Remove files created by the build and tests"
42 @echo "docker-push : Push the docker images to an external repository"
43 @echo "lint-dockerfile : Perform static analysis on Dockerfiles"
44 @echo "lint-style : Verify code is properly gofmt-ed"
45 @echo "lint : Shorthand for lint-style & lint-sanity"
46 @echo "test : Generate reports for all go tests"
47 @echo
48
Dinesh Belwalkar01217962019-05-23 21:51:16 +000049all: test
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000050build: docker-build
51
52
Dinesh Belwalkar01217962019-05-23 21:51:16 +000053docker-build:
54 docker build $(DOCKER_BUILD_ARGS) \
55 -t ${DOCKER_IMAGENAME} \
56 --build-arg org_label_schema_version="${VERSION}" \
57 --build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
58 --build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
59 --build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
60 --build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
61 -f Dockerfile .
62docker-push:
63 docker push ${DOCKER_IMAGENAME}
64
Dinesh Belwalkar0bc4ace2019-11-14 21:41:50 +000065PATH:=$(GOPATH)/bin:$(PATH)
66HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint)
67
68lint-dockerfile:
69ifeq (,$(shell PATH=$(GOPATH):$(PATH) which hadolint))
70 mkdir -p $(GOPATH)/bin
71 curl -o $(GOPATH)/bin/hadolint -sNSL https://github.com/hadolint/hadolint/releases/download/v1.17.1/hadolint-$(shell uname -s)-$(shell uname -m)
72 chmod 755 $(GOPATH)/bin/hadolint
73endif
74 @echo "Running Dockerfile lint check ..."
75 @hadolint $$(find . -name "Dockerfile")
76 @echo "Dockerfile lint check OK"
77
78lint-style:
79ifeq (,$(shell which gofmt))
80 go get -u github.com/golang/go/src/cmd/gofmt
81endif
82 @echo "Running style check..."
83 @gofmt_out="$$(gofmt -l $$(find . -name '*.go' -not -path './vendor/*'))" ;\
84 if [ ! -z "$$gofmt_out" ]; then \
85 echo "$$gofmt_out" ;\
86 echo "Style check failed on one or more files ^, run 'go fmt' to fix." ;\
87 exit 1 ;\
88 fi
89 @echo "Style check OK"
90
91
92lint: lint-style lint-dockerfile
93
94# Rules to automatically install golangci-lint
95GOLANGCI_LINT_TOOL?=$(shell which golangci-lint)
96ifeq (,$(GOLANGCI_LINT_TOOL))
97GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint
98golangci_lint_tool_install:
99 # Same version as installed by Jenkins ci-management
100 # Note that install using `go get` is not recommended as per https://github.com/golangci/golangci-lint
101 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.17.0
102else
103golangci_lint_tool_install:
104endif
105
106sca: golangci_lint_tool_install
107 rm -rf ./sca-report
108 @mkdir -p ./sca-report
109 $(GOLANGCI_LINT_TOOL) run --out-format junit-xml ./... 2>&1 | tee ./sca-report/sca-report.xml
110
Dinesh Belwalkar01217962019-05-23 21:51:16 +0000111test: docker-build
112
113clean:
114 @echo "No cleanup available"