Files
docling-serve/.github/workflows/job-image.yml
2025-11-21 15:42:42 +01:00

232 lines
7.8 KiB
YAML

name: Build docling-serve container image
on:
workflow_call:
inputs:
build_args:
type: string
description: "Extra build arguments for the build."
default: ""
ghcr_image_name:
type: string
description: "Name of the image for GHCR."
quay_image_name:
type: string
description: "Name of the image Quay."
platforms:
type: string
description: "Platform argument for building images."
default: linux/amd64, linux/arm64
publish:
type: boolean
description: "If true, the images will be published."
default: false
environment:
type: string
description: "GH Action environment"
default: ""
env:
GHCR_REGISTRY: ghcr.io
QUAY_REGISTRY: quay.io
jobs:
image:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
attestations: write
id-token: write
environment: ${{ inputs.environment }}
steps:
- name: Free up space in github runner
# Free space as indicated here : https://github.com/actions/runner-images/issues/2840#issuecomment-790492173
run: |
df -h
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
sudo rm -rf /usr/share/dotnet /opt/ghc /usr/local/lib/android /usr/local/share/powershell /usr/share/swift /usr/local/.ghcup
# shellcheck disable=SC2046
sudo docker rmi "$(docker image ls -aq)" >/dev/null 2>&1 || true
df -h
- name: Check out the repo
uses: actions/checkout@v5
- name: Log in to the GHCR container image registry
if: ${{ inputs.publish }}
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to the Quay container image registry
if: ${{ inputs.publish }}
uses: docker/login-action@v3
with:
registry: ${{ env.QUAY_REGISTRY }}
username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Extract metadata (tags, labels) for docling-serve ghcr image
id: ghcr_meta
uses: docker/metadata-action@v5
with:
images: ${{ env.GHCR_REGISTRY }}/${{ inputs.ghcr_image_name }}
# # Local test
# - name: Set metadata outputs for local testing ## comment out Free up space, Log in to cr, Cache Docker, Extract metadata, and quay blocks and run act
# id: ghcr_meta
# run: |
# echo "tags=ghcr.io/docling-project/docling-serve:pr-123" >> $GITHUB_OUTPUT
# echo "labels=org.opencontainers.image.source=https://github.com/docling-project/docling-serve" >> $GITHUB_OUTPUT
- name: Build and push image to ghcr.io
id: ghcr_push
uses: docker/build-push-action@v6
with:
context: .
push: ${{ inputs.publish }} # set 'false' for local test
tags: ${{ steps.ghcr_meta.outputs.tags }}
labels: ${{ steps.ghcr_meta.outputs.labels }}
platforms: ${{ inputs.platforms }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: Containerfile
build-args: ${{ inputs.build_args }}
pull: true
##
## This stage runs after the build, so it leverages all build cache
##
- name: Export built image for testing
id: ghcr_export_built_image
uses: docker/build-push-action@v6
with:
context: .
push: false
load: true
tags: ${{ env.GHCR_REGISTRY }}/${{ inputs.ghcr_image_name }}:${{ github.sha }}-test
labels: |
org.opencontainers.image.title=docling-serve
org.opencontainers.image.test=true
platforms: linux/amd64 # when 'load' is true, we can't use a list ${{ inputs.platforms }}
cache-from: type=gha
cache-to: type=gha,mode=max
file: Containerfile
build-args: ${{ inputs.build_args }}
- name: Test image
if: steps.ghcr_export_built_image.outcome == 'success'
run: |
set -e
IMAGE_TAG="${{ env.GHCR_REGISTRY }}/${{ inputs.ghcr_image_name }}:${{ github.sha }}-test"
echo "Testing local image: $IMAGE_TAG"
# Remove existing container if any
docker rm -f docling-serve-test-container 2>/dev/null || true
echo "Starting container..."
docker run -d -p 5001:5001 --name docling-serve-test-container "$IMAGE_TAG"
echo "Waiting 15s for container to boot..."
sleep 15
# Health check
echo "Checking service health..."
for i in {1..20}; do
HEALTH_RESPONSE=$(curl -s http://localhost:5001/health || true)
echo "Health check response [$i]: $HEALTH_RESPONSE"
if echo "$HEALTH_RESPONSE" | grep -q '"status":"ok"'; then
echo "Service is healthy!"
# Install pytest and dependencies
echo "Installing pytest and dependencies..."
pip install uv
uv venv --allow-existing
source .venv/bin/activate
uv sync --only-dev
# Run pytest tests
echo "Running tests..."
# Run pytest and check result directly
if ! pytest -sv -k "test_convert_url" tests/test_1-url-async.py \
--disable-warnings; then
echo "Tests failed!"
docker logs docling-serve-test-container
docker rm -f docling-serve-test-container
exit 1
fi
echo "Tests passed successfully!"
break
else
echo "Waiting for service... [$i/20]"
sleep 3
fi
done
# Final health check if service didn't pass earlier
if ! echo "$HEALTH_RESPONSE" | grep -q '"status":"ok"'; then
echo "Service did not become healthy in time."
docker logs docling-serve-test-container
docker rm -f docling-serve-test-container
exit 1
fi
# Cleanup
echo "Cleaning up test container..."
docker rm -f docling-serve-test-container
echo "Cleaning up test image..."
docker rmi "$IMAGE_TAG"
- name: Generate artifact attestation
if: ${{ inputs.publish }}
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.GHCR_REGISTRY }}/${{ inputs.ghcr_image_name }}
subject-digest: ${{ steps.ghcr_push.outputs.digest }}
push-to-registry: true
- name: Extract metadata (tags, labels) for docling-serve quay image
if: ${{ inputs.publish }}
id: quay_meta
uses: docker/metadata-action@v5
with:
images: ${{ env.QUAY_REGISTRY }}/${{ inputs.quay_image_name }}
- name: Build and push image to quay.io
if: ${{ inputs.publish }}
# id: push-serve-cpu-quay
uses: docker/build-push-action@v6
with:
context: .
push: ${{ inputs.publish }}
tags: ${{ steps.quay_meta.outputs.tags }}
labels: ${{ steps.quay_meta.outputs.labels }}
platforms: ${{ inputs.platforms}}
cache-from: type=gha
cache-to: type=gha,mode=max
file: Containerfile
build-args: ${{ inputs.build_args }}
pull: true
- name: Remove local Docker images
run: |
docker image prune -af