Skip to content

Commit

Permalink
Align build steps - distinguish clean vs dev builds (#109)
Browse files Browse the repository at this point in the history
* Align build steps - distinguish clean vs dev builds

- Reducing the Dockerfile dependencies allows faster dev builds
- Optimize docker caching wrt oc-commands dependencies
- Align also with other repos wrt build date / version (it was not correctly implemented, with variable names mismatch)
- Remove duplicated "make build" definitions

* Remove unused cli.Dockerfile

* dockerfile move commands
  • Loading branch information
jotak authored Nov 13, 2024
1 parent fd448a8 commit 6de3cd0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/push_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ jobs:
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: build and push manifest with images
run: |
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.WF_VERSION }} make images
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.short_sha }} OCI_BUILD_OPTS="--label quay.expires-after=2w" make images
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.WF_VERSION }} CLEAN_BUILD=1 make images
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.short_sha }} CLEAN_BUILD=1 OCI_BUILD_OPTS="--label quay.expires-after=2w" make images
if [[ "main" == "$WF_VERSION" ]]; then
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=latest make images
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=latest CLEAN_BUILD=1 make images
fi
codecov:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push_image_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: get short sha
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: build and push manifest with images
run: OCI_BUILD_OPTS="--label quay.expires-after=2w" IMAGE_ORG=${{ env.WF_ORG }} IMAGE=${{ env.WF_REGISTRY }}/${{ env.WF_IMAGE }}:${{ env.short_sha }} make images
run: OCI_BUILD_OPTS="--label quay.expires-after=2w" IMAGE_ORG=${{ env.WF_ORG }} IMAGE=${{ env.WF_REGISTRY }}/${{ env.WF_IMAGE }}:${{ env.short_sha }} CLEAN_BUILD=1 make images
- name: make commands
run: USER=netobserv VERSION=${{ env.short_sha }} make commands
- name: upload commands
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
password: ${{ secrets.QUAY_SECRET }}
registry: quay.io
- name: build and push manifest with images
run: MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} make images
run: MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} CLEAN_BUILD=1 make images
- name: build plugin artifact
run: IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} make release
- name: create github release
Expand Down
16 changes: 8 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ ARG TARGETARCH=amd64
FROM docker.io/library/golang:1.22 as builder

ARG TARGETARCH
ARG TARGETPLATFORM
ARG VERSION="unknown"
ARG LDFLAGS

WORKDIR /opt/app-root

COPY cmd cmd
COPY main.go main.go
COPY go.mod go.mod
COPY go.sum go.sum
COPY vendor/ vendor/

# Build collector
RUN GOARCH=$TARGETARCH go build -ldflags "$LDFLAGS" -mod vendor -a -o build/network-observability-cli

# We still need Makefile & resources for oc-commands; copy them after go build for caching
COPY commands/ commands/
COPY res/ res/
COPY scripts/ scripts/
COPY vendor/ vendor/
COPY Makefile Makefile
COPY .mk/ .mk/

# Build collector
RUN GOARCH=$TARGETARCH make compile

# Embedd commands in case users want to pull it from collector image
# Embed commands in case users want to pull it from collector image
RUN USER=netobserv VERSION=main make oc-commands

# Prepare output dir
Expand Down
26 changes: 9 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@
# - use the VERSION as arg of the bundle target (e.g make tar-commands VERSION=0.0.2)
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
VERSION ?= main
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1)
TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true)
BUILD_SHA := $(shell git rev-parse --short HEAD)
BUILD_VERSION := $(TAG:v%=%)
ifneq ($(COMMIT), $(TAG_COMMIT))
BUILD_VERSION := $(BUILD_VERSION)-$(BUILD_SHA)
endif
ifneq ($(shell git status --porcelain),)
BUILD_VERSION := $(BUILD_VERSION)-dirty
endif

# Go architecture and targets images to build
GOARCH ?= amd64
Expand Down Expand Up @@ -45,20 +34,26 @@ IMAGE ?= $(IMAGE_TAG_BASE):$(VERSION)
PULL_POLICY ?=Always
# Agent image URL to deploy
AGENT_IMAGE ?= quay.io/netobserv/netobserv-ebpf-agent:main
OCI_BUILD_OPTS ?=

# Image building tool (docker / podman) - docker is preferred in CI
OCI_BIN_PATH := $(shell which docker 2>/dev/null || which podman)
OCI_BIN ?= $(shell basename ${OCI_BIN_PATH})
OCI_BUILD_OPTS ?=
KREW_PLUGIN ?=false

ifneq ($(CLEAN_BUILD),)
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
BUILD_SHA := $(shell git rev-parse --short HEAD)
LDFLAGS ?= -X 'main.buildVersion=${VERSION}-${BUILD_SHA}' -X 'main.buildDate=${BUILD_DATE}'
endif

GOLANGCI_LINT_VERSION = v1.54.2
YQ_VERSION = v4.43.1

# build a single arch target provided as argument
define build_target
echo 'building image for arch $(1)'; \
DOCKER_BUILDKIT=1 $(OCI_BIN) buildx build --load --build-arg TARGETARCH=$(1) ${OCI_BUILD_OPTS} -t ${IMAGE}-$(1) -f Dockerfile .;
DOCKER_BUILDKIT=1 $(OCI_BIN) buildx build --load --build-arg LDFLAGS="${LDFLAGS}" --build-arg TARGETARCH=$(1) ${OCI_BUILD_OPTS} -t ${IMAGE}-$(1) -f Dockerfile .;
endef

# push a single arch target image
Expand Down Expand Up @@ -100,10 +95,7 @@ vendors: ## Refresh vendors directory.
.PHONY: compile
compile: ## Build the binary
@echo "### Compiling project"
GOARCH=${GOARCH} go build -ldflags "-X main.version=${VERSION} -X 'main.buildVersion=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}'" -mod vendor -a -o $(OUTPUT)

.PHONY: build
build: fmt lint compile ## Build project (fmt + lint + compile)
GOARCH=${GOARCH} go build -mod vendor -a -o $(OUTPUT)

.PHONY: test
test: ## Test code using go test
Expand Down
7 changes: 0 additions & 7 deletions cli.Dockerfile

This file was deleted.

8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

var (
BuildVersion string
BuildDate string
buildVersion = "unknown"
buildDate = "unknown"
)

func main() {
// Initial log message
fmt.Printf("Starting %s:\n=====\nBuild Version: %s\nBuild Date: %s\n\n",
filepath.Base(os.Args[0]), BuildVersion, BuildDate)
fmt.Printf("Starting %s:\n=====\nBuild version: %s\nBuild date: %s\n\n",
filepath.Base(os.Args[0]), buildVersion, buildDate)

err := cmd.Execute()
if err != nil {
Expand Down

0 comments on commit 6de3cd0

Please sign in to comment.