Skip to content

Commit 6de3cd0

Browse files
authored
Align build steps - distinguish clean vs dev builds (#109)
* 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
1 parent fd448a8 commit 6de3cd0

File tree

7 files changed

+26
-41
lines changed

7 files changed

+26
-41
lines changed

.github/workflows/push_image.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ jobs:
3535
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
3636
- name: build and push manifest with images
3737
run: |
38-
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.WF_VERSION }} make images
39-
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
38+
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.WF_VERSION }} CLEAN_BUILD=1 make images
39+
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
4040
if [[ "main" == "$WF_VERSION" ]]; then
41-
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=latest make images
41+
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=latest CLEAN_BUILD=1 make images
4242
fi
4343
4444
codecov:

.github/workflows/push_image_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- name: get short sha
3838
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
3939
- name: build and push manifest with images
40-
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
40+
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
4141
- name: make commands
4242
run: USER=netobserv VERSION=${{ env.short_sha }} make commands
4343
- name: upload commands

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
password: ${{ secrets.QUAY_SECRET }}
4343
registry: quay.io
4444
- name: build and push manifest with images
45-
run: MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} make images
45+
run: MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} CLEAN_BUILD=1 make images
4646
- name: build plugin artifact
4747
run: IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} make release
4848
- name: create github release

Dockerfile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ ARG TARGETARCH=amd64
55
FROM docker.io/library/golang:1.22 as builder
66

77
ARG TARGETARCH
8-
ARG TARGETPLATFORM
9-
ARG VERSION="unknown"
8+
ARG LDFLAGS
109

1110
WORKDIR /opt/app-root
1211

1312
COPY cmd cmd
1413
COPY main.go main.go
1514
COPY go.mod go.mod
1615
COPY go.sum go.sum
16+
COPY vendor/ vendor/
17+
18+
# Build collector
19+
RUN GOARCH=$TARGETARCH go build -ldflags "$LDFLAGS" -mod vendor -a -o build/network-observability-cli
20+
21+
# We still need Makefile & resources for oc-commands; copy them after go build for caching
1722
COPY commands/ commands/
1823
COPY res/ res/
1924
COPY scripts/ scripts/
20-
COPY vendor/ vendor/
2125
COPY Makefile Makefile
2226
COPY .mk/ .mk/
23-
24-
# Build collector
25-
RUN GOARCH=$TARGETARCH make compile
26-
27-
# Embedd commands in case users want to pull it from collector image
27+
# Embed commands in case users want to pull it from collector image
2828
RUN USER=netobserv VERSION=main make oc-commands
2929

3030
# Prepare output dir

Makefile

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44
# - use the VERSION as arg of the bundle target (e.g make tar-commands VERSION=0.0.2)
55
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
66
VERSION ?= main
7-
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
8-
TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1)
9-
TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true)
10-
BUILD_SHA := $(shell git rev-parse --short HEAD)
11-
BUILD_VERSION := $(TAG:v%=%)
12-
ifneq ($(COMMIT), $(TAG_COMMIT))
13-
BUILD_VERSION := $(BUILD_VERSION)-$(BUILD_SHA)
14-
endif
15-
ifneq ($(shell git status --porcelain),)
16-
BUILD_VERSION := $(BUILD_VERSION)-dirty
17-
endif
187

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

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

44+
ifneq ($(CLEAN_BUILD),)
45+
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
46+
BUILD_SHA := $(shell git rev-parse --short HEAD)
47+
LDFLAGS ?= -X 'main.buildVersion=${VERSION}-${BUILD_SHA}' -X 'main.buildDate=${BUILD_DATE}'
48+
endif
49+
5550
GOLANGCI_LINT_VERSION = v1.54.2
5651
YQ_VERSION = v4.43.1
5752

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

6459
# push a single arch target image
@@ -100,10 +95,7 @@ vendors: ## Refresh vendors directory.
10095
.PHONY: compile
10196
compile: ## Build the binary
10297
@echo "### Compiling project"
103-
GOARCH=${GOARCH} go build -ldflags "-X main.version=${VERSION} -X 'main.buildVersion=${BUILD_VERSION}' -X 'main.buildDate=${BUILD_DATE}'" -mod vendor -a -o $(OUTPUT)
104-
105-
.PHONY: build
106-
build: fmt lint compile ## Build project (fmt + lint + compile)
98+
GOARCH=${GOARCH} go build -mod vendor -a -o $(OUTPUT)
10799

108100
.PHONY: test
109101
test: ## Test code using go test

cli.Dockerfile

Lines changed: 0 additions & 7 deletions
This file was deleted.

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
)
1111

1212
var (
13-
BuildVersion string
14-
BuildDate string
13+
buildVersion = "unknown"
14+
buildDate = "unknown"
1515
)
1616

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

2222
err := cmd.Execute()
2323
if err != nil {

0 commit comments

Comments
 (0)