Simple container image publication #762

Merged
dboreham merged 4 commits from dboreham/publish-images into main 2024-02-23 19:57:51 +00:00
3 changed files with 62 additions and 5 deletions
Showing only changes of commit 5690303181 - Show all commits

View File

@ -31,6 +31,7 @@ from stack_orchestrator.opts import opts
from stack_orchestrator.util import include_exclude_check, get_parsed_stack_config, stack_is_external, error_exit, warn_exit
from stack_orchestrator.base import get_npm_registry_url
from stack_orchestrator.build.build_types import BuildContext
from stack_orchestrator.build.publish import publish_image
# TODO: find a place for this
# epilog="Config provided either in .env or settings.ini or env vars: CERC_REPO_BASE_DIR (defaults to ~/cerc)"
@ -113,6 +114,9 @@ def process_container(build_context: BuildContext):
sys.exit(1)
else:
print("****** Container Build Error, continuing because --continue-on-error is set")
else:
if build_context.publish_images:
publish_image(default_container_tag, build_context.image_registry)
else:
print("Skipped")
@ -123,9 +127,9 @@ def process_container(build_context: BuildContext):
@click.option("--force-rebuild", is_flag=True, default=False, help="Override dependency checking -- always rebuild")
@click.option("--extra-build-args", help="Supply extra arguments to build")
@click.option("--publish-images", is_flag=True, default=False, help="Publish the built images in the specified image registry")
@click.option("--image-registry-url", help="Specify the image registry for --publish-images")
@click.option("--image-registry", help="Specify the image registry for --publish-images")
@click.pass_context
def command(ctx, include, exclude, force_rebuild, extra_build_args, publish_images, image_registry_url):
def command(ctx, include, exclude, force_rebuild, extra_build_args, publish_images, image_registry):
'''build the set of containers required for a complete stack'''
local_stack = ctx.obj.local_stack
@ -147,8 +151,8 @@ def command(ctx, include, exclude, force_rebuild, extra_build_args, publish_imag
print('Dev root directory doesn\'t exist, creating')
if publish_images:
if not image_registry_url:
error_exit("ERROR: --image-registry-url must be supplied with --publish-images")
if not image_registry:
error_exit("--image-registry must be supplied with --publish-images")
# See: https://stackoverflow.com/a/20885799/1701505
from stack_orchestrator import data
@ -177,12 +181,15 @@ def command(ctx, include, exclude, force_rebuild, extra_build_args, publish_imag
for container in containers_in_scope:
if include_exclude_check(container, include, exclude):
build_context = BuildContext(
stack,
container,
container_build_dir,
container_build_env,
dev_root_path
dev_root_path,
publish_images,
image_registry
)
process_container(build_context)
else:

View File

@ -26,3 +26,5 @@ class BuildContext:
container_build_dir: Path
container_build_env: Mapping[str,str]
dev_root_path: str
publish_images: bool
image_registry: str

View File

@ -0,0 +1,48 @@
# Copyright © 2024 Vulcanize
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>.
from datetime import datetime
from python_on_whales import DockerClient
from stack_orchestrator.opts import opts
from stack_orchestrator.util import error_exit
def _publish_tag_for_image(local_image_tag: str, remote_repo: str, version: str):
# Turns image tags of the form: foo/bar:local into remote.repo/org/bar:deploy
(image_name, image_version) = local_image_tag.split(":")
if image_version == "local":
return f"{remote_repo}/{image_name}:{version}"
else:
error_exit("Asked to publish a non-locally built image")
def publish_image(local_tag, registry):
if opts.o.verbose:
print(f"Publishing this image: {local_tag} to this registry: {registry}")
docker = DockerClient()
# Figure out the target image tag
# Eventually this version will be generated from the source repo state
# Using a timestemp is an intermediate step
version = datetime.now().strftime("%Y%m%d%H%M")
remote_tag = _publish_tag_for_image(local_tag, registry, version)
# Tag the image thus
if opts.o.debug:
print(f"Tagging {local_tag} to {remote_tag}")
docker.image.tag(local_tag, remote_tag)
# Push it to the desired registry
if opts.o.verbose:
print(f"Pushing image {remote_tag}")
docker.image.push(remote_tag)