[Phase 2] Package Python code as container image for Kestra #25

Open
opened 2026-01-10 13:17:01 +00:00 by Damien · 0 comments
Owner

Description

Packager le code Python fabric-orchestrator dans une image container pour être utilisé par les tâches Kestra io.kestra.plugin.scripts.python.Script.

Pourquoi un container ?

Kestra exécute les scripts Python dans des containers isolés. Nous devons donc :

  1. Packager notre code Python avec toutes ses dépendances
  2. Publier l'image sur un registry (Gitea Container Registry ou GHCR)
  3. Référencer cette image dans les workflows Kestra

Dockerfile

# Dockerfile
FROM python:3.12-slim

WORKDIR /app

# Install system dependencies for pygnmi
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    libffi-dev \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY pyproject.toml .
RUN pip install --no-cache-dir .

# Copy source code
COPY src/ ./src/

# Install kestra package for outputs
RUN pip install --no-cache-dir kestra

# Set Python path
ENV PYTHONPATH=/app

# Default command (overridden by Kestra)
CMD ["python", "-c", "print('fabric-orchestrator ready')"]

Gitea Actions Workflow

# .gitea/workflows/build-container.yml
name: Build Container Image

on:
  push:
    branches: [main, feat/kestra-orchestration]
    paths:
      - 'src/**'
      - 'pyproject.toml'
      - 'Dockerfile'
  release:
    types: [published]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: gitea.arnodo.fr
          username: ${{ github.actor }}
          password: ${{ secrets.GITEA_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: gitea.arnodo.fr/damien/fabric-orchestrator
          tags: |
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=sha,prefix=

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

Usage dans Kestra

# Dans les workflows Kestra
tasks:
  - id: get_intent
    type: io.kestra.plugin.scripts.python.Script
    containerImage: gitea.arnodo.fr/damien/fabric-orchestrator:main
    script: |
      from kestra import Kestra
      from src.netbox import FabricNetBoxClient
      
      client = FabricNetBoxClient(
          url="{{ secret('NETBOX_URL') }}",
          token="{{ secret('NETBOX_TOKEN') }}"
      )
      intent = client.get_fabric_intent()
      Kestra.outputs({"intent": intent})

Tasks

  • Créer le Dockerfile optimisé
  • Créer .dockerignore pour exclure les fichiers inutiles
  • Créer le workflow Gitea Actions .gitea/workflows/build-container.yml
  • Configurer le Gitea Container Registry
  • Tester le build local avec docker build -t fabric-orchestrator .
  • Tester l'exécution dans Kestra
  • Documenter le processus de build dans le README

.dockerignore

.git
.gitignore
*.md
docs/
tests/
.env*
*.pyc
__pycache__
.pytest_cache
.mypy_cache
kestra/

Output

  • Dockerfile
  • .dockerignore
  • .gitea/workflows/build-container.yml

Dependencies

  • #24 (Kestra infrastructure setup)
## Description Packager le code Python fabric-orchestrator dans une image container pour être utilisé par les tâches Kestra `io.kestra.plugin.scripts.python.Script`. ## Pourquoi un container ? Kestra exécute les scripts Python dans des containers isolés. Nous devons donc : 1. Packager notre code Python avec toutes ses dépendances 2. Publier l'image sur un registry (Gitea Container Registry ou GHCR) 3. Référencer cette image dans les workflows Kestra ## Dockerfile ```dockerfile # Dockerfile FROM python:3.12-slim WORKDIR /app # Install system dependencies for pygnmi RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ libffi-dev \ && rm -rf /var/lib/apt/lists/* # Install Python dependencies COPY pyproject.toml . RUN pip install --no-cache-dir . # Copy source code COPY src/ ./src/ # Install kestra package for outputs RUN pip install --no-cache-dir kestra # Set Python path ENV PYTHONPATH=/app # Default command (overridden by Kestra) CMD ["python", "-c", "print('fabric-orchestrator ready')"] ``` ## Gitea Actions Workflow ```yaml # .gitea/workflows/build-container.yml name: Build Container Image on: push: branches: [main, feat/kestra-orchestration] paths: - 'src/**' - 'pyproject.toml' - 'Dockerfile' release: types: [published] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to Gitea Container Registry uses: docker/login-action@v3 with: registry: gitea.arnodo.fr username: ${{ github.actor }} password: ${{ secrets.GITEA_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: gitea.arnodo.fr/damien/fabric-orchestrator tags: | type=ref,event=branch type=semver,pattern={{version}} type=sha,prefix= - name: Build and push uses: docker/build-push-action@v5 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} cache-from: type=gha cache-to: type=gha,mode=max ``` ## Usage dans Kestra ```yaml # Dans les workflows Kestra tasks: - id: get_intent type: io.kestra.plugin.scripts.python.Script containerImage: gitea.arnodo.fr/damien/fabric-orchestrator:main script: | from kestra import Kestra from src.netbox import FabricNetBoxClient client = FabricNetBoxClient( url="{{ secret('NETBOX_URL') }}", token="{{ secret('NETBOX_TOKEN') }}" ) intent = client.get_fabric_intent() Kestra.outputs({"intent": intent}) ``` ## Tasks - [ ] Créer le `Dockerfile` optimisé - [ ] Créer `.dockerignore` pour exclure les fichiers inutiles - [ ] Créer le workflow Gitea Actions `.gitea/workflows/build-container.yml` - [ ] Configurer le Gitea Container Registry - [ ] Tester le build local avec `docker build -t fabric-orchestrator .` - [ ] Tester l'exécution dans Kestra - [ ] Documenter le processus de build dans le README ## .dockerignore ``` .git .gitignore *.md docs/ tests/ .env* *.pyc __pycache__ .pytest_cache .mypy_cache kestra/ ``` ## Output - `Dockerfile` - `.dockerignore` - `.gitea/workflows/build-container.yml` ## Dependencies - #24 (Kestra infrastructure setup)
Damien added the phase-2-minimal-reconciler label 2026-01-10 13:22:37 +00:00
Sign in to join this conversation.