Skip to content

Commit 6b24ca9

Browse files
authored
ci: fix tag definitions for image publishing (#116)
The push-images entrypoint was not correctly tagging or pushing the expected image tags. This change adds new functionality to the push-images entrypoint that allows specifying additional tags to apply to the published images. This is used by the cloudbuild config to add the desired image tags. Also adjusts the format of the default tag to provide a bit more context on the build artifacts (date + git describe).
1 parent a72d5c4 commit 6b24ca9

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

cloudbuild.yaml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ steps:
99
# Assuming the script accepts project and tag arguments.
1010
# Using standard Cloud Build substitutions.
1111
- "--image-prefix=${_IMAGE_PREFIX}/"
12-
13-
# The `images` section lists the container images built by this pipeline.
14-
# Cloud Build will use this to push the image to the registry.
15-
# This should match the image name that is built by the push-images script.
16-
images:
17-
- "${_IMAGE_PREFIX}/agent-sandbox-controller:$_GIT_TAG-$_CONFIG"
18-
- "${_IMAGE_PREFIX}/agent-sandbox-controller:latest-$_CONFIG"
12+
- "--extra-image-tag=${_GIT_TAG}-${_CONFIG}"
13+
- "--extra-image-tag=latest-${_CONFIG}"
1914

2015
options:
2116
enableStructuredLogging: true

dev/tools/push-images

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def create_buildx_builder_if_not_exists():
3939
subprocess.run(["docker", "buildx", "use", builder_name], check=True)
4040

4141

42-
def build_and_push_image_with_docker_buildx(args, image_name, srcdir, dockerfile_path):
42+
def build_and_push_image_with_docker_buildx(args, service_name, srcdir, dockerfile_path):
4343
platforms = "linux/amd64,linux/arm64"
4444
build_cmd = [
4545
"docker",
@@ -50,9 +50,12 @@ def build_and_push_image_with_docker_buildx(args, image_name, srcdir, dockerfile
5050
]
5151
if args.docker_build_output_type == "registry":
5252
build_cmd.append("--platform=" + platforms)
53+
image_name = utils.get_full_image_name(args, service_name)
54+
build_cmd.extend(["-t", image_name])
55+
for tag in args.extra_image_tags: # additional image tags
56+
extra_tag = utils.get_full_image_name(args, service_name, tag=tag)
57+
build_cmd.extend(["-t", extra_tag])
5358
build_cmd.extend([
54-
"-t",
55-
image_name,
5659
"-f",
5760
dockerfile_path,
5861
".",
@@ -61,7 +64,8 @@ def build_and_push_image_with_docker_buildx(args, image_name, srcdir, dockerfile
6164
print(f"pushed image {image_name}")
6265

6366

64-
def load_kind_image(cluster_name, image_name, srcdir):
67+
def load_kind_image(cluster_name, service_name, srcdir):
68+
image_name = utils.get_full_image_name(args, service_name)
6569
build_cmd = [
6670
"kind",
6771
"load",
@@ -74,10 +78,10 @@ def load_kind_image(cluster_name, image_name, srcdir):
7478
print(f"loaded image {image_name} into kind cluster {args.kind_cluster_name}")
7579

7680

77-
def build_and_push_image(args, image_name, srcdir, dockerfile_path):
78-
build_and_push_image_with_docker_buildx(args, image_name, srcdir, dockerfile_path)
81+
def build_and_push_image(args, service_name, srcdir, dockerfile_path):
82+
build_and_push_image_with_docker_buildx(args, service_name, srcdir, dockerfile_path)
7983
if args.kind_cluster_name:
80-
load_kind_image(args.kind_cluster_name, image_name, srcdir)
84+
load_kind_image(args.kind_cluster_name, service_name, srcdir)
8185

8286

8387
def push_image_for_go_mod(args, mod_dir):
@@ -95,18 +99,17 @@ def push_image_for_go_mod(args, mod_dir):
9599
dockerfile_path = os.path.join(root, filename)
96100
service_name = os.path.basename(root)
97101

98-
image_name = utils.get_full_image_name(args, service_name)
99-
100102
print(f"create Docker buildx builder for {service_name}")
101103
create_buildx_builder_if_not_exists()
102-
print(f"building image for {service_name} with tag {image_name}")
103-
build_and_push_image(args, image_name, mod_dir, dockerfile_path)
104+
print(f"building image for {service_name}")
105+
build_and_push_image(args, service_name, mod_dir, dockerfile_path)
104106

105107

106108
def main(args):
107109
"""Builds and pushes docker images."""
108110
golang.for_each_module(lambda mod_dir: push_image_for_go_mod(args, mod_dir))
109111

112+
110113
if __name__ == "__main__":
111114
# parse arguments and call
112115
parser = argparse.ArgumentParser(description="Build and push Docker images")
@@ -115,6 +118,12 @@ if __name__ == "__main__":
115118
help="Prefix for the image name. requires slash at the end if a path",
116119
type=str,
117120
default=os.getenv("IMAGE_PREFIX"))
121+
parser.add_argument("--extra-image-tag",
122+
dest="extra_image_tags",
123+
help="Additional tags to use when publishing images.",
124+
type=str,
125+
action="append",
126+
default=[])
118127
parser.add_argument("--kind-cluster-name",
119128
dest="kind_cluster_name",
120129
help="Name of the kind cluster. If set, images will be loaded into the cluster",

dev/tools/shared/utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from datetime import datetime
1617
import os
1718
import subprocess
1819

1920

20-
def get_git_commit_short():
21-
"""Gets the short git commit hash for HEAD."""
22-
return subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], text=True).strip()
21+
def git_describe():
22+
"""Gets the git describe output for HEAD."""
23+
return subprocess.check_output(
24+
["git", "describe", "--always", "--dirty"], text=True).strip()
2325

2426

2527
def get_image_tag():
26-
"""Gets the image tag based on the git commit."""
27-
return f"git-{get_git_commit_short()}"
28+
"""Gets the image tag based on the date and git commit."""
29+
day = datetime.today().strftime("%Y%m%d")
30+
return f"v{day}-{git_describe()}"
2831

2932

3033
def get_image_prefix(args):
@@ -34,10 +37,11 @@ def get_image_prefix(args):
3437
raise Exception(f"--image-prefix arg or IMAGE_PREFIX environment variable must be set")
3538

3639

37-
def get_full_image_name(args, image_id):
40+
def get_full_image_name(args, image_id, tag=None):
3841
"""Constructs the full GCR image name for an image."""
3942
image_prefix = get_image_prefix(args)
40-
tag = get_image_tag()
43+
if not tag:
44+
tag = get_image_tag()
4145
return f"{image_prefix}{image_id}:{tag}"
4246

4347

0 commit comments

Comments
 (0)