Skip to content

Commit f019303

Browse files
committed
Add hack/release.sh script
Create a new hack/release.sh script that can be used for building new container images and the clusterctl binary for both prod/GA and CI use. This script accepts arguments to optionally push the images to the registry and cloud storage buckets, and tags the images differently if they are designated as only for CI. This script is meant to be consumed by the top level Makefile in the future, but also can be used by developers directly.
1 parent 28645fa commit f019303

File tree

2 files changed

+267
-90
lines changed

2 files changed

+267
-90
lines changed

hack/build-container-images.sh

-90
This file was deleted.

hack/release.sh

+267
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#!/bin/bash
2+
3+
# Copyright 2019 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This script is used build new container images of the CAPV manager and
18+
# clusterctl. When invoked without arguments, the default behavior is to build
19+
# new ci images
20+
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
25+
# BASE_REPO is the root path of the image repository
26+
readonly BASE_IMAGE_REPO=gcr.io/cluster-api-provider-vsphere
27+
28+
# Release images
29+
readonly CAPV_MANAGER_IMAGE_RELEASE=${BASE_IMAGE_REPO}/release/manager
30+
readonly CAPV_MANIFESTS_IMAGE_RELEASE=${BASE_IMAGE_REPO}/release/manifests
31+
32+
# PR images
33+
readonly CAPV_MANAGER_IMAGE_PR=${BASE_IMAGE_REPO}/pr/manager
34+
readonly CAPV_MANIFESTS_IMAGE_PR=${BASE_IMAGE_REPO}/pr/manifests
35+
36+
# CI images
37+
readonly CAPV_MANAGER_IMAGE_CI=${BASE_IMAGE_REPO}/ci/manager
38+
readonly CAPV_MANIFESTS_IMAGE_CI=${BASE_IMAGE_REPO}/ci/manifests
39+
40+
AUTH=
41+
PUSH=
42+
LATEST=
43+
MANAGER_IMAGE_NAME=
44+
MANIFESTS_IMAGE_NAME=
45+
VERSION=$(git describe --dirty --always 2>/dev/null)
46+
GCR_KEY_FILE="${GCR_KEY_FILE:-}"
47+
48+
BUILD_RELEASE_TYPE="${BUILD_RELEASE_TYPE-}"
49+
50+
# If BUILD_RELEASE_TYPE is not set then check to see if this is a PR
51+
# or release build. This may still be overridden below with the "-t" flag.
52+
if [ -z "${BUILD_RELEASE_TYPE}" ]; then
53+
if hack/match-release-tag.sh >/dev/null 2>&1; then
54+
BUILD_RELEASE_TYPE=release
55+
else
56+
BUILD_RELEASE_TYPE=ci
57+
fi
58+
fi
59+
60+
USAGE="
61+
usage: ${0} [FLAGS]
62+
Builds and optionally pushes new images for Cluster API Provider vSphere (CAPV)
63+
64+
FLAGS
65+
-h show this help and exit
66+
-k path to GCR key file. Used to login to registry if specified
67+
(defaults to: ${GCR_KEY_FILE})
68+
-l tag the images as \"latest\" in addition to their version
69+
when used with -p, both tags will be pushed
70+
-p push the images to the public container registry
71+
-t the build/release type (defaults to ${BUILD_RELEASE_TYPE})
72+
one of [ci,pr,release]
73+
"
74+
75+
# Change directories to the parent directory of the one in which this
76+
# script is located.
77+
cd "$(dirname "${BASH_SOURCE[0]}")/.."
78+
79+
function error() {
80+
local exit_code="${?}"
81+
echo "${@}" 1>&2
82+
return "${exit_code}"
83+
}
84+
85+
function fatal() {
86+
error "${@}" || exit 1
87+
}
88+
89+
function build_images() {
90+
case "${BUILD_RELEASE_TYPE}" in
91+
ci)
92+
# A non-PR, non-release build. This is usually a build off of master
93+
MANAGER_IMAGE_NAME=${CAPV_MANAGER_IMAGE_CI}
94+
MANIFESTS_IMAGE_NAME=${CAPV_MANIFESTS_IMAGE_CI}
95+
;;
96+
pr)
97+
# A PR build
98+
MANAGER_IMAGE_NAME=${CAPV_MANAGER_IMAGE_PR}
99+
MANIFESTS_IMAGE_NAME=${CAPV_MANIFESTS_IMAGE_PR}
100+
;;
101+
release)
102+
# On an annotated tag
103+
MANAGER_IMAGE_NAME=${CAPV_MANAGER_IMAGE_RELEASE}
104+
MANIFESTS_IMAGE_NAME=${CAPV_MANIFESTS_IMAGE_RELEASE}
105+
;;
106+
esac
107+
108+
# Manager image
109+
echo "building ${MANAGER_IMAGE_NAME}:${VERSION}"
110+
docker build \
111+
-f Dockerfile \
112+
-t "${MANAGER_IMAGE_NAME}":"${VERSION}" \
113+
.
114+
if [ "${LATEST}" ]; then
115+
echo "tagging image ${MANAGER_IMAGE_NAME}:${VERSION} as latest"
116+
docker tag "${MANAGER_IMAGE_NAME}":"${VERSION}" "${MANAGER_IMAGE_NAME}":latest
117+
fi
118+
119+
# Manifests image
120+
echo "building ${MANIFESTS_IMAGE_NAME}:${VERSION}"
121+
docker build \
122+
-f hack/tools/generate-yaml/Dockerfile \
123+
-t "${MANIFESTS_IMAGE_NAME}":"${VERSION}" \
124+
--build-arg "CAPV_MANAGER_IMAGE=${MANAGER_IMAGE_NAME}:${VERSION}" \
125+
.
126+
if [ "${LATEST}" ]; then
127+
echo "tagging image ${MANIFESTS_IMAGE_NAME}:${VERSION} as latest"
128+
docker tag "${MANIFESTS_IMAGE_NAME}":"${VERSION}" "${MANIFESTS_IMAGE_NAME}":latest
129+
fi
130+
}
131+
132+
function logout() {
133+
if [ "${AUTH}" ]; then
134+
gcloud auth revoke
135+
fi
136+
}
137+
138+
function login() {
139+
# If GCR_KEY_FILE is set, use that service account to login
140+
if [ "${GCR_KEY_FILE}" ]; then
141+
trap logout EXIT
142+
gcloud auth activate-service-account --key-file "${GCR_KEY_FILE}" || fatal "unable to login"
143+
AUTH=1
144+
fi
145+
}
146+
147+
function push_images() {
148+
[ "${MANAGER_IMAGE_NAME}" ] || fatal "MANAGER_IMAGE_NAME not set"
149+
[ "${MANIFESTS_IMAGE_NAME}" ] || fatal "MANIFESTS_IMAGE_NAME not set"
150+
151+
login
152+
153+
# Manager image
154+
echo "pushing ${MANAGER_IMAGE_NAME}:${VERSION}"
155+
docker push "${MANAGER_IMAGE_NAME}":"${VERSION}"
156+
if [ "${LATEST}" ]; then
157+
echo "also pushing ${MANAGER_IMAGE_NAME}:${VERSION} as latest"
158+
docker push "${MANAGER_IMAGE_NAME}":latest
159+
fi
160+
161+
# Manifests image
162+
echo "pushing ${MANIFESTS_IMAGE_NAME}:${VERSION}"
163+
docker push "${MANIFESTS_IMAGE_NAME}":"${VERSION}"
164+
if [ "${LATEST}" ]; then
165+
echo "also pushing ${MANIFESTS_IMAGE_NAME}:${VERSION} as latest"
166+
docker push "${MANIFESTS_IMAGE_NAME}":latest
167+
fi
168+
}
169+
170+
function build_clusterctl() {
171+
for os in linux darwin; do
172+
# Don't build Darwin for PRs
173+
if [ "${BUILD_RELEASE_TYPE}" = "pr" ] && [ "${os}" == "darwin" ]; then
174+
continue
175+
fi
176+
echo "building clusterctl for ${os}"
177+
GOOS="${os}" GOARCH=amd64 make clusterctl-in-docker
178+
done
179+
}
180+
181+
function sha_sum() {
182+
{ sha256sum "${1}" || shasum -a 256 "${1}"; } 2>/dev/null > "${1}.sha256"
183+
}
184+
185+
function push_clusterctl() {
186+
local bucket
187+
case "${BUILD_RELEASE_TYPE}" in
188+
ci)
189+
bucket="capv-ci"
190+
;;
191+
pr)
192+
bucket="capv-pr"
193+
;;
194+
release)
195+
bucket="capv-release"
196+
;;
197+
esac
198+
199+
for os in linux darwin; do
200+
# Don't build Darwin for PRs
201+
if [ "${BUILD_RELEASE_TYPE}" = "pr" ] && [ "${os}" == "darwin" ]; then
202+
continue
203+
fi
204+
sha_sum "bin/clusterctl.${os}_amd64"
205+
echo "copying clusterctl version ${VERSION} for ${os} to ${bucket}"
206+
gsutil cp "bin/clusterctl.${os}_amd64" "gs://${bucket}/${VERSION}/bin/${os}/amd64/clusterctl"
207+
gsutil cp "bin/clusterctl.${os}_amd64.sha256" "gs://${bucket}/${VERSION}/bin/${os}/amd64/clusterctl.sha256"
208+
done
209+
}
210+
211+
# Start of main script
212+
while getopts ":hk:lpt:" opt; do
213+
case ${opt} in
214+
h)
215+
error "${USAGE}" && exit 1
216+
;;
217+
k)
218+
GCR_KEY_FILE="${OPTARG}"
219+
;;
220+
l)
221+
LATEST=1
222+
;;
223+
p)
224+
PUSH=1
225+
;;
226+
t)
227+
BUILD_RELEASE_TYPE="${OPTARG}"
228+
;;
229+
\?)
230+
error "invalid option: -${OPTARG} ${USAGE}" && exit 1
231+
;;
232+
:)
233+
error "option -${OPTARG} requires an argument" && exit 1
234+
;;
235+
esac
236+
done
237+
shift $((OPTIND-1))
238+
239+
# Verify the GCR_KEY_FILE exists if defined
240+
if [ "${GCR_KEY_FILE}" ]; then
241+
[ -e "${GCR_KEY_FILE}" ] || fatal "key file ${GCR_KEY_FILE} does not exist"
242+
fi
243+
244+
# Validate build/release type.
245+
case "${BUILD_RELEASE_TYPE}" in
246+
ci|pr|release)
247+
# do nothing
248+
;;
249+
*)
250+
fatal "invalid BUILD_RELEASE_TYPE: ${BUILD_RELEASE_TYPE}"
251+
;;
252+
esac
253+
254+
# make sure that Docker is available
255+
docker ps >/dev/null 2>&1 || fatal "Docker not available"
256+
257+
# build container images
258+
build_images
259+
260+
# build clusterctl
261+
build_clusterctl
262+
263+
# Optionally push artifacts
264+
if [ "${PUSH}" ]; then
265+
push_images
266+
push_clusterctl
267+
fi

0 commit comments

Comments
 (0)