Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 17dde9e

Browse files
authored
Merge pull request #87 from pohly/cloud-build
cloud build: initial set of shared files
2 parents 9084fec + bd41690 commit 17dde9e

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

Diff for: build.make

+63
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,69 @@ build: $(CMDS:%=build-%)
105105
container: $(CMDS:%=container-%)
106106
push: $(CMDS:%=push-%)
107107

108+
# Additional parameters are needed when pushing to a local registry,
109+
# see https://github.com/docker/buildx/issues/94.
110+
# However, that then runs into https://github.com/docker/cli/issues/2396.
111+
#
112+
# What works for local testing is:
113+
# make push-multiarch PULL_BASE_REF=master REGISTRY_NAME=<your account on dockerhub.io> BUILD_PLATFORMS="linux amd64; windows amd64 .exe; linux ppc64le -ppc64le; linux s390x -s390x"
114+
DOCKER_BUILDX_CREATE_ARGS ?=
115+
116+
# This target builds a multiarch image for one command using Moby BuildKit builder toolkit.
117+
# Docker Buildx is included in Docker 19.03.
118+
#
119+
# ./cmd/<command>/Dockerfile[.Windows] is used if found, otherwise Dockerfile[.Windows].
120+
# BUILD_PLATFORMS determines which individual images are included in the multiarch image.
121+
# PULL_BASE_REF must be set to 'master', 'release-x.y', or a tag name, and determines
122+
# the tag for the resulting multiarch image.
123+
push-multiarch-%: check-pull-base-ref build-%
124+
set -ex; \
125+
DOCKER_CLI_EXPERIMENTAL=enabled; \
126+
export DOCKER_CLI_EXPERIMENTAL; \
127+
docker buildx create $(DOCKER_BUILDX_CREATE_ARGS) --use --name multiarchimage-buildertest; \
128+
trap "docker buildx rm multiarchimage-buildertest" EXIT; \
129+
dockerfile_linux=$$(if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi); \
130+
dockerfile_windows=$$(if [ -e ./cmd/$*/Dockerfile.Windows ]; then echo ./cmd/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \
131+
if [ '$(BUILD_PLATFORMS)' ]; then build_platforms='$(BUILD_PLATFORMS)'; else build_platforms="linux amd64"; fi; \
132+
pushMultiArch () { \
133+
tag=$$1; \
134+
echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do \
135+
docker buildx build --push \
136+
--tag $(IMAGE_NAME):$$arch-$$os-$$tag \
137+
--platform=$$os/$$arch \
138+
--file $$(eval echo \$${dockerfile_$$os}) \
139+
--build-arg binary=./bin/$*$$suffix \
140+
--label revision=$(REV) \
141+
.; \
142+
done; \
143+
images=$$(echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do echo $(IMAGE_NAME):$$arch-$$os-$$tag; done); \
144+
docker manifest create --amend $(IMAGE_NAME):$$tag $$images; \
145+
docker manifest push -p $(IMAGE_NAME):$$tag; \
146+
}; \
147+
if [ $(PULL_BASE_REF) = "master" ]; then \
148+
: "creating or overwriting canary image"; \
149+
pushMultiArch canary; \
150+
elif echo $(PULL_BASE_REF) | grep -q -e 'release-*' ; then \
151+
: "creating or overwriting canary image for release branch"; \
152+
release_canary_tag=$$(echo $(PULL_BASE_REF) | cut -f2 -d '-')-canary; \
153+
pushMultiArch $$release_canary_tag; \
154+
elif docker pull $(IMAGE_NAME):$(PULL_BASE_REF) 2>&1 | tee /dev/stderr | grep -q "manifest for $(IMAGE_NAME):$(PULL_BASE_REF) not found"; then \
155+
: "creating release image"; \
156+
pushMultiArch $(PULL_BASE_REF); \
157+
else \
158+
: "ERROR: release image $(IMAGE_NAME):$(PULL_BASE_REF) already exists: a new tag is required!"; \
159+
exit 1; \
160+
fi
161+
162+
.PHONY: check-pull-base-ref
163+
check-pull-base-ref:
164+
if ! [ "$(PULL_BASE_REF)" ]; then \
165+
echo >&2 "ERROR: PULL_BASE_REF must be set to 'master', 'release-x.y', or a tag name."; \
166+
exit 1; \
167+
fi
168+
169+
push-multiarch: $(CMDS:%=push-multiarch-%)
170+
108171
clean:
109172
-rm -rf bin
110173

Diff for: cloudbuild.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /bin/bash
2+
3+
# shellcheck disable=SC1091
4+
. release-tools/prow.sh
5+
6+
gcr_cloud_build

Diff for: cloudbuild.yaml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# A configuration file for multi-arch image building with the Google cloud build service.
2+
#
3+
# Repos using this file must:
4+
# - import csi-release-tools
5+
# - add a symlink cloudbuild.yaml -> release-tools/cloudbuild.yaml
6+
# - add a .cloudbuild.sh which can be a custom file or a symlink
7+
# to release-tools/cloudbuild.sh
8+
# - accept "binary" as build argument in their Dockerfile(s) (see
9+
# https://github.com/pohly/node-driver-registrar/blob/3018101987b0bb6da2a2657de607174d6e3728f7/Dockerfile#L4-L6)
10+
# because binaries will get built for different architectures and then
11+
# get copied from the built host into the container image
12+
#
13+
# See https://github.com/kubernetes/test-infra/blob/master/config/jobs/image-pushing/README.md
14+
# for more details on image pushing process in Kubernetes.
15+
16+
# This must be specified in seconds. If omitted, defaults to 600s (10 mins).
17+
timeout: 1200s
18+
# This prevents errors if you don't use both _GIT_TAG and _PULL_BASE_REF,
19+
# or any new substitutions added in the future.
20+
options:
21+
substitution_option: ALLOW_LOOSE
22+
steps:
23+
# The image must contain bash and curl. Ideally it should also contain
24+
# the desired version of Go (currently defined in release-tools/travis.yml),
25+
# but that just speeds up the build and is not required.
26+
- name: 'gcr.io/k8s-testimages/gcb-docker-gcloud:v20200421-a2bf5f8'
27+
entrypoint: ./.cloudbuild.sh
28+
env:
29+
- GIT_TAG=${_GIT_TAG}
30+
- PULL_BASE_REF=${_PULL_BASE_REF}
31+
- REGISTRY_NAME=gcr.io/${_STAGING_PROJECT}
32+
- HOME=/root
33+
substitutions:
34+
# _GIT_TAG will be filled with a git-based tag for the image, of the form vYYYYMMDD-hash, and
35+
# can be used as a substitution.
36+
_GIT_TAG: '12345'
37+
# _PULL_BASE_REF will contain the ref that was pushed to trigger this build -
38+
# a branch like 'master' or 'release-0.2', or a tag like 'v0.2'.
39+
_PULL_BASE_REF: 'master'
40+
# The default gcr.io staging project for Kubernetes-CSI
41+
# (=> https://console.cloud.google.com/gcr/images/k8s-staging-csi/GLOBAL).
42+
# Might be overridden in the Prow build job for a repo which wants
43+
# images elsewhere.
44+
_STAGING_PROJECT: 'k8s-staging-csi'

Diff for: prow.sh

+14
Original file line numberDiff line numberDiff line change
@@ -1189,3 +1189,17 @@ main () {
11891189

11901190
return "$ret"
11911191
}
1192+
1193+
# This function can be called by a repo's top-level cloudbuild.sh:
1194+
# it handles environment set up in the GCR cloud build and then
1195+
# invokes "make push-multiarch" to do the actual image building.
1196+
gcr_cloud_build () {
1197+
# Register gcloud as a Docker credential helper.
1198+
# Required for "docker buildx build --push".
1199+
gcloud auth configure-docker
1200+
1201+
# Extract tag-n-hash value from GIT_TAG (form vYYYYMMDD-tag-n-hash) for REV value.
1202+
REV=v$(echo "$GIT_TAG" | cut -f3- -d 'v')
1203+
1204+
run_with_go "${CSI_PROW_GO_VERSION_BUILD}" make push-multiarch REV="${REV}" REGISTRY_NAME="${REGISTRY_NAME}" BUILD_PLATFORMS="${CSI_PROW_BUILD_PLATFORMS}"
1205+
}

0 commit comments

Comments
 (0)