Skip to content

Commit

Permalink
Update image generation process with more consistent naming
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Young <[email protected]>
  • Loading branch information
Nick Young authored and robscott committed Mar 9, 2022
1 parent c672930 commit 2c62164
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 13 deletions.
38 changes: 26 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# We need all the Make variables exported as env vars.
# Note that the ?= operator works regardless.

# Enable Go modules.
export GO111MODULE=on

REGISTRY ?= gcr.io/k8s-staging-gateway-api
TAG ?= dev
COMMIT=$(shell git rev-parse --short HEAD)
# The registry to push container images to.
export REGISTRY ?= gcr.io/k8s-staging-gateway-api

# These are overridden by cloudbuild.yaml when run by Prow.

# Prow gives this a value of the form vYYYYMMDD-hash.
# (It's similar to `git describe` output, and for non-tag
# builds will give vYYYYMMDD-COMMITS-HASH where COMMITS is the
# number of commits since the last tag.)
export GIT_TAG ?= dev

# Prow gives this the reference it's called on.
# The test-infra config job only allows our cloudbuild to
# be called on `master` and semver tags, so this will be
# set to one of those things.
export BASE_REF ?= master

# The commit hash of the current checkout
# Used to pass a binary version for master,
# overridden to semver for tagged versions.
export COMMIT=$(shell git rev-parse --short HEAD)

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

.PHONY: build
build:
docker build --build-arg COMMIT=$(COMMIT) --build-arg TAG=$(TAG) \
-t $(REGISTRY)/admission-server:$(TAG) .

.PHONY: release-staging
release-staging: build
docker push $(REGISTRY)/admission-server:$(TAG)
docker tag $(REGISTRY)/admission-server:$(TAG) $(REGISTRY)/admission-server:latest
docker push $(REGISTRY)/admission-server:latest
release-staging:
hack/build-and-push.sh

# Generate a virtualenv install, which is useful for hacking on the
# docs since it installs mkdocs and all the right dependencies.
Expand Down
3 changes: 2 additions & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ steps:
entrypoint: make
env:
- DOCKER_CLI_EXPERIMENTAL=enabled
- TAG=$_GIT_TAG
- GIT_TAG=$_GIT_TAG
- BASE_REF=$_PULL_BASE_REF
args:
- release-staging
substitutions:
Expand Down
90 changes: 90 additions & 0 deletions hack/build-and-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash

# Copyright 2022 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file is run by cloudbuild, from cloudbuild.yaml, using `make release-staging`.

set -o errexit
set -o nounset
set -o pipefail

if [[ -z "${GIT_TAG-}" ]];
then
echo "GIT_TAG env var must be set and nonempty."
exit 1
fi

if [[ -z "${BASE_REF-}" ]];
then
echo "BASE_REF env var must be set and nonempty."
exit 1
fi

if [[ -z "${COMMIT-}" ]];
then
echo "COMMIT env var must be set and nonempty."
exit 1
fi

if [[ -z "${REGISTRY-}" ]];
then
echo "REGISTRY env var must be set and nonempty."
exit 1
fi


LATEST=false

VERSION_TAG=$GIT_TAG

BINARY_VERSION=$COMMIT

# $BASE_REF has only two things that it can be set to by cloudbuild and Prow,
# `master`, or a semver tag.
# This is controlled by k8s.io/test-infra/config/jobs/image-pushing/k8s-staging-gateway-api.yaml.
if [[ "${BASE_REF}" != "master" ]]
then
# Since we know this is built from a tag or release branch, we can set the VERSION_TAG
VERSION_TAG="${BASE_REF}"
# We want the binary version to show up correctly too.
BINARY_VERSION="${BASE_REF}"
# Use some bash magic to check if the semver does not end with -sometext, that
# would indicate a prerelease version. If this is not a prerelease, then we want to set
# the `latest` tag too.
if [[ ! "${BASE_REF}" =~ -(.+)$ ]];
then
LATEST=true
fi
fi

# First, build the image, with the version info passed in.
# Note that an image will *always* be built tagged with the GIT_TAG, so we know when it was built.
docker build --build-arg COMMIT=${BINARY_VERSION} --build-arg TAG=${VERSION_TAG} \
-t ${REGISTRY}/admission-server:${GIT_TAG} .

docker push ${REGISTRY}/admission-server:${GIT_TAG}

# Then, we add extra tags if required.
if [[ $VERSION_TAG != $GIT_TAG ]]
then
docker tag ${REGISTRY}/admission-server:${GIT_TAG} ${REGISTRY}/admission-server:${VERSION_TAG}
docker push ${REGISTRY}/admission-server:${VERSION_TAG}
fi

if [[ $LATEST == true ]]
then
docker tag ${REGISTRY}/admission-server:${GIT_TAG} ${REGISTRY}/admission-server:latest
docker push ${REGISTRY}/admission-server:latest
fi

0 comments on commit 2c62164

Please sign in to comment.