Skip to content

Commit 0073bd1

Browse files
committed
add support for building images using a ci pipeline
Signed-off-by: upodroid <cy@borg.dev>
1 parent 0e1d3ca commit 0073bd1

File tree

7 files changed

+110
-103
lines changed

7 files changed

+110
-103
lines changed

.gcloudignore

Whitespace-only changes.

Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
ARG ARCH=amd64
2-
FROM --platform=linux/${ARCH} gcr.io/distroless/static-debian12@sha256:87bce11be0af225e4ca761c40babb06d6d559f5767fbf7dc3c47f0f1a466b92c
1+
FROM gcr.io/distroless/static-debian12@sha256:87bce11be0af225e4ca761c40babb06d6d559f5767fbf7dc3c47f0f1a466b92c
32

4-
ADD etcd /usr/local/bin/
5-
ADD etcdctl /usr/local/bin/
6-
ADD etcdutl /usr/local/bin/
3+
ARG TARGETARCH
4+
ARG VERSION
5+
ARG BUILD_DIR
6+
7+
ADD ${BUILD_DIR}/etcd-${VERSION}-linux-${TARGETARCH}/etcd /usr/local/bin/
8+
ADD ${BUILD_DIR}/etcd-${VERSION}-linux-${TARGETARCH}/etcdctl /usr/local/bin/
9+
ADD ${BUILD_DIR}/etcd-${VERSION}-linux-${TARGETARCH}/etcdutl /usr/local/bin/
710

811
WORKDIR /var/etcd/
912
WORKDIR /var/lib/etcd/

cloudbuild.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# See https://cloud.google.com/cloud-build/docs/build-config
2+
timeout: 1800s
3+
options:
4+
machineType: "E2_HIGHCPU_32"
5+
steps:
6+
# Push the images
7+
- name: "gcr.io/k8s-staging-test-infra/gcb-docker-gcloud:v20251110-7ccd542560"
8+
id: images
9+
entrypoint: bash
10+
args:
11+
- scripts/build-release.sh
12+
env:
13+
- REGISTRY=$_REGISTRY
14+
- GCS_LOCATION=$_GCS_LOCATION
15+
- CI=1
16+
- BUILDX_NO_DEFAULT_ATTESTATIONS=1
17+
substitutions:
18+
_REGISTRY: gcr.io/k8s-staging-etcd #us-central1-docker.pkg.dev/k8s-staging-images/us-central1/etcd
19+
_GCS_LOCATION: k8s-staging-etcd/releases

scripts/build-binary.sh

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,28 @@ set -euo pipefail
1818
source ./scripts/test_lib.sh
1919

2020
VER=${1:-}
21-
REPOSITORY="${REPOSITORY:-git@github.com:etcd-io/etcd.git}"
2221

2322
if [ -z "$VER" ]; then
24-
echo "Usage: ${0} VERSION" >> /dev/stderr
25-
exit 255
23+
VER=$(git describe --tags --always --dirty)
2624
fi
2725

28-
function setup_env {
29-
local ver=${1}
30-
local proj=${2}
31-
32-
if [ ! -d "${proj}" ]; then
33-
run git clone "${REPOSITORY}"
34-
fi
35-
36-
pushd "${proj}" >/dev/null
37-
run git fetch --all
38-
run git checkout "${ver}"
39-
popd >/dev/null
40-
}
41-
4226

4327
function package {
4428
local target=${1}
45-
local srcdir="${2}/bin"
46-
47-
local ccdir="${srcdir}/${GOOS}_${GOARCH}"
48-
if [ -d "${ccdir}" ]; then
49-
srcdir="${ccdir}"
50-
fi
51-
local ext=""
52-
if [ "${GOOS}" == "windows" ]; then
53-
ext=".exe"
54-
fi
55-
for bin in etcd etcdctl etcdutl; do
56-
cp "${srcdir}/${bin}" "${target}/${bin}${ext}"
57-
done
5829

59-
cp etcd/README.md "${target}"/README.md
60-
cp etcd/etcdctl/README.md "${target}"/README-etcdctl.md
61-
cp etcd/etcdctl/READMEv2.md "${target}"/READMEv2-etcdctl.md
62-
cp etcd/etcdutl/README.md "${target}"/README-etcdutl.md
63-
64-
cp -R etcd/Documentation "${target}"/Documentation
30+
cp README.md "${target}"/README.md
31+
cp etcdctl/README.md "${target}"/README-etcdctl.md
32+
cp etcdctl/READMEv2.md "${target}"/READMEv2-etcdctl.md
33+
cp etcdutl/README.md "${target}"/README-etcdutl.md
34+
cp -R Documentation "${target}/"
6535
}
6636

6737
function main {
68-
local proj="etcd"
69-
7038
mkdir -p release
71-
cd release
72-
setup_env "${VER}" "${proj}"
73-
7439
local tarcmd=tar
75-
if [[ $(go env GOOS) == "darwin" ]]; then
76-
echo "Please use linux machine for release builds."
77-
exit 1
40+
if [ $(go env GOOS) == "darwin" ]; then
41+
tarcmd=gtar
7842
fi
79-
8043
for os in darwin windows linux; do
8144
export GOOS=${os}
8245
TARGET_ARCHS=("amd64")
@@ -94,20 +57,21 @@ function main {
9457
for TARGET_ARCH in "${TARGET_ARCHS[@]}"; do
9558
export GOARCH=${TARGET_ARCH}
9659

97-
pushd etcd >/dev/null
98-
GO_LDFLAGS="-s -w" ./scripts/build.sh
99-
popd >/dev/null
100-
101-
TARGET="etcd-${VER}-${GOOS}-${GOARCH}"
102-
mkdir "${TARGET}"
103-
package "${TARGET}" "${proj}"
60+
TARGET="release/etcd-${VER}-${GOOS}-${GOARCH}"
61+
BINDIR="${TARGET}" GO_LDFLAGS="-s -w" scripts/build.sh
62+
package "${TARGET}"
10463

10564
if [ ${GOOS} == "linux" ]; then
106-
${tarcmd} cfz "${TARGET}.tar.gz" "${TARGET}"
107-
echo "Wrote release/${TARGET}.tar.gz"
65+
# https://reproducible-builds.org/docs/archives/#full-example
66+
GZIP=-n ${tarcmd} --sort=name \
67+
--mtime="UTC 1970-01-01" \
68+
--owner=0 --group=0 --numeric-owner \
69+
--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \
70+
-zcf "${TARGET}.tar.gz" "${TARGET}"
71+
echo "Wrote ${TARGET}.tar.gz"
10872
else
109-
zip -qr "${TARGET}.zip" "${TARGET}"
110-
echo "Wrote release/${TARGET}.zip"
73+
zip -qrX "${TARGET}.zip" "${TARGET}"
74+
echo "Wrote ${TARGET}.zip"
11175
fi
11276
done
11377
done

scripts/build-docker.sh

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,57 @@
1515

1616
set -euo pipefail
1717

18-
if [ "$#" -ne 1 ]; then
19-
echo "Usage: $0 VERSION" >&2
20-
exit 1
21-
fi
18+
source ./scripts/test_lib.sh
2219

23-
VERSION=${1}
20+
VERSION=${1:-}
2421
if [ -z "$VERSION" ]; then
2522
echo "Usage: ${0} VERSION" >&2
2623
exit 1
2724
fi
2825

29-
ARCH=$(go env GOARCH)
30-
VERSION="${VERSION}-${ARCH}"
31-
DOCKERFILE="Dockerfile"
26+
PLATFORMS=${2:-"linux/amd64,linux/arm64,linux/ppc64le,linux/s390x"}
3227

33-
if [ -z "${BINARYDIR:-}" ]; then
34-
RELEASE="etcd-${1}"-$(go env GOOS)-${ARCH}
35-
BINARYDIR="${RELEASE}"
36-
TARFILE="${RELEASE}.tar.gz"
37-
TARURL="https://github.com/etcd-io/etcd/releases/download/${1}/${TARFILE}"
38-
if ! curl -f -L -o "${TARFILE}" "${TARURL}" ; then
39-
echo "Failed to download ${TARURL}."
40-
exit 1
41-
fi
42-
tar -zvxf "${TARFILE}"
28+
if [ -n "${CI:-}" ]; then
29+
docker run --privileged --rm tonistiigi/binfmt --install all
30+
docker buildx create \
31+
--name multiarch-multiplatform-builder \
32+
--driver docker-container \
33+
--bootstrap --use
4334
fi
4435

45-
BINARYDIR=${BINARYDIR:-.}
46-
BUILDDIR=${BUILDDIR:-.}
47-
48-
IMAGEDIR=${BUILDDIR}/image-docker
49-
50-
mkdir -p "${IMAGEDIR}"/var/etcd
51-
mkdir -p "${IMAGEDIR}"/var/lib/etcd
52-
cp "${BINARYDIR}"/etcd "${BINARYDIR}"/etcdctl "${BINARYDIR}"/etcdutl "${IMAGEDIR}"
53-
54-
cat ./"${DOCKERFILE}" > "${IMAGEDIR}"/Dockerfile
36+
if [ -z "${REGISTRY:-}" ]; then
37+
docker build --build-arg="VERSION=${VERSION}" \
38+
--build-arg="BUILD_DIR=${BUILD_DIR}" \
39+
--platform="${PLATFORMS}" \
40+
-t "gcr.io/etcd-development/etcd:${VERSION}" \
41+
-t "quay.io/coreos/etcd:${VERSION}" \
42+
.
43+
# we should deprecate publishing tags for specific architectures and use the multi-arch image
44+
for arch in amd64 arm64 ppc64le s390x; do
45+
log_callout "Building ${arch} docker image..."
46+
docker build --build-arg="VERSION=${VERSION}" \
47+
--build-arg="BUILD_DIR=${BUILD_DIR}" \
48+
--platform="linux/${arch}" \
49+
-t "gcr.io/etcd-development/etcd:${VERSION}-${arch}" \
50+
-t "quay.io/coreos/etcd:${VERSION}-${arch}" \
51+
.
52+
done
5553

56-
if [ -z "${TAG:-}" ]; then
57-
# Fix incorrect image "Architecture" using buildkit
58-
# From https://stackoverflow.com/q/72144329/
59-
DOCKER_BUILDKIT=1 docker build --build-arg="ARCH=${ARCH}" -t "gcr.io/etcd-development/etcd:${VERSION}" "${IMAGEDIR}"
60-
DOCKER_BUILDKIT=1 docker build --build-arg="ARCH=${ARCH}" -t "quay.io/coreos/etcd:${VERSION}" "${IMAGEDIR}"
6154
else
62-
docker build -t "${TAG}:${VERSION}" "${IMAGEDIR}"
55+
docker buildx build --build-arg="VERSION=${VERSION}" \
56+
--build-arg="BUILD_DIR=${BUILD_DIR}" \
57+
--platform="${PLATFORMS}" \
58+
-t "${REGISTRY}/etcd:${VERSION}" \
59+
--push \
60+
.
61+
# we should deprecate publishing tags for specific architectures and use the multi-arch image
62+
for arch in amd64 arm64 ppc64le s390x; do
63+
log_callout "Building ${arch} docker image..."
64+
docker buildx build --build-arg="VERSION=${VERSION}" \
65+
--build-arg="BUILD_DIR=${BUILD_DIR}" \
66+
--platform="linux/${arch}" \
67+
-t "${REGISTRY}/etcd:${VERSION}-${arch}" \
68+
--push \
69+
.
70+
done
6371
fi

scripts/build-release.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,36 @@ source ./scripts/test_lib.sh
2222

2323
VERSION=${1:-}
2424
if [ -z "${VERSION}" ]; then
25-
echo "Usage: ${0} VERSION" >> /dev/stderr
26-
exit 255
25+
VERSION=$(git describe --tags --always --dirty)
2726
fi
2827

2928
if ! command -v docker >/dev/null; then
3029
echo "cannot find docker"
3130
exit 1
3231
fi
3332

33+
if [ -n "${CI:-}" ]; then
34+
# there are few things missing in CI that we need to install
35+
apk --no-cache add zip
36+
fi
37+
3438
ETCD_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
3539

40+
VERSION=$(git describe --tags --always --dirty)
41+
3642
pushd "${ETCD_ROOT}" >/dev/null
3743
log_callout "Building etcd binary..."
3844
./scripts/build-binary.sh "${VERSION}"
3945

40-
for TARGET_ARCH in "amd64" "arm64" "ppc64le" "s390x"; do
41-
log_callout "Building ${TARGET_ARCH} docker image..."
42-
GOOS=linux GOARCH=${TARGET_ARCH} BINARYDIR=release/etcd-${VERSION}-linux-${TARGET_ARCH} BUILDDIR=release ./scripts/build-docker.sh "${VERSION}"
43-
done
46+
BUILD_DIR=release ./scripts/build-docker.sh "${VERSION}"
47+
find release -name '*.*' -type f -maxdepth 1 -exec sha256sum {} \; | sed "s~release/~~" > release/SHA256SUMS
48+
if [ -n "${CI:-}" ]; then
49+
# cloudbuild will copy contents of this folder to GCS
50+
echo "Copying release artifacts to release/cloudbuild/${VERSION}"
51+
mkdir -p "release/cloudbuild/${VERSION}"
52+
cp release/SHA256SUMS release/cloudbuild/"${VERSION}"/SHA256SUMS
53+
cp release/*.zip release/cloudbuild/"${VERSION}"/
54+
cp release/*.tar.gz release/cloudbuild/"${VERSION}"/
55+
gcloud storage cp --recursive "release/cloudbuild" "gs://${GCS_LOCATION}"
56+
fi
4457
popd >/dev/null

scripts/build_lib.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ tools_build() {
106106
run_build() {
107107
echo Running "$1"
108108
if $1; then
109-
log_success "SUCCESS: $1 (GOARCH=${GOARCH})"
109+
log_success "SUCCESS: $1 (GOOS=${GOOS},GOARCH=${GOARCH})"
110110
else
111-
log_error "FAIL: $1 (GOARCH=${GOARCH})"
111+
log_error "FAIL: $1 (GOOS=${GOOS},GOARCH=${GOARCH})"
112112
exit 2
113113
fi
114114
}

0 commit comments

Comments
 (0)