Skip to content

Commit 29e6958

Browse files
committed
Add another Makefile target for publish action
1 parent e1256cf commit 29e6958

File tree

8 files changed

+163
-27
lines changed

8 files changed

+163
-27
lines changed

Makefile

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,43 +117,25 @@ goimports: localbin ## Download goimports locally if necessary. If wrong version
117117
echo $(FORMATTER_VERSION) > hack/goimports_version )
118118

119119
##@ Build
120-
121-
.PHONY: build
122-
build: manifests generate fmt vet ## Build manager binary.
123-
go build -o bin/manager cmd/main.go
120+
build: generate ## Builds binaries for all components specified in COMPONENTS and all platforms specified in PLATFORMS.
121+
@PLATFORMS=$(PLATFORMS) COMPONENTS=control-plane-operator ./hack/common/build-binary.sh
124122

125123
.PHONY: run
126124
run: manifests generate fmt vet ## Run a controller from your host.
127125
go run ./cmd/main.go start
128126

129-
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
130-
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
131-
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
132-
.PHONY: docker-build
133-
docker-build: ## Build docker image with the manager.
134-
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -a -o bin/manager-linux.$(GOARCH) cmd/main.go
135-
$(CONTAINER_TOOL) build --platform $(CONTAINER_PLATFORM) -t ${IMG} .
127+
PLATFORMS ?= linux/arm64,linux/amd64
128+
EFFECTIVE_VERSION ?=$(shell cat ./VERSION)
129+
130+
.PHONY: image-build
131+
image-build: ## Builds the docker images for all components specified in COMPONENTS and all platforms specified in PLATFORMS. Requires 'make build' to have run before.
132+
@PLATFORMS=$(PLATFORMS) COMPONENTS=control-plane-operator EFFECTIVE_VERSION=$(EFFECTIVE_VERSION) ./hack/common/build-image.sh
133+
136134

137135
.PHONY: docker-push
138136
docker-push: ## Push docker image with the manager.
139137
$(CONTAINER_TOOL) push ${IMG}
140138

141-
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
142-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
143-
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
144-
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
145-
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
146-
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
147-
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
148-
.PHONY: docker-buildx
149-
docker-buildx: ## Build and push docker image for the manager for cross-platform support
150-
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
151-
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
152-
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
153-
$(CONTAINER_TOOL) buildx use project-v3-builder
154-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
155-
- $(CONTAINER_TOOL) buildx rm project-v3-builder
156-
rm Dockerfile.cross
157139

158140
##@ Deployment
159141

hack/common/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Use distroless as minimal base image to package the component binary
2+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
3+
FROM gcr.io/distroless/static:nonroot
4+
ARG TARGETOS
5+
ARG TARGETARCH
6+
ARG COMPONENT
7+
WORKDIR /
8+
COPY bin/$COMPONENT-$TARGETOS.$TARGETARCH /<component>
9+
USER 65532:65532
10+
11+
# docker doesn't substitue args in ENTRYPOINT, so we replace this during the build script
12+
ENTRYPOINT ["/<component>"]

hack/common/build-binary.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
source "$(realpath "$(dirname $0)/environment.sh")"
5+
6+
echo
7+
echo "> Building binaries ..."
8+
(
9+
cd "$PROJECT_ROOT"
10+
for comp in ${COMPONENTS//,/ }; do
11+
for pf in ${PLATFORMS//,/ }; do
12+
echo "> Building binary for component '$comp' ($pf) ..." | indent 1
13+
os=${pf%/*}
14+
arch=${pf#*/}
15+
CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -a -o bin/${comp}-${os}.${arch} cmd/main.go | indent 2
16+
done
17+
done
18+
)

hack/common/build-image.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
source "$(realpath "$(dirname $0)/environment.sh")"
5+
6+
if [[ -z ${IMAGE_REGISTRY:-} ]]; then
7+
IMAGE_REGISTRY=$("$COMMON_SCRIPT_DIR/get-registry.sh" -i)
8+
fi
9+
10+
VERSION=$("$COMMON_SCRIPT_DIR/get-version.sh")
11+
12+
DOCKER_BUILDER_NAME="mcp-multiarch-builder"
13+
if ! docker buildx ls | grep "$DOCKER_BUILDER_NAME" >/dev/null; then
14+
docker buildx create --name "$DOCKER_BUILDER_NAME"
15+
fi
16+
17+
# remove temporary Dockerfile on exit
18+
trap "rm -f \"${PROJECT_ROOT}/Dockerfile.tmp\"" EXIT
19+
20+
echo
21+
echo "> Building images ..."
22+
for comp in ${COMPONENTS//,/ }; do
23+
for pf in ${PLATFORMS//,/ }; do
24+
os=${pf%/*}
25+
arch=${pf#*/}
26+
img="${IMAGE_REGISTRY}/${comp}:${VERSION}-${os}-${arch}"
27+
echo "> Building image for component '$comp' ($pf): $img ..." | indent 1
28+
cat "${COMMON_SCRIPT_DIR}/Dockerfile" | sed "s/<component>/$comp/g" > "${PROJECT_ROOT}/Dockerfile.tmp"
29+
docker buildx build --builder ${DOCKER_BUILDER_NAME} --load --build-arg COMPONENT=${comp} --platform ${pf} -t $img -f Dockerfile.tmp "${PROJECT_ROOT}" | indent 2
30+
done
31+
done
32+
33+
docker buildx rm "$DOCKER_BUILDER_NAME"

hack/common/environment.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
export COMMON_SCRIPT_DIR="$(realpath "$(dirname ${BASH_SOURCE[0]})")"
4+
source "$COMMON_SCRIPT_DIR/lib.sh"
5+
export PROJECT_ROOT="${PROJECT_ROOT:-$(realpath "$COMMON_SCRIPT_DIR/../..")}"
6+
export COMPONENT_DEFINITION_FILE="${COMPONENT_DEFINITION_FILE:-"$PROJECT_ROOT/components/components.yaml"}"
7+
8+
export LOCALBIN="${LOCALBIN:-"$PROJECT_ROOT/bin"}"
9+
export HELM="${HELM:-"$LOCALBIN/helm"}"
10+
export JQ="${JQ:-"$LOCALBIN/jq"}"
11+
export FORMATTER=${FORMATTER:-"$LOCALBIN/goimports"}
12+
export OCM="${OCM:-"$LOCALBIN/ocm"}"
13+
export YAML2JSON="${YAML2JSON:-"$LOCALBIN/yaml2json"}"
14+
15+
if [[ -f "$COMMON_SCRIPT_DIR/../environment.sh" ]]; then
16+
source "$COMMON_SCRIPT_DIR/../environment.sh"
17+
fi

hack/common/get-registry.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash -eu
2+
3+
set -euo pipefail
4+
5+
if [[ -z ${BASE_REGISTRY:-} ]]; then
6+
BASE_REGISTRY=ghcr.io/openmcp-project
7+
fi
8+
9+
if [[ -z ${IMAGE_REGISTRY:-} ]]; then
10+
IMAGE_REGISTRY=$BASE_REGISTRY
11+
fi
12+
if [[ -z ${CHART_REGISTRY:-} ]]; then
13+
CHART_REGISTRY=$BASE_REGISTRY/charts
14+
fi
15+
if [[ -z ${COMPONENT_REGISTRY:-} ]]; then
16+
COMPONENT_REGISTRY=$BASE_REGISTRY/components
17+
fi
18+
19+
mode="BASE_"
20+
21+
while [[ "$#" -gt 0 ]]; do
22+
case ${1:-} in
23+
"-i"|"--image")
24+
mode="IMAGE_"
25+
;;
26+
"-h"|"--helm")
27+
mode="CHART_"
28+
;;
29+
"-c"|"--component")
30+
mode="COMPONENT_"
31+
;;
32+
*)
33+
echo "invalid argument: $1" 1>&2
34+
exit 1
35+
;;
36+
esac
37+
shift
38+
done
39+
40+
eval echo "\$${mode}REGISTRY"

hack/common/get-version.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash -eu
2+
3+
if [[ -n ${EFFECTIVE_VERSION:-} ]] ; then
4+
# running in the pipeline use the provided EFFECTIVE_VERSION
5+
echo "$EFFECTIVE_VERSION"
6+
exit 0
7+
fi
8+
9+
set -euo pipefail
10+
source "$(realpath "$(dirname $0)/environment.sh")"
11+
12+
VERSION="$(cat "${PROJECT_ROOT}/VERSION")"
13+
14+
(
15+
cd "$PROJECT_ROOT"
16+
17+
if [[ "$VERSION" = *-dev ]] ; then
18+
VERSION="$VERSION-$(git rev-parse HEAD)"
19+
fi
20+
21+
echo "$VERSION"
22+
)

hack/common/lib.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# pipe some text into 'indent X' to indent each line by X levels (one 'level' being two spaces)
4+
function indent() {
5+
local level=${1:-""}
6+
if [[ -z "$level" ]]; then
7+
level=1
8+
fi
9+
local spaces=$(($level * 2))
10+
local iv=$(printf %${spaces}s)
11+
sed "s/^/$iv/"
12+
}

0 commit comments

Comments
 (0)