From 07c4819e1bdf2ad2e460abbe2b45b3a748cd386d Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Tue, 11 Nov 2025 23:38:13 +0800 Subject: [PATCH] chore(exporter): build nvitop-exporter docker image with local source code --- .editorconfig | 3 +++ .github/workflows/build.yaml | 6 +++--- .github/workflows/docker.yaml | 24 +++++++++++++++--------- Dockerfile | 11 +++++++++-- nvitop-exporter/Dockerfile | 34 +++++++++++++++++++++++++++------- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/.editorconfig b/.editorconfig index b2f94fa..0bbae25 100644 --- a/.editorconfig +++ b/.editorconfig @@ -24,6 +24,9 @@ x-soft-wrap-text = true indent_size = 4 x-soft-wrap-text = true +[{*.,}{Dockerfile,dockerfile}{*.,}] +indent_size = 2 + [Makefile] indent_style = tab diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a8874ad..f9422f0 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -83,12 +83,12 @@ jobs: ) ) - - name: Test docker build + - name: Test Docker build run: | - docker build --tag nvitop:latest . + docker build --tag nvitop:latest --file Dockerfile . docker run --rm nvitop:latest --version docker run --rm nvitop:latest --help - docker build --tag nvitop-exporter:latest ./nvitop-exporter + docker build --tag nvitop-exporter:latest --file nvitop-exporter/Dockerfile . docker run --rm nvitop-exporter:latest --version docker run --rm nvitop-exporter:latest --help diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index a0f43bd..0dfe35c 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -54,8 +54,10 @@ jobs: include: - name: nvitop context: . + dockerfile: Dockerfile - name: nvitop-exporter - context: ./nvitop-exporter + context: . + dockerfile: nvitop-exporter/Dockerfile fail-fast: false steps: - name: Checkout @@ -64,7 +66,7 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Test docker build + - name: Test Docker build run: | docker build --tag test-image:latest ${{ matrix.context }} docker run --rm test-image:latest --version @@ -75,15 +77,18 @@ jobs: run: | if [[ "${{ github.event_name }}" == 'release' ]]; then VERSION="${GITHUB_REF#refs/tags/}" - TAG="${VERSION#v}" + TAGS="${VERSION#v},latest" elif [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then - TAG="${{ github.event.inputs.tag }}" + TAGS="${{ github.event.inputs.tag }}" else - TAG="pr-${{ github.event.number }}" + TAGS="pr-${{ github.event.number }}" fi - echo "image-name=${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ matrix.name }}" | - tr '[:upper:]' '[:lower:]' | tee -a "${GITHUB_OUTPUT}" - echo "image-tag=${TAG}" | tee -a "${GITHUB_OUTPUT}" + NAME="${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ matrix.name }}" + NAME="$(echo "${NAME}" | tr '[:upper:]' '[:lower:]')" + TAGS="$(echo "${TAGS}" | tr -d '[:space:]' | tr ',' '\n' | sort -uV | sed "s|^|${NAME}:|" | tr '\n' ',')" + TAGS="${TAGS%,}" + echo "image-name=${NAME}" | tee -a "${GITHUB_OUTPUT}" + echo "image-tags=${TAGS}" | tee -a "${GITHUB_OUTPUT}" - name: Login to Container Registry if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') @@ -98,7 +103,8 @@ jobs: uses: docker/build-push-action@v6 with: context: ${{ matrix.context }} - tags: "${{ steps.tag.outputs.image-name }}:${{ steps.tag.outputs.image-tag }}${{ github.event_name == 'release' && format(',{0}:latest', steps.tag.outputs.image-name) || '' }}" + file: ${{ matrix.dockerfile }} + tags: ${{ steps.tag.outputs.image-tags }} platforms: linux/amd64,linux/arm64 pull: true push: ${{ github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') }} diff --git a/Dockerfile b/Dockerfile index e181068..80f5d9b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,10 @@ +# Dockerfile for nvitop +# +# Build the image with: +# +# docker build --tag nvitop:latest . +# +# ============================================================================== FROM ubuntu:latest RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \ @@ -19,14 +26,14 @@ RUN python3 -m venv /venv && \ . /venv/bin/activate && \ python3 -m pip install --upgrade pip setuptools && \ rm -rf /root/.cache && \ - echo && echo && echo "source /venv/bin/activate" >> /root/.bashrc + ( echo && echo && echo "source /venv/bin/activate" ) >> /root/.bashrc ENV SHELL=/bin/bash # Install nvitop COPY . /nvitop WORKDIR /nvitop RUN . /venv/bin/activate && \ - python3 -m pip install . && \ + python3 -m pip install /nvitop && \ rm -rf /root/.cache # Entrypoint diff --git a/nvitop-exporter/Dockerfile b/nvitop-exporter/Dockerfile index dea7159..36a1e56 100644 --- a/nvitop-exporter/Dockerfile +++ b/nvitop-exporter/Dockerfile @@ -1,3 +1,10 @@ +# Dockerfile for nvitop-exporter +# +# Build the image in the repository root with: +# +# docker build --tag nvitop-exporter:latest --file nvitop-exporter/Dockerfile . +# +# ============================================================================== FROM ubuntu:latest RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \ @@ -5,17 +12,30 @@ RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \ ENV DEBIAN_FRONTEND=noninteractive -# Setup locale +# Install Python 3 RUN apt-get update && \ - apt-get install --quiet --yes --no-install-recommends python3 locales && \ + apt-get install --quiet --yes --no-install-recommends python3-dev python3-venv locales && \ rm -rf /var/lib/apt/lists/* + +# Setup locale ENV LC_ALL=C.UTF-8 RUN update-locale LC_ALL="C.UTF-8" -# Install uv -COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ +# Setup environment +RUN python3 -m venv /venv && \ + . /venv/bin/activate && \ + python3 -m pip install --upgrade pip setuptools && \ + rm -rf /root/.cache && \ + ( echo && echo && echo "source /venv/bin/activate" ) >> /root/.bashrc +ENV SHELL=/bin/bash -# Install nvitop-exporter -RUN /bin/uv tool install nvitop-exporter +# Install nvitop +COPY .. /nvitop +WORKDIR /nvitop +RUN . /venv/bin/activate && \ + python3 -m pip install /nvitop && \ + python3 -m pip install /nvitop/nvitop-exporter && \ + rm -rf /root/.cache -ENTRYPOINT [ "/bin/uvx", "--quiet", "--offline", "nvitop-exporter" ] +# Entrypoint +ENTRYPOINT [ "/venv/bin/python3", "-m", "nvitop_exporter" ]