Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Unifiy build process up and downstream #787

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
bin/
*.tar
32 changes: 0 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,38 +175,6 @@ jobs:
- name: Build Image
run: GOOS=linux GOARCH=amd64 make image-build

build-manager:
needs:
- verify-go-modules
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache: true
cache-dependency-path: '**/go.sum'
- name: Build Manager
run: make manager-only

build-web-console-validator:
needs:
- verify-go-modules
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache: true
cache-dependency-path: '**/go.sum'
- name: Build Web Console Validator
run: make web-console-validator-only

test:
needs:
- verify-go-modules
Expand Down
85 changes: 10 additions & 75 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,77 +1,7 @@
# Go version used to build the binaries.
ARG GO_VERSION=1.23

## Docker image used to build the binaries.
FROM golang:${GO_VERSION} as builder


## --------------------------------------
## Multi-platform support
## --------------------------------------

ARG TARGETOS
ARG TARGETARCH


## --------------------------------------
## Environment variables
## --------------------------------------

ENV GOOS=${TARGETOS}
ENV GOARCH=${TARGETARCH}


## --------------------------------------
## Build information
## --------------------------------------

ARG BUILD_COMMIT
ARG BUILD_NUMBER
ARG BUILD_VERSION

ENV BUILD_COMMIT=${BUILD_COMMIT}
ENV BUILD_NUMBER=${BUILD_NUMBER}
ENV BUILD_VERSION=${BUILD_VERSION}


## --------------------------------------
## Configure the working directory
## --------------------------------------

WORKDIR /workspace


## --------------------------------------
## Copy in local sources
## --------------------------------------

# Copy the sources
COPY ./ ./


## --------------------------------------
## Build the binaries
## --------------------------------------

# Build
RUN make manager-only
RUN make web-console-validator-only


## --------------------------------------
## Create the manager image
## --------------------------------------
ARG BASE_IMAGE=gcr.io/distroless/base-debian12

# Copy the controller-manager into a thin image
FROM gcr.io/distroless/static-debian11


## --------------------------------------
## Multi-platform support
## --------------------------------------

ARG TARGETOS
ARG TARGETARCH
FROM ${BASE_IMAGE}


## --------------------------------------
Expand All @@ -96,15 +26,20 @@ ARG BUILD_VERSION
## Image labels
## --------------------------------------

LABEL buildNumber=$BUILD_NUMBER commit=$BUILD_COMMIT branch=$BUILD_BRANCH version=$BUILD_VERSION
LABEL branch="${BUILD_BRANCH}" \
buildNumber="${BUILD_NUMBER}" \
commit="${BUILD_COMMIT}" \
name="VM Operator" \
vendor="Broadcom" \
version="${BUILD_VERSION}"


## --------------------------------------
## Copy the binaries from the builder
## --------------------------------------

WORKDIR /
COPY --from=builder /workspace/bin/manager .
COPY --from=builder /workspace/bin/web-console-validator .
COPY ./bin/manager .
COPY ./bin/web-console-validator .
USER nobody
ENTRYPOINT ["/manager"]
47 changes: 41 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ WEBHOOK_ROOT ?= $(MANIFEST_ROOT)/webhook
RBAC_ROOT ?= $(MANIFEST_ROOT)/rbac

# Image URL to use all building/pushing image targets
BASE_IMAGE ?= gcr.io/distroless/base-debian12
IMAGE ?= vmoperator-controller
IMAGE_TAG ?= latest
IMG ?= ${IMAGE}:${IMAGE_TAG}
Expand Down Expand Up @@ -149,8 +150,37 @@ NET_OP_API_SLUG := github.com/vmware-tanzu/net-operator-api

BUILD_TYPE ?= dev
BUILD_NUMBER ?= 00000000

BUILD_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
BUILD_COMMIT ?= $(shell git rev-parse --short HEAD)
export BUILD_VERSION ?= $(shell git describe --always --match "v*" | sed 's/v//')

# ex. 1.2.3+abcdefg+4.5.6+hijklmn
ifeq (,$(strip $(PRDCT_VERSION)))
PRDCT_VERSION := $(shell git describe --always --match 'v*' 2>/dev/null | awk -F- '{gsub(/^v/,"",$$1);gsub(/^g/,"+",$$3);print $$1$$3}')
endif

ifeq (,$(strip $(BUILD_VERSION)))

# ex. 1.2.3+abcdefg+4.5.6+hijklmn
BUILD_VERSION := $(PRDCT_VERSION)

ifneq (, $(strip,$(BUILD_NUMBER)))
# ex. 1.2.3+abcdefg+4.5.6+hijklmn+7891011
BUILD_VERSION := $(BUILD_VERSION)+$(BUILD_NUMBER)
endif

endif

# ex. 1.2.3-abcdefg-4.5.6-hijklmn-7891011
IMAGE_VERSION ?= $(subst +,-,$(BUILD_VERSION))

IMAGE_FILE ?= $(abspath $(ARTIFACTS_DIR))/$(IMAGE)-$(GOOS)_$(GOARCH).tar

export BUILD_BRANCH
export BUILD_COMMIT
export BUILD_VERSION

CGO_ENABLED ?= 0

BUILDINFO_LDFLAGS = "\
-X $(PROJECT_SLUG)/pkg.BuildVersion=$(BUILD_VERSION) \
Expand Down Expand Up @@ -210,15 +240,15 @@ coverage: ## Show test coverage in browser
.PHONY: $(MANAGER) manager-only
manager-only: $(MANAGER) ## Build manager binary only
$(MANAGER):
CGO_ENABLED=0 go build -o $@ -ldflags $(BUILDINFO_LDFLAGS) .
GOOS="$(GOOS)" GOARCH="$(GOARCH)" CGO_ENABLED=$(CGO_ENABLED) go build -o $@ -ldflags $(BUILDINFO_LDFLAGS) .

.PHONY: manager
manager: prereqs generate lint-go manager-only ## Build manager binary

.PHONY: $(WEB_CONSOLE_VALIDATOR) web-console-validator-only
web-console-validator-only: $(WEB_CONSOLE_VALIDATOR) ## Build web-console-validator binary only
$(WEB_CONSOLE_VALIDATOR):
CGO_ENABLED=0 go build -o $@ -ldflags $(BUILDINFO_LDFLAGS) cmd/web-console-validator/main.go
GOOS="$(GOOS)" GOARCH="$(GOARCH)" CGO_ENABLED=$(CGO_ENABLED) go build -o $@ -ldflags $(BUILDINFO_LDFLAGS) cmd/web-console-validator/main.go

.PHONY: web-console-validator
web-console-validator: prereqs generate lint-go web-console-validator-only ## Build web-console-validator binary
Expand Down Expand Up @@ -709,21 +739,26 @@ docs-serve-docker: ## Serve docs w container

.PHONY: image-build
image-build: GOOS=linux
image-build: manager-only web-console-validator-only
image-build: ## Build container image
GOOS="$(GOOS)" GOARCH="$(GOARCH)" hack/build-container.sh \
-i "$(IMAGE)" \
-t "$(IMAGE_TAG)" \
-v "$(BUILD_VERSION)" \
-V "$(IMAGE_VERSION)" \
-n "$(BUILD_NUMBER)" \
-o "$(abspath $(ARTIFACTS_DIR))/$(IMAGE)-$(GOOS)_$(GOARCH).tar"
-B "$(BASE_IMAGE)" \
-o "$(IMAGE_FILE)"

.PHONY: image-build-amd64
image-build-amd64: GOARCH=amd64
image-build-amd64: image-build
image-build-amd64: ## Build amd64 container image
GOARCH=amd64 $(MAKE) image-build

.PHONY: image-build-arm64
image-build-arm64: GOARCH=arm64
image-build-arm64: image-build
image-build-arm64: ## Build arm64 container image
GOARCH=arm64 $(MAKE) image-build

.PHONY: image-push
image-push: ## Push container image
Expand Down
Loading