Skip to content

Commit

Permalink
build(make): Update docker targets to use updated Dockerfiles
Browse files Browse the repository at this point in the history
Refactored common parts to parent directory.

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
  • Loading branch information
alfrunes committed Sep 5, 2024
1 parent ba3692e commit 11f26d6
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 807 deletions.
35 changes: 22 additions & 13 deletions backend/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# RFC: Should we make all services explicit to allow auto-complete?
SERVICES = $(subst services/,,$(wildcard services/*))

BUILD_TARGETS := $(addsuffix "-build", $(SERVICES))
TEST_TARGETS := $(addsuffix "-test", $(SERVICES))
TEST_UNIT_TARGETS := $(addsuffix "-test-unit", $(SERVICES))
TEST_ACCEPTANCE_TARGETS := $(addsuffix "-test-acceptance", $(SERVICES))
DOCKER_TARGETS := $(addsuffix "-docker", $(SERVICES))
DOCKER_ACCEPTANCE_TARGETS := $(addsuffix "-docker-acceptance", $(SERVICES))
MENDER_IMAGE_PREFIX ?= localhost:5000/mender-server
MENDER_IMAGE_TAG ?= latest

SERVICES = $(subst services,,$(subst /,,$(wildcard services/*/)))
BUILD_TARGETS := $(addsuffix -build, $(SERVICES))
TEST_TARGETS := $(addsuffix -test, $(SERVICES))
TEST_UNIT_TARGETS := $(addsuffix -test-unit, $(SERVICES))
TEST_ACCEPTANCE_TARGETS := $(addsuffix -test-acceptance, $(SERVICES))
DOCKER_TARGETS := $(addsuffix -docker, $(SERVICES))
BUILD_ACCEPTANCE_TARGETS := $(addsuffix -build-acceptance, $(SERVICES))
DOCKER_ACCEPTANCE_TARGETS := $(addsuffix -docker-acceptance, $(SERVICES))

.PHONY: build
build: $(BUILD_TARGETS)
Expand All @@ -21,7 +23,6 @@ test: $(TEST_TARGETS) pkg-test test-integration

.PHONY: $(TEST_TARGETS)
$(TEST_TARGETS):
@echo $(subst -test,,$@)
@$(MAKE) -C services/$(subst -test,,$@) test

.PHONY: pkg-test
Expand All @@ -33,15 +34,14 @@ test-unit: $(TEST_UNIT_TARGETS)

.PHONY: $(TEST_UNIT_TARGETS)
$(TEST_UNIT_TARGETS):
@echo $(subst -test-unit,,$@)
@$(MAKE) -C services/$(subst -test-unit,,$@) test-unit

.PHONY: test-acceptance
test-acceptance: $(TEST_ACCEPTANCE_TARGETS)
docker compose -p acceptance down --remove-orphans

.PHONY: $(TEST_ACCEPTANCE_TARGETS)
$(TEST_ACCEPTANCE_TARGETS):
@echo $(subst -test-acceptance,,$@)
@$(MAKE) -C services/$(subst -test-acceptance,,$@) test-acceptance

.PHONY: test-integration
Expand All @@ -53,7 +53,16 @@ docker: $(DOCKER_TARGETS)

.PHONY: $(DOCKER_TARGETS)
$(DOCKER_TARGETS):
@$(MAKE) -C services/$(subst -docker,,$@) docker
@$(MAKE) -C services/$(subst -docker,,$@) docker \
MENDER_IMAGE_PREFIX=$(MENDER_IMAGE_PREFIX) \
MENDER_IMAGE_TAG=$(MENDER_IMAGE_TAG)

.PHONY: build-acceptance
build-acceptance: $(BUILD_ACCEPTANCE_TARGETS)

.PHONY: $(BUILD_ACCEPTANCE_TARGETS)
$(BUILD_ACCEPTANCE_TARGETS):
@$(MAKE) -C services/$(subst -build-acceptance,,$@) build-acceptance

.PHONY: docker-acceptance
docker-acceptance: $(DOCKER_ACCEPTANCE_TARGETS)
Expand Down
83 changes: 83 additions & 0 deletions backend/services/Makefile.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Makefile.common initializes varialbes and common targets (build* test*)
# for all backend services.
MENDER_IMAGE_PREFIX ?= localhost:5000/mender-server
MENDER_IMAGE_TAG ?= $(MENDER_IMAGE_TAG_DEFAULT)

MENDER_IMAGE_TAG_DEFAULT := latest

bindir ?= $(GIT_ROOT)/dist
binfile ?= $(bindir)/$(COMPONENT)_$(GOOS)-$(GOARCH)

VERSION := $(shell git describe --tag --dirty 2>/dev/null)
GIT_ROOT := $(shell git rev-parse --show-toplevel)
MAKEDIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
DOCFILES := $(wildcard docs/*_api.yml)

GOFILES := $(shell find -name '*.go' -not -name '_test.go')
GOTESTFILES := $(shell find -name '_test.go')
CGO_ENABLED ?= 0
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
GOMODDIR := $(shell go list -m -f '{{.Dir}}')

LDFLAGS ?= -s -w
BUILDFLAGS ?= -trimpath
TESTFLAGS ?=
PYTEST_ARGS ?=

# Convert TAGS to a `-tags` BUILDFLAG by joining the list by comma.
_none :=
_space := $(_none) $(_none)
_comma := ,
BUILDFLAGS += $(let tags,$(TAGS),-tags=$(subst $(_space),$(_comma),$(tags)))

# DOCKER_PLATFORM default is defined with respect to the docker server.
# This fixes the default case on darwin where docker is running inside a VM (linux).
DOCKER_PLATFORM ?= $(shell docker system info -f ' \
{{- .OSType}}/{{- if eq .Architecture "aarch64" -}} \
arm64 \
{{- else -}} \
amd64 \
{{- end -}} \
')
DOCKER_TAG = $(MENDER_IMAGE_PREFIX)/$(COMPONENT):$(MENDER_IMAGE_TAG)

$(binfile): $(GOFILES)
# TODO: Add -ldflags "-X ...$(Version)"
env CGO_ENABLED=$(CGO_ENABLED) \
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
go build -o $(binfile) \
-ldflags $(LDFLAGS) \
$(BUILDFLAGS)

.PHONY: build
build: $(binfile)

.PHONY: generate
generate:
go generate ./...

.PHONY: test-unit
test-unit:
go test $(BUILDFLAGS) $(TESTFLAGS) ./...

.PHONY: test
test: test-unit test-acceptance

docker-acceptance: export TAGS += acceptance
docker-acceptance: export BUILDFLAGS += -cover -installsuffix=.test
%-acceptance test-acceptance-run: export MENDER_IMAGE_TAG_DEFAULT := test

.PHONY: docker
docker:
docker build $(DOCKER_ARGS) \
--platform $(DOCKER_PLATFORM) \
-f Dockerfile \
--build-arg GOFLAGS="$(BUILDFLAGS)" \
--build-arg GOLDFLAGS="$(LDFLAGS)" \
-t $(DOCKER_TAG) \
$(GIT_ROOT)/backend

.PHONY: docker-acceptance
docker-acceptance: docker
72 changes: 2 additions & 70 deletions backend/services/create-artifact-worker/Makefile
Original file line number Diff line number Diff line change
@@ -1,71 +1,3 @@
COMPONENT := $(notdir $(shell go list))
GIT_ROOT := $(shell git rev-parse --show-toplevel)
VERSION := $(shell git describe --tag --dirty 2>/dev/null)
COMPONENT := create-artifact-worker

distdir ?= $(GIT_ROOT)/dist
bindir ?= $(distdir)/$(GOOS)/$(GOARCH)
binfile ?= $(bindir)/$(COMPONENT)

CGO_ENABLED ?= 0
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
GOMODDIR := $(shell go list -m -f '{{.Dir}}')

LDFLAGS ?= "-s -w"
BUILDFLAGS ?= -trimpath -ldflags $(LDFLAGS)
TESTFLAGS ?=

DOCKER_TAG ?= $(COMPONENT):latest
DOCKER_BIN ?= $(subst $(GIT_ROOT),,$(binfile)) # Repo-local file to binary

.PHONY: build
build:
# TODO: Add -ldflags "-X ...Version"
env CGO_ENABLED=$(CGO_ENABLED) \
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
go build -o $(binfile) $(BUILDFLAGS)

.PHONY: build-test
build-test: BUILDFLAGS += -cover -installsuffix .test
build-test: binfile = $(GIT_ROOT)/backend/tests/bin/$(COMPONENT).test
build-test: build

.PHONY: generate
generate:
go generate ./...

.PHONY: test
test: test-unit

.PHONY: test-unit
test-unit:
go test $(BUILDFLAGS) $(TESTFLAGS) ./...

.PHONY: test-acceptance
test-acceptance:

.PHONY: docker
docker: export GOOS = linux
docker: build
$(MAKE) GOOS=$(GOOS) -C ../workflows build
docker build $(DOCKER_ARGS) \
--build-arg BIN_FILE=$(DOCKER_BIN) \
--platform $(GOOS)/$(GOARCH) \
-f Dockerfile \
-t $(DOCKER_TAG) \
$(GIT_ROOT)

.PHONY: docker-acceptance
docker-acceptance: export DOCKER_TAG = $(COMPONENT):test
docker-acceptance: export binfile = $(GIT_ROOT)/backend/tests/bin/$(COMPONENT).test
docker-acceptance: export DOCKER_BIN = $(subst $(GIT_ROOT),,$(binfile))
docker-acceptance: export GOOS = linux
docker-acceptance: build-test
$(MAKE) GOOS=$(GOOS) -C ../workflows build-test
docker build $(DOCKER_ARGS) \
--build-arg BIN_FILE=$(DOCKER_BIN) \
--platform $(GOOS)/$(GOARCH) \
-f Dockerfile \
-t $(DOCKER_TAG) \
$(GIT_ROOT)
include ../Makefile.common
77 changes: 3 additions & 74 deletions backend/services/deployments/Makefile
Original file line number Diff line number Diff line change
@@ -1,75 +1,4 @@
COMPONENT := $(notdir $(shell go list))
GIT_ROOT := $(shell git rev-parse --show-toplevel)
VERSION := $(shell git describe --tag --dirty 2>/dev/null)
COMPONENT := deployments
BUILDFLAGS ?= -tags=nopkcs11 -trimpath

distdir ?= $(GIT_ROOT)/dist
bindir ?= $(distdir)/$(GOOS)/$(GOARCH)
binfile ?= $(bindir)/$(COMPONENT)

CGO_ENABLED ?= 0
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
GOMODDIR := $(shell go list -m -f '{{.Dir}}')

LDFLAGS ?= "-s -w"
BUILDFLAGS ?= -tags nopkcs11 -trimpath -ldflags $(LDFLAGS)
TESTFLAGS ?=
PYTEST_ARGS ?=

DOCKER_TAG ?= $(COMPONENT):latest
DOCKER_BIN ?= $(subst $(GIT_ROOT),,$(binfile)) # Repo-local file to binary

.PHONY: build
build:
# TODO: Add -ldflags "-X ...Version"
env CGO_ENABLED=$(CGO_ENABLED) \
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
go build -o $(binfile) $(BUILDFLAGS)

.PHONY: build-test
build-test: BUILDFLAGS += -cover -installsuffix .test
build-test: binfile = $(GIT_ROOT)/backend/tests/bin/$(COMPONENT).test
build-test: build

.PHONY: generate
generate:
go generate ./...

.PHONY: test-unit
test-unit:
go test $(BUILDFLAGS) $(TESTFLAGS) ./...

.PHONY: test-acceptance-run
test-acceptance-run: docker-acceptance
docker compose -f tests/docker-compose.yml run --rm --use-aliases acceptance-tester $(PYTEST_ARGS)

.PHONY: test-acceptance
test-acceptance: test-acceptance-run
docker compose -f tests/docker-compose.yml down --remove-orphans -v

.PHONY: test
test: test-unit test-acceptance

.PHONY: docker
docker: export GOOS = linux
docker: build
docker build $(DOCKER_ARGS) \
--build-arg BIN_FILE=$(DOCKER_BIN) \
--platform $(GOOS)/$(GOARCH) \
-f Dockerfile \
-t $(DOCKER_TAG) \
$(GIT_ROOT)

.PHONY: docker-acceptance
docker-acceptance: export DOCKER_TAG = $(COMPONENT):test
docker-acceptance: export binfile = $(GIT_ROOT)/backend/tests/bin/$(COMPONENT).test
docker-acceptance: export DOCKER_BIN = $(subst $(GIT_ROOT),,$(binfile))
docker-acceptance: export GOOS = linux
docker-acceptance: build-test
docker build $(DOCKER_ARGS) \
--build-arg BIN_FILE=$(DOCKER_BIN) \
--platform $(GOOS)/$(GOARCH) \
-f Dockerfile \
-t $(DOCKER_TAG) \
$(GIT_ROOT)
include ../Makefile.common
76 changes: 2 additions & 74 deletions backend/services/deviceauth/Makefile
Original file line number Diff line number Diff line change
@@ -1,75 +1,3 @@
COMPONENT := $(notdir $(shell go list))
GIT_ROOT := $(shell git rev-parse --show-toplevel)
VERSION := $(shell git describe --tag --dirty 2>/dev/null)
COMPONENT := deviceauth

distdir ?= $(GIT_ROOT)/dist
bindir ?= $(distdir)/$(GOOS)/$(GOARCH)
binfile ?= $(bindir)/$(COMPONENT)

CGO_ENABLED ?= 0
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
GOMODDIR := $(shell go list -m -f '{{.Dir}}')

LDFLAGS ?= "-s -w"
BUILDFLAGS ?= -trimpath -ldflags $(LDFLAGS)
TESTFLAGS ?=
PYTEST_ARGS ?=

DOCKER_TAG ?= $(COMPONENT):latest
DOCKER_BIN ?= $(subst $(GIT_ROOT),,$(binfile)) # Repo-local file to binary

.PHONY: build
build:
# TODO: Add -ldflags "-X ...Version"
env CGO_ENABLED=$(CGO_ENABLED) \
GOOS=$(GOOS) \
GOARCH=$(GOARCH) \
go build -o $(binfile) $(BUILDFLAGS)

.PHONY: build-test
build-test: BUILDFLAGS += -cover -installsuffix .test
build-test: binfile = $(GIT_ROOT)/backend/tests/bin/$(COMPONENT).test
build-test: build

.PHONY: generate
generate:
go generate ./...

.PHONY: test-unit
test-unit:
go test $(BUILDFLAGS) $(TESTFLAGS) ./...

.PHONY: test-acceptance-run
test-acceptance-run: docker-acceptance
docker compose -f tests/docker-compose.yml run --rm --use-aliases acceptance-tester $(PYTEST_ARGS)

.PHONY: test-acceptance
test-acceptance: test-acceptance-run
docker compose -f tests/docker-compose.yml down --remove-orphans -v

.PHONY: test
test: test-unit test-acceptance

.PHONY: docker
docker: export GOOS = linux
docker: build
docker build $(DOCKER_ARGS) \
--build-arg BIN_FILE=$(DOCKER_BIN) \
--platform $(GOOS)/$(GOARCH) \
-f Dockerfile \
-t $(DOCKER_TAG) \
$(GIT_ROOT)

.PHONY: docker-acceptance
docker-acceptance: export DOCKER_TAG = $(COMPONENT):test
docker-acceptance: export binfile = $(GIT_ROOT)/backend/tests/bin/$(COMPONENT).test
docker-acceptance: export DOCKER_BIN = $(subst $(GIT_ROOT),,$(binfile))
docker-acceptance: export GOOS = linux
docker-acceptance: build-test
docker build $(DOCKER_ARGS) \
--build-arg BIN_FILE=$(DOCKER_BIN) \
--platform $(GOOS)/$(GOARCH) \
-f Dockerfile \
-t $(DOCKER_TAG) \
$(GIT_ROOT)
include ../Makefile.common
Loading

0 comments on commit 11f26d6

Please sign in to comment.