chore(exporter): build nvitop-exporter docker image with local source code

This commit is contained in:
Xuehai Pan 2025-11-11 23:38:13 +08:00
parent d0a043e205
commit 07c4819e1b
5 changed files with 57 additions and 21 deletions

View file

@ -24,6 +24,9 @@ x-soft-wrap-text = true
indent_size = 4 indent_size = 4
x-soft-wrap-text = true x-soft-wrap-text = true
[{*.,}{Dockerfile,dockerfile}{*.,}]
indent_size = 2
[Makefile] [Makefile]
indent_style = tab indent_style = tab

View file

@ -83,12 +83,12 @@ jobs:
) )
) )
- name: Test docker build - name: Test Docker build
run: | run: |
docker build --tag nvitop:latest . docker build --tag nvitop:latest --file Dockerfile .
docker run --rm nvitop:latest --version docker run --rm nvitop:latest --version
docker run --rm nvitop:latest --help 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 --version
docker run --rm nvitop-exporter:latest --help docker run --rm nvitop-exporter:latest --help

View file

@ -54,8 +54,10 @@ jobs:
include: include:
- name: nvitop - name: nvitop
context: . context: .
dockerfile: Dockerfile
- name: nvitop-exporter - name: nvitop-exporter
context: ./nvitop-exporter context: .
dockerfile: nvitop-exporter/Dockerfile
fail-fast: false fail-fast: false
steps: steps:
- name: Checkout - name: Checkout
@ -64,7 +66,7 @@ jobs:
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Test docker build - name: Test Docker build
run: | run: |
docker build --tag test-image:latest ${{ matrix.context }} docker build --tag test-image:latest ${{ matrix.context }}
docker run --rm test-image:latest --version docker run --rm test-image:latest --version
@ -75,15 +77,18 @@ jobs:
run: | run: |
if [[ "${{ github.event_name }}" == 'release' ]]; then if [[ "${{ github.event_name }}" == 'release' ]]; then
VERSION="${GITHUB_REF#refs/tags/}" VERSION="${GITHUB_REF#refs/tags/}"
TAG="${VERSION#v}" TAGS="${VERSION#v},latest"
elif [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then elif [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then
TAG="${{ github.event.inputs.tag }}" TAGS="${{ github.event.inputs.tag }}"
else else
TAG="pr-${{ github.event.number }}" TAGS="pr-${{ github.event.number }}"
fi fi
echo "image-name=${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ matrix.name }}" | NAME="${{ env.IMAGE_REGISTRY }}/${{ github.repository_owner }}/${{ matrix.name }}"
tr '[:upper:]' '[:lower:]' | tee -a "${GITHUB_OUTPUT}" NAME="$(echo "${NAME}" | tr '[:upper:]' '[:lower:]')"
echo "image-tag=${TAG}" | tee -a "${GITHUB_OUTPUT}" 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 - name: Login to Container Registry
if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') 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 uses: docker/build-push-action@v6
with: with:
context: ${{ matrix.context }} 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 platforms: linux/amd64,linux/arm64
pull: true pull: true
push: ${{ github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') }} push: ${{ github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') }}

View file

@ -1,3 +1,10 @@
# Dockerfile for nvitop
#
# Build the image with:
#
# docker build --tag nvitop:latest .
#
# ==============================================================================
FROM ubuntu:latest FROM ubuntu:latest
RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \ RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \
@ -19,14 +26,14 @@ RUN python3 -m venv /venv && \
. /venv/bin/activate && \ . /venv/bin/activate && \
python3 -m pip install --upgrade pip setuptools && \ python3 -m pip install --upgrade pip setuptools && \
rm -rf /root/.cache && \ 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 ENV SHELL=/bin/bash
# Install nvitop # Install nvitop
COPY . /nvitop COPY . /nvitop
WORKDIR /nvitop WORKDIR /nvitop
RUN . /venv/bin/activate && \ RUN . /venv/bin/activate && \
python3 -m pip install . && \ python3 -m pip install /nvitop && \
rm -rf /root/.cache rm -rf /root/.cache
# Entrypoint # Entrypoint

View file

@ -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 FROM ubuntu:latest
RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \ RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \
@ -5,17 +12,30 @@ RUN . /etc/os-release && [ "${NAME}" = "Ubuntu" ] || \
ENV DEBIAN_FRONTEND=noninteractive ENV DEBIAN_FRONTEND=noninteractive
# Setup locale # Install Python 3
RUN apt-get update && \ 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/* rm -rf /var/lib/apt/lists/*
# Setup locale
ENV LC_ALL=C.UTF-8 ENV LC_ALL=C.UTF-8
RUN update-locale LC_ALL="C.UTF-8" RUN update-locale LC_ALL="C.UTF-8"
# Install uv # Setup environment
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ 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 # Install nvitop
RUN /bin/uv tool install nvitop-exporter 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" ]