feat(workflows/docker): add workflow to build docker images (#191)

This commit is contained in:
Xuehai Pan 2025-11-11 15:57:18 +08:00 committed by GitHub
parent 466f4f57b4
commit ffc344d379
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 133 additions and 11 deletions

View file

@ -34,10 +34,15 @@ concurrency:
permissions:
contents: read
env:
FORCE_COLOR: "1"
CLICOLOR_FORCE: "1"
jobs:
build:
runs-on: ubuntu-latest
name: Build sdist and wheels
if: github.repository_owner == 'XuehaiPan'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout
@ -47,7 +52,6 @@ jobs:
fetch-depth: 0
- name: Set up Python
id: py
uses: actions/setup-python@v6
with:
python-version: "3.8 - 3.14"
@ -82,7 +86,11 @@ jobs:
- name: Test docker build
run: |
docker build --tag nvitop:latest .
docker run --rm nvitop:latest --version
docker run --rm nvitop:latest --help
docker build --tag nvitop-exporter:latest ./nvitop-exporter
docker run --rm nvitop-exporter:latest --version
docker run --rm nvitop-exporter:latest --help
- name: Set __release__
if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch'
@ -111,12 +119,13 @@ jobs:
if-no-files-found: error
publish:
runs-on: ubuntu-latest
name: Publish to PyPI
needs: [build]
if: |
github.repository_owner == 'XuehaiPan' && github.event_name != 'pull_request' &&
(github.event_name != 'workflow_dispatch' || github.event.inputs.task == 'build-and-publish') &&
(github.event_name != 'push' || startsWith(github.ref, 'refs/tags/'))
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout
@ -127,7 +136,6 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v6
if: startsWith(github.ref, 'refs/tags/')
with:
python-version: "3.8 - 3.14"
update-environment: true

114
.github/workflows/docker.yaml vendored Normal file
View file

@ -0,0 +1,114 @@
name: Build and Publish Docker Images
on:
pull_request:
paths:
- setup.py
- setup.cfg
- pyproject.toml
- MANIFEST.in
- nvitop/version.py
- nvitop-exporter/nvitop_exporter/version.py
- Dockerfile
- nvitop-exporter/Dockerfile
- .github/workflows/docker.yaml
release:
types:
- published
# Allow to trigger the workflow manually
workflow_dispatch:
inputs:
publish:
description: "Publish to GHCR"
type: boolean
default: true
required: false
tag:
description: "Version tag to publish"
type: string
required: true
concurrency:
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
packages: write
id-token: write
attestations: write
env:
IMAGE_REGISTRY: "ghcr.io"
FORCE_COLOR: "1"
CLICOLOR_FORCE: "1"
jobs:
build-and-publish:
name: Build and Publish Docker Images for ${{ matrix.name }}
if: github.repository_owner == 'XuehaiPan'
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix:
include:
- name: nvitop
context: .
- name: nvitop-exporter
context: ./nvitop-exporter
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Test docker build
run: |
docker build --tag test-image:latest ${{ matrix.context }}
docker run --rm test-image:latest --version
docker run --rm test-image:latest --help
- name: Extract version
id: tag
run: |
if [[ "${{ github.event_name }}" == 'release' ]]; then
VERSION="${GITHUB_REF#refs/tags/}"
TAG="${VERSION#v}"
elif [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then
TAG="${{ github.event.inputs.tag }}"
else
TAG="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: Login to Container Registry
if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true')
uses: docker/login-action@v3
with:
registry: ${{ env.IMAGE_REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- name: Build and push Docker image
id: build
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) || '' }}"
platforms: linux/amd64,linux/arm64
pull: true
push: ${{ github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true') }}
cache-from: type=gha,scope=${{ matrix.name }}
cache-to: type=gha,mode=max,scope=${{ matrix.name }}
- name: Generate artifact attestation
if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish == 'true')
uses: actions/attest-build-provenance@v3
with:
subject-name: ${{ steps.tag.outputs.image-name }}
subject-digest: ${{ steps.build.outputs.digest }}
push-to-registry: true

View file

@ -13,6 +13,10 @@ concurrency:
permissions:
contents: read
env:
FORCE_COLOR: "1"
CLICOLOR_FORCE: "1"
jobs:
lint:
runs-on: ubuntu-latest
@ -24,7 +28,6 @@ jobs:
fetch-depth: 0
- name: Set up Python
id: py
uses: actions/setup-python@v6
with:
python-version: "3.8 - 3.14"

View file

@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `nvidia-ml-py` 13.580.82 to support list for NVIDIA Spark/Thor by [@johnnynunez](https://github.com/johnnynunez) in [#186](https://github.com/XuehaiPan/nvitop/pull/186).
- Add bar charts for memory bandwidth and power usage in the main screen by [@XuehaiPan](https://github.com/XuehaiPan) in [#190](https://github.com/XuehaiPan/nvitop/pull/190).
- Add workflow to build and publish Docker images to GitHub Container Registry by [@XuehaiPan](https://github.com/XuehaiPan) in [#190](https://github.com/XuehaiPan/nvitop/pull/190). Issued by [@ntheanh201](https://github.com/ntheanh201).
### Changed

View file

@ -313,9 +313,7 @@ Press <kbd>h</kbd> for help or <kbd>q</kbd> to return to the terminal. See [Keyb
Build and run the Docker image with [nvidia-container-toolkit](https://github.com/NVIDIA/nvidia-container-toolkit):
```bash
git clone --depth=1 https://github.com/XuehaiPan/nvitop.git && cd nvitop # clone this repo first
docker build --tag nvitop:latest . # build the Docker image
docker run -it --rm --runtime=nvidia --gpus=all --pid=host nvitop:latest # run the Docker container
docker run -it --rm --runtime=nvidia --gpus=all --pid=host ghcr.io/xuehaipan/nvitop:latest
```
**NOTE:** Don't forget to add the `--pid=host` option when running the container.

View file

@ -25,9 +25,7 @@ services:
- prometheus
nvitop-exporter:
build:
dockerfile: Dockerfile
context: ..
image: ghcr.io/xuehaipan/nvitop-exporter:latest
command: ["--bind-address", "0.0.0.0", "--port", "5050"]
pid: host
restart: unless-stopped