Skip to content

Commit cc43ff5

Browse files
authored
chore(ci): publish to github container registry (#6)
1 parent 9e9d703 commit cc43ff5

File tree

4 files changed

+77
-48
lines changed

4 files changed

+77
-48
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Publish Docker image
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
push_to_registry:
8+
name: Push Docker image to GitHub Container Registry
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
packages: write
13+
14+
steps:
15+
- name: Check out the repo
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Docker Buildx
19+
uses: docker/setup-buildx-action@v3
20+
21+
- name: Log in to GitHub Container Registry
22+
uses: docker/login-action@v3
23+
with:
24+
registry: ghcr.io
25+
username: ${{ github.actor }}
26+
password: ${{ secrets.GITHUB_TOKEN }}
27+
28+
- name: Extract metadata (tags, labels) for Docker
29+
id: meta
30+
uses: docker/metadata-action@v5
31+
with:
32+
images: ghcr.io/${{ github.repository }}
33+
tags: |
34+
type=semver,pattern={{version}}
35+
type=semver,pattern={{major}}.{{minor}}
36+
type=sha,format=short
37+
latest
38+
39+
- name: Build and push Docker image
40+
uses: docker/build-push-action@v5
41+
with:
42+
context: .
43+
push: true
44+
tags: ${{ steps.meta.outputs.tags }}
45+
labels: ${{ steps.meta.outputs.labels }}
46+
cache-from: type=gha
47+
cache-to: type=gha,mode=max

.github/workflows/publish.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ jobs:
1212
needs: build
1313
uses: ./.github/workflows/test.yml
1414
with:
15-
python-version: '3.9'
15+
python-version: '3.10'
1616
upload-coverage: true
1717

18-
publish:
18+
publish-docker:
19+
needs: [build, test]
20+
uses: ./.github/workflows/publish-docker.yml
21+
22+
publish-pypi:
1923
needs: [build, test]
2024
runs-on: ubuntu-latest
2125
steps:

Dockerfile

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,36 @@
1-
# Base image with Python
2-
FROM python:3.10-slim AS base
1+
# Build stage
2+
FROM python:3.10-slim AS builder
33

4-
# Set working directory
54
WORKDIR /app
65

7-
# Install system dependencies
8-
RUN apt-get update && apt-get install -y \
9-
build-essential \
10-
curl \
11-
&& rm -rf /var/lib/apt/lists/*
6+
# Install a specific version of Poetry that supports --no-dev
7+
RUN pip install poetry==2.1.1
128

13-
# Install Poetry
14-
RUN curl -sSL https://install.python-poetry.org | python3 - && \
15-
ln -s /root/.local/bin/poetry /usr/local/bin/
9+
# Copy project files
10+
COPY pyproject.toml poetry.lock* README.md ./
11+
COPY babeltron ./babeltron
1612

17-
# Copy Poetry configuration files
18-
COPY README.md pyproject.toml poetry.lock* ./
13+
# Configure poetry to not use a virtual environment
14+
RUN poetry config virtualenvs.create false \
15+
&& poetry install --without dev --no-interaction --no-ansi
1916

20-
# Copy application code
21-
COPY babeltron/ ./babeltron/
17+
FROM python:3.10-slim
2218

23-
# Configure Poetry to not use virtualenvs in Docker
24-
RUN poetry config virtualenvs.create false
19+
WORKDIR /app
2520

26-
# Install dependencies
27-
RUN poetry install --without dev --no-interaction --no-ansi
21+
# Copy Python dependencies from builder stage
22+
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
23+
COPY --from=builder /usr/local/bin /usr/local/bin
24+
25+
# Copy application code
26+
COPY --from=builder /app/babeltron ./babeltron
2827

29-
# Set environment variables
30-
ENV MODEL_PATH=/models
3128
ENV PYTHONPATH=/app
32-
ENV WORKERS=1
33-
ENV HOST=0.0.0.0
34-
ENV PORT=8000
35-
ENV MODEL_COMPRESSION_ENABLED=true
29+
ENV MODEL_PATH=/models
3630

37-
# Expose the port the app runs on
38-
EXPOSE 8000
31+
# Create a non-root user
32+
RUN useradd -m appuser
33+
USER appuser
3934

40-
# Create a script to start the application with the specified number of workers
41-
RUN echo '#!/bin/bash\n\
42-
echo "Starting with $WORKERS workers"\n\
43-
echo "Model compression: $MODEL_COMPRESSION_ENABLED"\n\
44-
if [ "$WORKERS" -eq "1" ]; then\n\
45-
# Single worker mode uses uvicorn directly\n\
46-
exec uvicorn babeltron.app.main:app --host $HOST --port $PORT\n\
47-
else\n\
48-
# Multi-worker mode uses gunicorn with uvicorn workers\n\
49-
exec gunicorn babeltron.app.main:app \\\n\
50-
--workers $WORKERS \\\n\
51-
--worker-class uvicorn.workers.UvicornWorker \\\n\
52-
--bind $HOST:$PORT\n\
53-
fi' > /app/start.sh && chmod +x /app/start.sh
54-
55-
# Command to run the application
56-
CMD ["/app/start.sh"]
35+
EXPOSE 8000
36+
CMD ["uvicorn", "babeltron.app.main:app", "--host", "0.0.0.0", "--port", "8000"]

babeltron/app/routers/translate.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ class TranslationResponse(BaseModel):
8080
)
8181
@track_dynamic_translation_metrics()
8282
async def translate(request: TranslationRequest):
83-
# Get current span from context
8483
current_span = trace.get_current_span()
85-
# Add request attributes to the current span
8684
current_span.set_attribute("src_lang", request.src_lang)
8785
current_span.set_attribute("tgt_lang", request.tgt_lang)
8886
current_span.set_attribute("text_length", len(request.text))

0 commit comments

Comments
 (0)