Skip to content

Commit

Permalink
fix ci failures
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnider2195 committed Sep 3, 2024
1 parent b275f63 commit 1b085b2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on: # yamllint disable-line rule:truthy rule:comments
pull_request: ~

env:
APP_NAME: "nautobot-app-device-onboarding"
APP_NAME: "nautobot-device-onboarding"

jobs:
ruff-format:
Expand Down Expand Up @@ -91,6 +91,10 @@ jobs:
uses: "actions/checkout@v4"
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Constrain Nautobot version and regenerate lock file"
env:
INVOKE_NAUTOBOT_DEVICE_ONBOARDING_LOCAL: "true"
run: "poetry run invoke lock --constrain-nautobot-ver --constrain-python-ver"
- name: "Set up Docker Buildx"
id: "buildx"
uses: "docker/setup-buildx-action@v3"
Expand Down Expand Up @@ -141,6 +145,10 @@ jobs:
uses: "actions/checkout@v4"
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Constrain Nautobot version and regenerate lock file"
env:
INVOKE_NAUTOBOT_DEVICE_ONBOARDING_LOCAL: "true"
run: "poetry run invoke lock --constrain-nautobot-ver --constrain-python-ver"
- name: "Set up Docker Buildx"
id: "buildx"
uses: "docker/setup-buildx-action@v3"
Expand Down
31 changes: 10 additions & 21 deletions development/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,18 @@ RUN which poetry || curl -sSL https://install.python-poetry.org | python3 - && \
WORKDIR /source
COPY . /source

# Get container's installed Nautobot version as a forced constraint
# NAUTOBOT_VER may be a branch name and not a published release therefor we need to get the installed version
# so pip can use it to recognize local constraints.
RUN pip show nautobot | grep "^Version: " | sed -e 's/Version: /nautobot==/' > constraints.txt
# Build args must be declared in each stage
ARG PYTHON_VER

# Use Poetry to grab dev dependencies from the lock file
# Can be improved in Poetry 1.2 which allows `poetry install --only dev`
#
# We can't use the entire freeze as it takes forever to resolve with rigidly fixed non-direct dependencies,
# especially those that are only direct to Nautobot but the container included versions slightly mismatch
RUN poetry export -f requirements.txt --without-hashes --extras all --output poetry_freeze_base.txt
RUN poetry export -f requirements.txt --without-hashes --extras all --with dev --output poetry_freeze_all.txt
RUN sort poetry_freeze_base.txt poetry_freeze_all.txt | uniq -u > poetry_freeze_dev.txt

# Install all local project as editable, constrained on Nautobot version, to get any additional
# direct dependencies of the app
RUN --mount=type=cache,target="/root/.cache/pip",sharing=locked \
pip install -c constraints.txt -e .[all]
# Constrain the Nautobot version to NAUTOBOT_VER
# In CI, this should be done outside of the Dockerfile to prevent cross-compile build failures
ARG CI
RUN if [ -z "${CI+x}" ]; then \
INSTALLED_NAUTOBOT_VER=$(pip show nautobot | grep "^Version" | sed "s/Version: //"); \
poetry add --lock nautobot@${INSTALLED_NAUTOBOT_VER} --python ${PYTHON_VER}; fi

# Install any dev dependencies frozen from Poetry
# Can be improved in Poetry 1.2 which allows `poetry install --only dev`
RUN --mount=type=cache,target="/root/.cache/pip",sharing=locked \
pip install -c constraints.txt -r poetry_freeze_dev.txt
# Install the app
RUN poetry install --extras all --with dev

COPY development/nautobot_config.py ${NAUTOBOT_ROOT}/nautobot_config.py
# !!! USE CAUTION WHEN MODIFYING LINES ABOVE
44 changes: 40 additions & 4 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"""

import os
import re
from pathlib import Path
from time import sleep

from invoke.collection import Collection
from invoke.exceptions import Exit
from invoke.tasks import task as invoke_task


Expand Down Expand Up @@ -205,17 +207,51 @@ def generate_packages(context):
run_command(context, command)


def _get_docker_nautobot_version(context, nautobot_ver=None, python_ver=None):
"""Extract Nautobot version from base docker image."""
if nautobot_ver is None:
nautobot_ver = context.nautobot_device_onboarding.nautobot_ver
if python_ver is None:
python_ver = context.nautobot_device_onboarding.python_ver
dockerfile_path = os.path.join(context.nautobot_device_onboarding.compose_dir, "Dockerfile")
base_image = context.run(f"grep --max-count=1 '^FROM ' {dockerfile_path}", hide=True).stdout.strip().split(" ")[1]
base_image = base_image.replace(r"${NAUTOBOT_VER}", nautobot_ver).replace(r"${PYTHON_VER}", python_ver)
pip_nautobot_ver = context.run(f"docker run --rm --entrypoint '' {base_image} pip show nautobot", hide=True)
match_version = re.search(r"^Version: (.+)$", pip_nautobot_ver.stdout.strip(), flags=re.MULTILINE)
if match_version:
return match_version.group(1)
else:
raise Exit(f"Nautobot version not found in Docker base image {base_image}.")


@task(
help={
"check": (
"If enabled, check for outdated dependencies in the poetry.lock file, "
"instead of generating a new one. (default: disabled)"
)
),
"constrain_nautobot_ver": (
"Run 'poetry add nautobot@[version] --lock' to generate the lockfile, "
"where [version] is the version installed in the Dockerfile's base image. "
"Generally intended to be used in CI and not for local development. (default: disabled)"
),
"constrain_python_ver": (
"When using `constrain_nautobot_ver`, further constrain the nautobot version "
"to python_ver so that poetry doesn't complain about python version incompatibilities. "
"Generally intended to be used in CI and not for local development. (default: disabled)"
),
}
)
def lock(context, check=False):
"""Generate poetry.lock inside the Nautobot container."""
run_command(context, f"poetry {'check' if check else 'lock --no-update'}")
def lock(context, check=False, constrain_nautobot_ver=False, constrain_python_ver=False):
"""Generate poetry.lock file."""
if constrain_nautobot_ver:
docker_nautobot_version = _get_docker_nautobot_version(context)
command = f"poetry add --lock nautobot@{docker_nautobot_version}"
if constrain_python_ver:
command += f" --python {context.nautobot_device_onboarding.python_ver}"
else:
command = f"poetry {'check' if check else 'lock --no-update'}"
run_command(context, command)


# ------------------------------------------------------------------------------
Expand Down

0 comments on commit 1b085b2

Please sign in to comment.