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 |
| 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 Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 49 | all: test |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame^] | 50 | build: docker-build |
| 51 | |
| 52 | |
Dinesh Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 53 | docker-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 . |
| 62 | docker-push: |
| 63 | docker push ${DOCKER_IMAGENAME} |
| 64 | |
Dinesh Belwalkar | 0bc4ace | 2019-11-14 21:41:50 +0000 | [diff] [blame^] | 65 | PATH:=$(GOPATH)/bin:$(PATH) |
| 66 | HADOLINT=$(shell PATH=$(GOPATH):$(PATH) which hadolint) |
| 67 | |
| 68 | lint-dockerfile: |
| 69 | ifeq (,$(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 |
| 73 | endif |
| 74 | @echo "Running Dockerfile lint check ..." |
| 75 | @hadolint $$(find . -name "Dockerfile") |
| 76 | @echo "Dockerfile lint check OK" |
| 77 | |
| 78 | lint-style: |
| 79 | ifeq (,$(shell which gofmt)) |
| 80 | go get -u github.com/golang/go/src/cmd/gofmt |
| 81 | endif |
| 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 | |
| 92 | lint: lint-style lint-dockerfile |
| 93 | |
| 94 | # Rules to automatically install golangci-lint |
| 95 | GOLANGCI_LINT_TOOL?=$(shell which golangci-lint) |
| 96 | ifeq (,$(GOLANGCI_LINT_TOOL)) |
| 97 | GOLANGCI_LINT_TOOL=$(GOPATH)/bin/golangci-lint |
| 98 | golangci_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 |
| 102 | else |
| 103 | golangci_lint_tool_install: |
| 104 | endif |
| 105 | |
| 106 | sca: 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 Belwalkar | 0121796 | 2019-05-23 21:51:16 +0000 | [diff] [blame] | 111 | test: docker-build |
| 112 | |
| 113 | clean: |
| 114 | @echo "No cleanup available" |