name: Docker Build and Publish
on:
push:
branches: [main]
tags:
- 'v*'
pull_request:
branches: [main]
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Extract version for build args
id: version
env:
GIT_REF: ${{ github.ref }}
GIT_REF_NAME: ${{ github.ref_name }}
GIT_SHA: ${{ github.sha }}
run: |
# Extract version from tag (v1.2.3 -> 1.2.3) or use 'dev' for branches
if [[ "$GIT_REF" == refs/tags/v* ]]; then
VERSION="${GIT_REF_NAME#v}" # Remove 'v' prefix
else
VERSION="dev-${GIT_SHA:0:7}"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "build_date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "vcs_ref=$GIT_SHA" >> $GITHUB_OUTPUT
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ steps.version.outputs.version }}
BUILD_DATE=${{ steps.version.outputs.build_date }}
VCS_REF=${{ steps.version.outputs.vcs_ref }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Test Docker image
if: github.event_name == 'pull_request'
run: |
# Build the image
docker build -t test-image .
# Test CLI help works for all entry points
echo "=== Testing CLI entry points ==="
docker run --rm test-image python -m fal_mcp_server.server --help
docker run --rm test-image python -m fal_mcp_server.server_http --help
docker run --rm test-image python -m fal_mcp_server.server_dual --help
# Test HTTP server starts with environment variables
echo "=== Starting HTTP server container with env vars ==="
docker run -d \
--name test-container \
-e FAL_KEY=test-key \
-e FAL_MCP_TRANSPORT=http \
-e FAL_MCP_HOST=0.0.0.0 \
-e FAL_MCP_PORT=8080 \
-p 8080:8080 \
test-image
# Wait for server to start
echo "Waiting for server to start..."
sleep 5
# Check container is still running (didn't crash on startup)
echo "=== Checking container is running ==="
if ! docker ps | grep test-container; then
echo "ERROR: Container is not running!"
docker logs test-container
exit 1
fi
# Check container logs for successful startup
echo "=== Container logs ==="
docker logs test-container
# Verify server started successfully (check for Uvicorn startup message)
echo "=== Verifying successful startup ==="
if docker logs test-container 2>&1 | grep -q "Uvicorn running on"; then
echo "✓ Server started successfully"
else
echo "ERROR: Server did not start properly"
exit 1
fi
# Verify environment variables were expanded correctly (not literal ${VAR})
if docker logs test-container 2>&1 | grep -q '\${FAL_MCP'; then
echo "ERROR: Environment variables not expanded correctly"
exit 1
else
echo "✓ Environment variables expanded correctly"
fi
# Test SSE endpoint is accessible
echo "=== Testing SSE endpoint ==="
curl -sf http://localhost:8080/sse --max-time 2 -o /dev/null || echo "✓ SSE endpoint responded (timeout expected for SSE)"
# Verify no Python exceptions in logs
echo "=== Checking for errors ==="
if docker logs test-container 2>&1 | grep -i "traceback"; then
echo "ERROR: Found Python traceback in container logs"
exit 1
else
echo "✓ No Python tracebacks found"
fi
# Verify version info is embedded in image
echo "=== Checking version info ==="
docker stop test-container
VERSION_INFO=$(docker run --rm test-image printenv FAL_MCP_VERSION)
if [ -n "$VERSION_INFO" ]; then
echo "✓ Version info embedded: $VERSION_INFO"
else
echo "WARNING: Version info not found (expected in tagged builds)"
fi
# Restart container for cleanup
docker start test-container
# Cleanup
echo "=== Cleaning up ==="
docker stop test-container
docker rm test-container
echo "=== All Docker tests passed! ==="