Update get_docker_image.py to enable use without image cache container registry. (#6177)

Update get_docker_image.py to enable use without image cache container registry.
This commit is contained in:
Edward Chen 2020-12-18 19:01:02 -08:00 committed by GitHub
parent 11b0a5401e
commit cd3a5acca0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 42 deletions

View file

@ -51,8 +51,9 @@ def parse_args():
"content.")
parser.add_argument(
"--container-registry", required=True,
help="The Azure container registry name.")
"--container-registry",
help="The Azure container registry name. "
"If not provided, no container registry will be used.")
parser.add_argument(
"--repository", required=True, help="The image repository name.")
@ -125,7 +126,9 @@ def container_registry_has_image(full_image_name, docker_path):
proc = run(
docker_path, "manifest", "inspect", "--insecure", full_image_name,
env=env, check=False, quiet=True)
return proc.returncode == 0
image_found = proc.returncode == 0
log.debug("Image {} in registry".format("found" if image_found else "not found"))
return image_found
def main():
@ -134,20 +137,25 @@ def main():
log.debug("Dockerfile: {}, context: {}, docker build args: '{}'".format(
args.dockerfile, args.context, args.docker_build_args))
use_container_registry = args.container_registry is not None
if not use_container_registry:
log.info("No container registry will be used")
tag = generate_tag(args.dockerfile, args.context, args.docker_build_args)
full_image_name = "{}.azurecr.io/{}:{}".format(
args.container_registry, args.repository, tag)
full_image_name = \
"{}.azurecr.io/{}:{}".format(args.container_registry, args.repository, tag) \
if use_container_registry else \
"{}:{}".format(args.repository, tag)
log.info("Image: {}".format(full_image_name))
if container_registry_has_image(full_image_name, args.docker_path):
log.info("Image found, pulling...")
if use_container_registry and container_registry_has_image(full_image_name, args.docker_path):
log.info("Pulling image...")
run(args.docker_path, "pull", full_image_name)
else:
log.info("Image not found, building and pushing...")
log.info("Building image...")
run(args.docker_path, "build",
"--pull",
*shlex.split(args.docker_build_args),
@ -156,12 +164,14 @@ def main():
"--file", args.dockerfile,
args.context)
# avoid pushing if an identically tagged image has been pushed since the last check
# there is still a race condition, but this reduces the chance of a redundant push
if not container_registry_has_image(full_image_name, args.docker_path):
run(args.docker_path, "push", full_image_name)
else:
log.info("Image now found, skipping push")
if use_container_registry:
# avoid pushing if an identically tagged image has been pushed since the last check
# there is still a race condition, but this reduces the chance of a redundant push
if not container_registry_has_image(full_image_name, args.docker_path):
log.info("Pushing image...")
run(args.docker_path, "push", full_image_name)
else:
log.info("Image now found, skipping push")
# tag so we can refer to the image by repository name
run(args.docker_path, "tag", full_image_name, args.repository)

View file

@ -10,17 +10,29 @@ parameters:
default: ""
- name: Repository
type: string
- name: UseImageCacheContainerRegistry
type: boolean
default: true
steps:
- template: with-container-registry-steps.yml
parameters:
Steps:
- script: |
tools/ci_build/get_docker_image.py \
--dockerfile "${{ parameters.Dockerfile }}" \
--context "${{ parameters.Context }}" \
--docker-build-args "${{ parameters.DockerBuildArgs }}" \
--container-registry onnxruntimebuildcache \
--repository "${{ parameters.Repository }}"
displayName: "Get ${{ parameters.Repository }} image"
ContainerRegistry: onnxruntimebuildcache
- ${{ if eq(parameters.UseImageCacheContainerRegistry, true) }}:
- template: with-container-registry-steps.yml
parameters:
Steps:
- script: |
tools/ci_build/get_docker_image.py \
--dockerfile "${{ parameters.Dockerfile }}" \
--context "${{ parameters.Context }}" \
--docker-build-args "${{ parameters.DockerBuildArgs }}" \
--container-registry onnxruntimebuildcache \
--repository "${{ parameters.Repository }}"
displayName: "Get ${{ parameters.Repository }} image"
ContainerRegistry: onnxruntimebuildcache
- ${{ if eq(parameters.UseImageCacheContainerRegistry, false) }}:
- script: |
tools/ci_build/get_docker_image.py \
--dockerfile "${{ parameters.Dockerfile }}" \
--context "${{ parameters.Context }}" \
--docker-build-args "${{ parameters.DockerBuildArgs }}" \
--repository "${{ parameters.Repository }}"
displayName: "Get ${{ parameters.Repository }} image"

View file

@ -6,12 +6,20 @@ parameters:
- name: DisplayName
type: string
default: "Call run_dockerbuild.sh"
- name: UseImageCacheContainerRegistry
type: boolean
default: true
steps:
- template: with-container-registry-steps.yml
parameters:
Steps:
- script: |
tools/ci_build/github/linux/run_dockerbuild.sh -i onnxruntimebuildcache ${{ parameters.RunDockerBuildArgs }}
displayName: "${{ parameters.DisplayName }}"
ContainerRegistry: onnxruntimebuildcache
- ${{ if eq(parameters.UseImageCacheContainerRegistry, true) }}:
- template: with-container-registry-steps.yml
parameters:
Steps:
- script: |
tools/ci_build/github/linux/run_dockerbuild.sh -i onnxruntimebuildcache ${{ parameters.RunDockerBuildArgs }}
displayName: "${{ parameters.DisplayName }}"
ContainerRegistry: onnxruntimebuildcache
- ${{ if eq(parameters.UseImageCacheContainerRegistry, false) }}:
- script: |
tools/ci_build/github/linux/run_dockerbuild.sh ${{ parameters.RunDockerBuildArgs }}
displayName: "${{ parameters.DisplayName }}"

View file

@ -43,14 +43,10 @@ EXIT_CODE=1
PYTHON_VER=${PYTHON_VER:=3.6}
echo "bo=$BUILD_OS bd=$BUILD_DEVICE bdir=$BUILD_DIR pv=$PYTHON_VER bex=$BUILD_EXTR_PAR"
if [[ -z "${IMAGE_CACHE_CONTAINER_REGISTRY_NAME}" ]]; then
echo "Please specify an image cache container registry name (-i)."
exit 1
GET_DOCKER_IMAGE_CMD="${SOURCE_ROOT}/tools/ci_build/get_docker_image.py"
if [[ -n "${IMAGE_CACHE_CONTAINER_REGISTRY_NAME}" ]]; then
GET_DOCKER_IMAGE_CMD="${GET_DOCKER_IMAGE_CMD} --container-registry ${IMAGE_CACHE_CONTAINER_REGISTRY_NAME}"
fi
COMMON_GET_DOCKER_IMAGE_ARGS="--container-registry ${IMAGE_CACHE_CONTAINER_REGISTRY_NAME}"
GET_DOCKER_IMAGE_CMD="${SOURCE_ROOT}/tools/ci_build/get_docker_image.py ${COMMON_GET_DOCKER_IMAGE_ARGS}"
DOCKER_CMD="docker"
cd $SCRIPT_DIR/docker