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
x-soft-wrap-text = true
[{*.,}{Dockerfile,dockerfile}{*.,}]
indent_size = 2
[Makefile]
indent_style = tab

View file

@ -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

View file

@ -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') }}

View file

@ -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

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
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" ]