Skip to content

Commit

Permalink
Fix docker publishing error in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogsilva committed Feb 19, 2024
1 parent 8f206fd commit 5ce506f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: ${{ github.event.schedule && 'Routine vulnerability scan' || 'Continuous Integration' }}

on:
push:
Expand Down
34 changes: 29 additions & 5 deletions tests/ci/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import argparse
import asyncio
import logging
import os
import shlex
import sys
from pathlib import Path

import dagger

logger = logging.getLogger(__name__)
POSTGIS_IMAGE_VERSION = "postgis/postgis:16-3.4"


def sanitize_docker_image_name(docker_image_name: str) -> str:
"""Ensure input docker_image_name is valid.
This function sanitizes the input according to the rules described in
the docker docs at:
https://docs.docker.com/engine/reference/commandline/image_tag/#extended-description
Most notably, this will ensure the path portion of the image name is
lowercase, which may sometimes not be the case for images being pushed to
the github container registry.
"""
host, rest = docker_image_name.partition("/")[::2]
path, tag = rest.partition(":")[::2]
if "_" in host:
logger.warning(
"Docker image name's host section cannot contain the '_' character.")
return f"{host}/{path.lower()}:{tag or 'latest'}"


def get_env_variables() -> dict[str, str | None]:
return {
"API_COMMAND": os.getenv("API_COMMAND", "daphne"),
Expand Down Expand Up @@ -115,7 +137,7 @@ async def run_pipeline(
*,
with_tests: bool,
with_security_scan: bool,
image_registry: str | None = None
publish_docker_image: str | None = None
):
env_variables = get_env_variables()
conf = dagger.Config(
Expand All @@ -139,8 +161,9 @@ async def run_pipeline(
await run_security_scan(built_container)
if with_tests:
await run_tests(client, built_container, env_variables)
if image_registry is not None:
await built_container.publish(image_registry)
if publish_docker_image is not None:
sanitized_name = sanitize_docker_image_name(publish_docker_image)
await built_container.publish(sanitized_name)

print("Done")

Expand All @@ -165,7 +188,7 @@ async def run_pipeline(
)
)
parser.add_argument(
"--image-registry",
"--publish-docker-image",
help=(
"Full URI to an image registry where the built container image should be "
"published, including the image tag. This assumes that logging in to the "
Expand All @@ -175,10 +198,11 @@ async def run_pipeline(
)
)
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
asyncio.run(
run_pipeline(
with_tests=args.with_tests,
with_security_scan=args.with_security_scan,
image_registry=args.image_registry,
publish_docker_image=args.publish_docker_image,
)
)

0 comments on commit 5ce506f

Please sign in to comment.