Skip to content

Commit

Permalink
Make sure image run has correct options by default
Browse files Browse the repository at this point in the history
To ensure the container has access to the same network as the
docker-compose services we need to supply it. This is now done by
default. As is setting the correctly database URL when running inside
the container.

The run-image task was refactored a little to make it easily to
default/override these values. The CI action to check the container
was also abler to be simplified a little as result.
  • Loading branch information
a-musing-moose committed Jul 11, 2024
1 parent 54704a2 commit 0c4d672
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
21 changes: 11 additions & 10 deletions template/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ jobs:
- name: Build the documentation
# When running the ci locally using act, MkDocs build fails at "make html" with
# `ValueError: ZoneInfo keys may not be absolute paths, got: /UTC.`
# It seems to be a timezone related issue that arises from the interaction of the Python
# packages babel & zoneinfo.
# It seems to be a timezone related issue that arises from the interaction of the
# Python packages babel & zoneinfo.
# setting `TZ=UTC` is a work around here to bypass that error.
# NOTE: it runs fine when pushing to Github, but unfortunately not in act.
# link to issue on act repository: https://github.com/nektos/act/issues/1853
Expand Down Expand Up @@ -162,9 +162,10 @@ jobs:
invoke install
cp example.env .env
# Since there are separate jobs for running system and functional tests independently,
# and we use different ports to access the database, the following configuration sets up
# the environment variable to point to the correct database URL.
# Since there are separate jobs for running system and functional tests
# independently, and we use different ports to access the database, the following
# configuration sets up the environment variable to point to the correct database
# URL.
- name: Set DATABASE_URL for functional tests
run: echo "DATABASE_URL=postgres://dev:dev_password@localhost:5433/dev" >> $GITHUB_ENV

Expand All @@ -177,8 +178,9 @@ jobs:
runs-on: ubuntu-latest

services:
postgres:
db:
image: postgres:16

env:
POSTGRES_USER: dev
POSTGRES_PASSWORD: dev_password
Expand All @@ -189,8 +191,6 @@ jobs:
--health-timeout 5s
--health-retries 5
ports:
# Allows us to use the same env config used locally by mapping to port to
# the test runner container.
- 5432:5432

steps:
Expand All @@ -208,11 +208,12 @@ jobs:
- name: Setup environment variables
run: |
cp example.env .env
echo 'DATABASE_URL=postgres://dev:dev_password@postgres:5432/dev' >> .env
- name: Build service Docker Image
run: invoke build-image

- name: Run service Docker image
# Since we are running the app in docker we need to ensure it attaches to the same
# virtual network.
run: |
invoke run-image --options '--network {% verbatim %}${{ job.container.network }}{% endverbatim %} --env-file .env' --command check
invoke run-image --network {% verbatim %}${{ job.container.network }}{% endverbatim %} --command check
33 changes: 21 additions & 12 deletions template/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ def build_docs(ctx):

@invoke.task
def run_image(
ctx, tag: str | None = None, options: str | None = None, command: str | None = None
ctx,
tag: str | None = None,
network: str | None = None,
env_file: str | None = None,
database_url: str | None = None,
command: str | None = None,
):
"""
Run the service Docker image
Expand All @@ -250,24 +255,28 @@ def run_image(
"""
_title("Running Docker image 🐳")

if tag is None:
data = _build_data(ctx)
tag = data.tag

if options is None:
options = "--env-file .env"

args = [f"docker run --rm -p 8000:8000 {options} {PACKAGE}:{tag}"]
tag = tag or _build_data(ctx).tag
network = network or f"{PACKAGE}_default"
env_file = env_file or ".env"
database_url = database_url or "postgres://dev:dev_password@db:5432/dev"

args = [
"docker run",
"--rm",
"-it",
"-p 8000:8000",
f"--network {network}",
f"-e DATABASE_URL={database_url}",
f"--env-file {env_file}",
f"{PACKAGE}:{tag}"
]

if command:
args.append(command)

ctx.run(" ".join(args), echo=True, pty=True)





@invoke.task
def run_docs(ctx):
"""
Expand Down

0 comments on commit 0c4d672

Please sign in to comment.