Skip to content

Commit 3d49b34

Browse files
authored
Merge pull request #1034 from robscott/cherry-pick-webhook-tagging
Update image generation process with more consistent naming
2 parents c672930 + 2c62164 commit 3d49b34

File tree

3 files changed

+118
-13
lines changed

3 files changed

+118
-13
lines changed

Makefile

+26-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,33 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# We need all the Make variables exported as env vars.
16+
# Note that the ?= operator works regardless.
17+
1518
# Enable Go modules.
1619
export GO111MODULE=on
1720

18-
REGISTRY ?= gcr.io/k8s-staging-gateway-api
19-
TAG ?= dev
20-
COMMIT=$(shell git rev-parse --short HEAD)
21+
# The registry to push container images to.
22+
export REGISTRY ?= gcr.io/k8s-staging-gateway-api
23+
24+
# These are overridden by cloudbuild.yaml when run by Prow.
25+
26+
# Prow gives this a value of the form vYYYYMMDD-hash.
27+
# (It's similar to `git describe` output, and for non-tag
28+
# builds will give vYYYYMMDD-COMMITS-HASH where COMMITS is the
29+
# number of commits since the last tag.)
30+
export GIT_TAG ?= dev
31+
32+
# Prow gives this the reference it's called on.
33+
# The test-infra config job only allows our cloudbuild to
34+
# be called on `master` and semver tags, so this will be
35+
# set to one of those things.
36+
export BASE_REF ?= master
37+
38+
# The commit hash of the current checkout
39+
# Used to pass a binary version for master,
40+
# overridden to semver for tagged versions.
41+
export COMMIT=$(shell git rev-parse --short HEAD)
2142

2243
DOCKER ?= docker
2344
# TOP is the current directory where this Makefile lives.
@@ -84,16 +105,9 @@ docs:
84105
# Add them to spec page originally generated by mkdocs
85106
sed -i -e '/REPLACE_WITH_GENERATED_CONTENT/{r site/v1alpha2-spec.html' -e 'd}' site/v1alpha2/references/spec/index.html
86107

87-
.PHONY: build
88-
build:
89-
docker build --build-arg COMMIT=$(COMMIT) --build-arg TAG=$(TAG) \
90-
-t $(REGISTRY)/admission-server:$(TAG) .
91-
92108
.PHONY: release-staging
93-
release-staging: build
94-
docker push $(REGISTRY)/admission-server:$(TAG)
95-
docker tag $(REGISTRY)/admission-server:$(TAG) $(REGISTRY)/admission-server:latest
96-
docker push $(REGISTRY)/admission-server:latest
109+
release-staging:
110+
hack/build-and-push.sh
97111

98112
# Generate a virtualenv install, which is useful for hacking on the
99113
# docs since it installs mkdocs and all the right dependencies.

cloudbuild.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ steps:
1111
entrypoint: make
1212
env:
1313
- DOCKER_CLI_EXPERIMENTAL=enabled
14-
- TAG=$_GIT_TAG
14+
- GIT_TAG=$_GIT_TAG
15+
- BASE_REF=$_PULL_BASE_REF
1516
args:
1617
- release-staging
1718
substitutions:

hack/build-and-push.sh

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 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 file is run by cloudbuild, from cloudbuild.yaml, using `make release-staging`.
18+
19+
set -o errexit
20+
set -o nounset
21+
set -o pipefail
22+
23+
if [[ -z "${GIT_TAG-}" ]];
24+
then
25+
echo "GIT_TAG env var must be set and nonempty."
26+
exit 1
27+
fi
28+
29+
if [[ -z "${BASE_REF-}" ]];
30+
then
31+
echo "BASE_REF env var must be set and nonempty."
32+
exit 1
33+
fi
34+
35+
if [[ -z "${COMMIT-}" ]];
36+
then
37+
echo "COMMIT env var must be set and nonempty."
38+
exit 1
39+
fi
40+
41+
if [[ -z "${REGISTRY-}" ]];
42+
then
43+
echo "REGISTRY env var must be set and nonempty."
44+
exit 1
45+
fi
46+
47+
48+
LATEST=false
49+
50+
VERSION_TAG=$GIT_TAG
51+
52+
BINARY_VERSION=$COMMIT
53+
54+
# $BASE_REF has only two things that it can be set to by cloudbuild and Prow,
55+
# `master`, or a semver tag.
56+
# This is controlled by k8s.io/test-infra/config/jobs/image-pushing/k8s-staging-gateway-api.yaml.
57+
if [[ "${BASE_REF}" != "master" ]]
58+
then
59+
# Since we know this is built from a tag or release branch, we can set the VERSION_TAG
60+
VERSION_TAG="${BASE_REF}"
61+
# We want the binary version to show up correctly too.
62+
BINARY_VERSION="${BASE_REF}"
63+
# Use some bash magic to check if the semver does not end with -sometext, that
64+
# would indicate a prerelease version. If this is not a prerelease, then we want to set
65+
# the `latest` tag too.
66+
if [[ ! "${BASE_REF}" =~ -(.+)$ ]];
67+
then
68+
LATEST=true
69+
fi
70+
fi
71+
72+
# First, build the image, with the version info passed in.
73+
# Note that an image will *always* be built tagged with the GIT_TAG, so we know when it was built.
74+
docker build --build-arg COMMIT=${BINARY_VERSION} --build-arg TAG=${VERSION_TAG} \
75+
-t ${REGISTRY}/admission-server:${GIT_TAG} .
76+
77+
docker push ${REGISTRY}/admission-server:${GIT_TAG}
78+
79+
# Then, we add extra tags if required.
80+
if [[ $VERSION_TAG != $GIT_TAG ]]
81+
then
82+
docker tag ${REGISTRY}/admission-server:${GIT_TAG} ${REGISTRY}/admission-server:${VERSION_TAG}
83+
docker push ${REGISTRY}/admission-server:${VERSION_TAG}
84+
fi
85+
86+
if [[ $LATEST == true ]]
87+
then
88+
docker tag ${REGISTRY}/admission-server:${GIT_TAG} ${REGISTRY}/admission-server:latest
89+
docker push ${REGISTRY}/admission-server:latest
90+
fi

0 commit comments

Comments
 (0)