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

feat(build): idempotent builds #5291

Merged
merged 20 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ jobs:
fi
fi
env
export CI=true
make ${MAKE_PARAMETERS} CI=true << parameters.target >>
- store_test_results:
path: build/reports
Expand Down
17 changes: 4 additions & 13 deletions mk/build.mk
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
BUILD_INFO_GIT_TAG ?= $(shell git describe --tags 2>/dev/null || echo unknown)
BUILD_INFO_GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo unknown)
BUILD_INFO_BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ" || echo unknown)
BUILD_INFO_VERSION ?= $(shell $(TOOLS_DIR)/releases/version.sh)
BUILD_INFO_ENVOY_VERSION ?= $(ENVOY_VERSION)

build_info_fields := \
version=$(BUILD_INFO_VERSION) \
gitTag=$(BUILD_INFO_GIT_TAG) \
gitCommit=$(BUILD_INFO_GIT_COMMIT) \
buildDate=$(BUILD_INFO_BUILD_DATE) \
Envoy=$(BUILD_INFO_ENVOY_VERSION)
build_info_ld_flags := $(foreach entry,$(build_info_fields), -X github.com/kumahq/kuma/pkg/version.$(entry))
build_info := $(shell $(TOOLS_DIR)/releases/version.sh)
BUILD_INFO_VERSION ?= $(firstword $(build_info))
BUILD_INFO_GIT_TAG ?= $(word 2, $(build_info))
build_info_ld_flags := $(shell $(TOOLS_DIR)/releases/build-info-flags.sh)

LD_FLAGS := -ldflags="-s -w $(build_info_ld_flags) $(EXTRA_LD_FLAGS)"
CGO_ENABLED := 0
Expand Down
17 changes: 11 additions & 6 deletions mk/docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,31 @@ export DOCKER_BUILDKIT := 1

.PHONY: image/kuma-cp
image/kuma-cp: build/kuma-cp/linux-${GOARCH} ## Dev: Rebuild `kuma-cp` Docker image
docker build -t $(KUMA_CP_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kuma-cp .
$(TOOLS_DIR)/releases/copy-image-artifacts.sh kuma-cp ${GOOS} ${GOARCH}
docker build -t $(KUMA_CP_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kuma-cp build/artifacts-${GOOS}-${GOARCH}/kuma-cp

.PHONY: image/kuma-dp
image/kuma-dp: build/kuma-dp/linux-${GOARCH} build/coredns/linux-${GOARCH} build/artifacts-linux-${GOARCH}/envoy/envoy ## Dev: Rebuild `kuma-dp` Docker image
docker build -t $(KUMA_DP_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} --build-arg ENVOY_VERSION=${ENVOY_VERSION} -f tools/releases/dockerfiles/Dockerfile.kuma-dp .
$(TOOLS_DIR)/releases/copy-image-artifacts.sh kuma-dp ${GOOS} ${GOARCH}
docker build -t $(KUMA_DP_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} --build-arg ENVOY_VERSION=${ENVOY_VERSION} -f tools/releases/dockerfiles/Dockerfile.kuma-dp build/artifacts-${GOOS}-${GOARCH}/kuma-dp

.PHONY: image/kumactl
image/kumactl: build/kumactl/linux-${GOARCH} ## Dev: Rebuild `kumactl` Docker image
docker build -t $(KUMACTL_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kumactl .
$(TOOLS_DIR)/releases/copy-image-artifacts.sh kumactl ${GOOS} ${GOARCH}
docker build -t $(KUMACTL_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kumactl build/artifacts-${GOOS}-${GOARCH}/kumactl

.PHONY: image/kuma-init
image/kuma-init: build/kumactl/linux-${GOARCH} ## Dev: Rebuild `kuma-init` Docker image
docker build -t $(KUMA_INIT_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kuma-init .
$(TOOLS_DIR)/releases/copy-image-artifacts.sh kuma-init ${GOOS} ${GOARCH}
docker build -t $(KUMA_INIT_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kuma-init build/artifacts-${GOOS}-${GOARCH}/kuma-init

.PHONY: image/kuma-cni
image/kuma-cni: build/kuma-cni/linux-${GOARCH} build/install-cni/linux-${GOARCH}
docker build -t $(KUMA_CNI_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kuma-cni .
$(TOOLS_DIR)/releases/copy-image-artifacts.sh kuma-cni ${GOOS} ${GOARCH}
docker build -t $(KUMA_CNI_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f tools/releases/dockerfiles/Dockerfile.kuma-cni build/artifacts-${GOOS}-${GOARCH}/kuma-cni

.PHONY: image/kuma-universal
image/kuma-universal: build/linux-${GOARCH}
image/kuma-universal: build/kuma-cp/linux-${GOARCH} build/kuma-dp/linux-${GOARCH} build/artifacts-linux-${GOARCH}/envoy/envoy build/coredns/linux-${GOARCH} build/kumactl/linux-${GOARCH} build/test-server/linux-${GOARCH}
docker build -t $(KUMA_UNIVERSAL_DOCKER_IMAGE) ${DOCKER_BUILD_ARGS} --build-arg ARCH=${GOARCH} --build-arg ENVOY_VERSION=${ENVOY_VERSION} --build-arg BASE_IMAGE_ARCH=${GOARCH} -f test/dockerfiles/Dockerfile.universal .

.PHONY: images
Expand Down
6 changes: 6 additions & 0 deletions test/dockerfiles/Dockerfile.universal.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
!build/artifacts-linux-arm64/kumactl/kumactl
!build/artifacts-linux-arm64/coredns/coredns
!build/artifacts-linux-arm64/test-server/test-server
!build/artifacts-darwin-arm64/kuma-cp/kuma-cp
!build/artifacts-darwin-arm64/kuma-dp/kuma-dp
!build/artifacts-darwin-arm64/envoy/*
!build/artifacts-darwin-arm64/kumactl/kumactl
!build/artifacts-darwin-arm64/coredns/coredns
!build/artifacts-darwin-arm64/test-server/test-server
!pkg/config/app/kuma-cp/kuma-cp.defaults.yaml
!tools/releases/templates/LICENSE
!tools/releases/templates/NOTICE
Expand Down
12 changes: 12 additions & 0 deletions tools/releases/build-info-flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

versionInfo=$("${BASH_SOURCE%/*}"/version.sh)

IFS=' ' read -r -a values <<< "$versionInfo"

fields=( "version" "gitTag" "gitCommit" "buildDate" "Envoy" )

for index in "${!fields[@]}"
do
echo -n "-X github.com/kumahq/kuma/pkg/version.${fields[index]}=${values[index]} "
done
36 changes: 36 additions & 0 deletions tools/releases/copy-image-artifacts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

# this script copies artifacts from a .dockeringore file to a build folder

if [[ $# -ne 3 ]]; then
usage
exit 1
fi

function usage() {
echo "Usage: $0 <component> <os> <arch>"
}

DOCKERIGNORE_FILE="tools/releases/dockerfiles/Dockerfile.$1.dockerignore"
GOOS="$2"
GOARCH="$3"
ARTIFACTS_DIR="build/artifacts-$GOOS-$GOARCH"
DESTINATION_DIR="$ARTIFACTS_DIR/$1"

function safe_cp() {
FILE="$1"
DIR_1=$(dirname "$FILE")
DEST="$2"
FINAL_DEST="$DEST/$DIR_1/"
# shellcheck disable=SC2086
mkdir -p "$FINAL_DEST" && cp -R $FILE "$FINAL_DEST"
}

export -f safe_cp

tail -n +2 "$DOCKERIGNORE_FILE" | cut -d '!' -f 2 | while read -r file
do
safe_cp "$file" "$DESTINATION_DIR"
done

find build | xargs -I {} touch -mt 201212211111 {}
10 changes: 6 additions & 4 deletions tools/releases/dockerfiles/Dockerfile.kuma-cni.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*
!build/artifacts-linux-arm64/kuma-cni/
!build/artifacts-linux-amd64/kuma-cni/
!build/artifacts-linux-amd64/install-cni/
!build/artifacts-linux-arm64/install-cni/
!build/artifacts-linux-arm64/kuma-cni/kuma-cni
!build/artifacts-linux-arm64/install-cni/install-cni
!build/artifacts-darwin-arm64/kuma-cni/kuma-cni
!build/artifacts-darwin-arm64/install-cni/install-cni
!build/artifacts-linux-amd64/kuma-cni/kuma-cni
!build/artifacts-linux-amd64/install-cni/install-cni
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*
!build/artifacts-linux-amd64/kuma-cp/kuma-cp
!build/artifacts-linux-arm64/kuma-cp/kuma-cp
!build/artifacts-darwin-arm64/kuma-cp/kuma-cp
!pkg/config/app/kuma-cp/kuma-cp.defaults.yaml
!tools/releases/templates/LICENSE
!tools/releases/templates/NOTICE
Expand Down
3 changes: 3 additions & 0 deletions tools/releases/dockerfiles/Dockerfile.kuma-dp.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
!build/artifacts-linux-arm64/kuma-dp/kuma-dp
!build/artifacts-linux-arm64/coredns/coredns
!build/artifacts-linux-arm64/envoy/*
!build/artifacts-darwin-arm64/kuma-dp/kuma-dp
!build/artifacts-darwin-arm64/coredns/coredns
!build/artifacts-darwin-arm64/envoy/*
!tools/releases/templates/LICENSE
!tools/releases/templates/NOTICE
!tools/releases/templates/README
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*
!build/artifacts-linux-amd64/kumactl/kumactl
!build/artifacts-linux-arm64/kumactl/kumactl
!build/artifacts-darwin-amd64/kumactl/kumactl
!build/artifacts-darwin-arm64/kumactl/kumactl
!tools/releases/templates/LICENSE
!tools/releases/templates/NOTICE-kumactl
!tools/releases/templates/README
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*
!build/artifacts-linux-amd64/kumactl/kumactl
!build/artifacts-linux-arm64/kumactl/kumactl
!build/artifacts-darwin-amd64/kumactl/kumactl
!build/artifacts-darwin-arm64/kumactl/kumactl
!tools/releases/templates/LICENSE
!tools/releases/templates/NOTICE-kumactl
!tools/releases/templates/README
!tools/releases/templates/install_missing_crds.sh
38 changes: 30 additions & 8 deletions tools/releases/version.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
#!/usr/bin/env bash

# Kuma version is built as follows:
# Kuma version info has the following format:
# version git-tag git-commit build-date envoy-version

# version is built as follows:
# 1) If a git tag is present on the current commit, then the version is a git tag (without a starting v)
# 2) If the branch is "release-X.Y" look at the existing tags and use either X.Y.0-<shortHash> if there's none or
# the latest tag with a patch version increased by one (.e.g if latest tag is X.Y.1 the version will be `X.Y.2-<shortHash>`)
# 3) In non release branch use `0.0.0-$shortHash`

# git-tag - if the current HEAD has a tag then use it otherwise it's "not-tagged"
# git-commit - HEAD sha
# build-date - local date if built on CI

# Note: this format must be changed carefully, other scripts depend on it
exactTag=$(git describe --exact-match --tags 2> /dev/null)
indexStatus=$(git diff --quiet && echo "clean-index" || echo "dirty-index")
shortHash=$(git rev-parse --short HEAD 2> /dev/null || echo "no-commit")
currentBranch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null || echo "no-branch")
envoyVersion=$("${BASH_SOURCE%/*}"/../envoy/version.sh)

if [[ -z "${CI}" ]]; then
versionDate="local-build"
describedTag="local-build"
longHash="local-build"
else
versionDate=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
describedTag=$(shell git describe --tags 2>/dev/null || echo "unknown")
longHash=$(git rev-parse HEAD 2>/dev/null || echo "no-commit")
fi

exactTag=$(git describe --exact-match --tags 2> /dev/null || echo "not-tagged")
# We only support tags of the format: "v?X.Y.Z(-<alphaNumericName>)?" all other tags will just be ignored and use the regular versioning scheme
if [[ ${exactTag} =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?$ ]]; then
echo "${exactTag/^v//}"
echo "${exactTag/^v//} $describedTag $longHash $versionDate $envoyVersion"
exit 0
fi

shortHash=$(git rev-parse --short HEAD 2> /dev/null)
currentBranch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [[ ${currentBranch} == release-* ]]; then
releasePrefix=${currentBranch//release-/}
lastGitTag=$(git tag -l | grep -E "^v?${releasePrefix}\.[0-9]+$" | sed 's/^v//'| sort -V | tail -1)
if [[ ${lastGitTag} ]]; then
IFS=. read -r major minor patch <<< "${lastGitTag}"
echo "${major}.${minor}.$((++patch))-preview.v${shortHash}"
version="${major}.${minor}.$((++patch))-preview.v${shortHash}"
else
echo "${releasePrefix}.0-preview.v${shortHash}"
version="${releasePrefix}.0-preview.v${shortHash}"
fi
else
echo "0.0.0-preview.v${shortHash}"
version="0.0.0-preview.v${shortHash}"
fi

echo "$version $describedTag $longHash $versionDate $envoyVersion"