Skip to content

Commit

Permalink
chore: rework default Dockerfile (#1165)
Browse files Browse the repository at this point in the history
<!-- markdownlint-disable MD041 -->
#### What this PR does / why we need it

uses make now to unify our build process to work exactly like we expect,
everytime

#### Which issue(s) this PR fixes
<!--
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
follows up on #1163 as
github went crazy due to me reverting to a previously detached commit
  • Loading branch information
jakobmoellerdev authored Nov 29, 2024
1 parent aeaa706 commit b07a7ff
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bin
/hack
/LICENSES
/local
/gen

/pkg/test*

Expand All @@ -35,3 +36,4 @@ bin

!go.*
!**/*.go
!.git
12 changes: 12 additions & 0 deletions .github/workflows/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ env:
components: '["ocmcli", "helminstaller", "helmdemo", "subchartsdemo", "ecrplugin"]'
IMAGE_PLATFORMS: 'linux/amd64 linux/arm64'
PLATFORMS: 'windows/amd64 darwin/arm64 darwin/amd64 linux/amd64 linux/arm64'
BUILDX_CACHE_PUSH: ${{ github.ref == 'refs/heads/main' }}
BUILDX_CACHE_REF_BASE: ghcr.io/${{ github.repository }}/buildx-cache

jobs:
define-matrix:
Expand Down Expand Up @@ -66,6 +68,14 @@ jobs:
with:
go-version-file: '${{ github.workspace }}/go.mod'
cache: false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get go environment for use with cache
run: |
echo "go_cache=$(go env GOCACHE)" >> $GITHUB_ENV
Expand Down Expand Up @@ -95,6 +105,8 @@ jobs:
VERSION=${{ inputs.version }} \
PLATFORMS="${{ env.PLATFORMS }}" \
IMAGE_PLATFORMS="${{ env.IMAGE_PLATFORMS }}" \
BUILDX_CACHE_REF=${{ env.BUILDX_CACHE_REF_BASE }}:${{ matrix.component }} \
BUILDX_CACHE_PUSH=${{ env.BUILDX_CACHE_PUSH }} \
make \
ctf descriptor describe
- name: Upload CTF
Expand Down
23 changes: 13 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
FROM golang:1.23-alpine3.20 AS build
FROM --platform=$BUILDPLATFORM golang:1.23-alpine3.20 AS build

RUN apk add --no-cache make git

WORKDIR /src
RUN go env -w GOMODCACHE=/root/.cache/go-build

COPY go.mod go.sum *.go VERSION ./

ARG GO_PROXY="https://proxy.golang.org"
ENV GOPROXY=${GO_PROXY}
RUN --mount=type=cache,target=/root/.cache/go-build go mod download
RUN go mod download

COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
export VERSION=$(go run api/version/generate/release_generate.go print-rc-version) && \
export NOW=$(date -u +%FT%T%z) && \
go build -trimpath -ldflags \
"-s -w -X ocm.software/ocm/api/version.gitVersion=$VERSION -X ocm.software/ocm/api/version.buildDate=$NOW" \
-o /bin/ocm ./cmds/ocm/main.go

ENV BUILD_FLAGS="-trimpath"

# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN make bin/ocm GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH}

FROM gcr.io/distroless/static-debian12:nonroot@sha256:d71f4b239be2d412017b798a0a401c44c3049a3ca454838473a4c32ed076bfea

COPY --from=build /bin/ocm /usr/local/bin/ocm
COPY --from=build /src/bin/ocm /usr/local/bin/ocm

# https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys
LABEL org.opencontainers.image.description="Open Component Model command line interface based on Distroless"
Expand Down
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ SOURCES := $(shell go list -f '{{$$I:=.Dir}}{{range .GoFiles }}{{$$I}}/{{.}} {{e
GOPATH := $(shell go env GOPATH)

NOW := $(shell date -u +%FT%T%z)
BUILD_FLAGS := "-s -w \
LD_FLAGS := "-s -w \
-X ocm.software/ocm/api/version.gitVersion=$(EFFECTIVE_VERSION) \
-X ocm.software/ocm/api/version.gitTreeState=$(GIT_TREE_STATE) \
-X ocm.software/ocm/api/version.gitCommit=$(COMMIT) \
-X ocm.software/ocm/api/version.buildDate=$(NOW)"
CGO_ENABLED := 0
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)

COMPONENTS ?= ocmcli helminstaller demoplugin ecrplugin helmdemo subchartsdemo

Expand All @@ -42,19 +44,19 @@ bin:
mkdir -p bin

bin/ocm: bin $(SOURCES)
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags $(BUILD_FLAGS) -o bin/ocm ./cmds/ocm
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(BUILD_FLAGS) -ldflags $(LD_FLAGS) -o bin/ocm ./cmds/ocm

bin/helminstaller: bin $(SOURCES)
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags $(BUILD_FLAGS) -o bin/helminstaller ./cmds/helminstaller
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(BUILD_FLAGS) -ldflags $(LD_FLAGS) -o bin/helminstaller ./cmds/helminstaller

bin/demo: bin $(SOURCES)
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags $(BUILD_FLAGS) -o bin/demo ./cmds/demoplugin
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(BUILD_FLAGS) -ldflags $(LD_FLAGS) -o bin/demo ./cmds/demoplugin

bin/cliplugin: bin $(SOURCES)
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags $(BUILD_FLAGS) -o bin/cliplugin ./cmds/cliplugin
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(BUILD_FLAGS) -ldflags $(LD_FLAGS) -o bin/cliplugin ./cmds/cliplugin

bin/ecrplugin: bin $(SOURCES)
CGO_ENABLED=$(CGO_ENABLED) go build -ldflags $(BUILD_FLAGS) -o bin/ecrplugin ./cmds/ecrplugin
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(BUILD_FLAGS) -ldflags $(LD_FLAGS) -o bin/ecrplugin ./cmds/ecrplugin

api: $(SOURCES)
go build ./api/...
Expand Down
14 changes: 11 additions & 3 deletions components/helminstaller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ PLATFORM := $(shell go env GOOS)/$(shell g
CACHE_DIR := $(shell go env GOCACHE)
MOD_CACHE_DIR := $(shell go env GOMODCACHE)


ifneq ($(BUILDX_CACHE_REF),)
ADDITIONAL_BUILDX_ARGS += --cache-from type=registry,ref=$(BUILDX_CACHE_REF)
ifeq ($(BUILDX_CACHE_PUSH),true)
ADDITIONAL_BUILDX_ARGS += --cache-to type=registry,ref=$(BUILDX_CACHE_REF)
endif
endif

CREDS ?=
# Define the path to the binary
OCM_BIN = $(REPO_ROOT)/bin/ocm
Expand All @@ -48,7 +56,7 @@ BUILD_FLAGS := "-s -w \
-X ocm.software/ocm/api/version.buildDate=$(NOW)"

CMDSRCS=$(shell find $(REPO_ROOT)/cmds/$(NAME) -type f)
OCMSRCS=$(shell find $(REPO_ROOT)/pkg -type f) $(REPO_ROOT)/go.*
OCMSRCS=$(shell find $(REPO_ROOT)/api -type f) $(REPO_ROOT)/go.*

ifeq ($(MULTI),true)
FLAGSUF = .multi
Expand Down Expand Up @@ -89,7 +97,7 @@ $(GEN)/image.$(NAME): $(GEN)/.exists Dockerfile $(CMDSRCS) $(OCMSRCS)
--build-arg CACHE_DIR=$(CACHE_DIR) \
--build-arg MOD_CACHE_DIR=$(MOD_CACHE_DIR) \
--build-arg EFFECTIVE_VERSION=$(EFFECTIVE_VERSION) \
--build-arg GIT_TREE_STATE=$(GIT_TREE_STATE); \
--build-arg GIT_TREE_STATE=$(GIT_TREE_STATE) $(ADDITIONAL_BUILDX_ARGS); \
@touch $(GEN)/image.$(NAME)

push-image:
Expand All @@ -108,7 +116,7 @@ $(GEN)/image.$(NAME).multi: $(GEN)/.exists Dockerfile $(CMDSRCS) $(OCMSRCS)
--build-arg CACHE_DIR=$(CACHE_DIR) \
--build-arg MOD_CACHE_DIR=$(MOD_CACHE_DIR) \
--build-arg EFFECTIVE_VERSION=$(EFFECTIVE_VERSION) \
--build-arg GIT_TREE_STATE=$(GIT_TREE_STATE); \
--build-arg GIT_TREE_STATE=$(GIT_TREE_STATE) $(ADDITIONAL_BUILDX_ARGS); \
done
@touch $(GEN)/image.$(NAME).multi

Expand Down
23 changes: 0 additions & 23 deletions components/ocmcli/Dockerfile

This file was deleted.

42 changes: 22 additions & 20 deletions components/ocmcli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ ifeq ($(MULTI),true)
FLAGSUF = .multi
endif

ifneq ($(BUILDX_CACHE_REF),)
ADDITIONAL_BUILDX_ARGS += --cache-from type=registry,ref=$(BUILDX_CACHE_REF)
ifeq ($(BUILDX_CACHE_PUSH),true)
ADDITIONAL_BUILDX_ARGS += --cache-to type=registry,ref=$(BUILDX_CACHE_REF),mode=max
endif
endif

CREDS ?=
# Define the path to the binary
OCM_BIN = $(REPO_ROOT)/bin/ocm
Expand Down Expand Up @@ -68,40 +75,35 @@ $(GEN)/build: $(GEN) $(GEN)/.exists $(CMDSRCS) $(OCMSRCS)
@touch $(GEN)/build

.PHONY: image
image: $(GEN)/image

$(GEN)/image: $(GEN)/.exists Dockerfile $(GEN)/build
image:
@PLATFORM_OS_OVERRIDE=$(PLATFORM_OS); \
if [ $$PLATFORM_OS_OVERRIDE == darwin ]; then \
echo; echo "Building linux instead of darwin as there's no native Docker platform for darwin"; echo; \
docker buildx build -t $(IMAGE):$(VERSION) --platform linux/$(PLATFORM_ARCH) --file Dockerfile $(REPO_ROOT) \
--build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \
--build-arg SUFFIX=$$(echo linux/$(PLATFORM_ARCH) | sed -e s:/:-:g); \
docker buildx build -t $(IMAGE):$(VERSION) --platform linux/$(PLATFORM_ARCH) --file $(REPO_ROOT)/Dockerfile $(REPO_ROOT) \
--label org.opencontainers.image.version=$(VERSION) \
--label org.opencontainers.image.revision=$(VERSION) $(ADDITIONAL_BUILDX_ARGS); \
else \
echo; echo "Building for $(PLATFORM_OS)/$(ARCH)"; echo; \
docker buildx build -t $(IMAGE):$(VERSION) --platform $(PLATFORM_OS)/$(PLATFORM_ARCH) --file Dockerfile $(REPO_ROOT) \
--build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \
--build-arg SUFFIX=$$(echo $(PLATFORM_OS)/$(PLATFORM_ARCH) | sed -e s:/:-:g); \
docker buildx build -t $(IMAGE):$(VERSION) --platform $(PLATFORM_OS)/$(PLATFORM_ARCH) --file $(REPO_ROOT)/Dockerfile $(REPO_ROOT) \
--label org.opencontainers.image.version=$(VERSION) \
--label org.opencontainers.image.revision=$(VERSION) $(ADDITIONAL_BUILDX_ARGS); \
fi
@touch $(GEN)/image

.PHONY: image.multi
image.multi: $(GEN)/image.multi

$(GEN)/image.multi: Dockerfile $(GEN)/build
image.multi:
for i in $(IMAGE_PLATFORMS); do \
tag=$$(echo $$i | sed -e s:/:-:g); \
echo building platform $$i; \
docker buildx build --load -t $(IMAGE):$(VERSION)-$$tag --platform $$i --file Dockerfile $(REPO_ROOT) \
--build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \
--build-arg SUFFIX=$$tag; \
tag=$$(echo $$i | sed -e s:/:-:g); \
echo building platform $$i; \
docker buildx build --load -t $(IMAGE):$(VERSION)-$$tag --platform $$i --file $(REPO_ROOT)/Dockerfile $(REPO_ROOT) \
--build-arg OCM_VERSION=$(EFFECTIVE_VERSION) \
--label org.opencontainers.image.version=$(VERSION) \
--label org.opencontainers.image.revision=$(VERSION) $(ADDITIONAL_BUILDX_ARGS); \
done
@touch $(GEN)/image.multi

.PHONY: ctf
ctf: $(GEN)/ctf

$(GEN)/ctf: $(OCM_BIN) $(GEN)/.exists $(GEN)/build $(GEN)/image$(FLAGSUF) component-constructor.yaml $(CHARTSRCS) Makefile
$(GEN)/ctf: $(OCM_BIN) $(GEN)/.exists $(GEN)/build image$(FLAGSUF) component-constructor.yaml $(CHARTSRCS) Makefile
@rm -rf "$(GEN)/ctf"
$(OCM) add componentversions \
--create \
Expand Down

0 comments on commit b07a7ff

Please sign in to comment.