Skip to content

Commit

Permalink
Add image build flow (#13)
Browse files Browse the repository at this point in the history
* add docker image creation

* update image build to alpine python

* add labels
  • Loading branch information
jerdog authored Nov 26, 2024
1 parent b002d21 commit b9fa957
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Docker Build and Publish

on:
push:
branches: [ "main" ]
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
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=sha
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
25 changes: 17 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,40 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean

# Install Python dependencies
# Copy requirements first to leverage Docker cache
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy project files
COPY . .

# Install the package in development mode
RUN pip install -e .

# Create necessary directories with proper permissions
RUN mkdir -p /app/src/bluesky_notify/data /app/logs /app/instance \
&& chmod -R 777 /app/src/bluesky_notify/data \
&& chmod -R 777 /app/logs \
&& chmod 777 /app/instance

# Create a non-root user and set ownership
# Create a non-root user
RUN useradd -m -r appuser \
&& chown -R appuser:appuser /app

# Copy project files
COPY --chown=appuser:appuser . .

# Install the package
RUN pip install --no-cache-dir .

# Switch to non-root user
USER appuser

# Expose the application port
EXPOSE 5001

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:5001/health')"

# Run the application
CMD ["python", "run.py"]

# Set labels
LABEL org.opencontainers.image.source="https://github.com/jerdog/bluesky-notify"
LABEL org.opencontainers.image.description="A cross-platform notification system for tracking and receiving alerts about new Bluesky social media posts."
LABEL org.opencontainers.image.licenses="MIT"
7 changes: 6 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

# Start the application
logger.info("Starting Bluesky Notification Tracker...")
app.run(host="0.0.0.0", port=port, debug=True)
is_container = os.environ.get('DOCKER_CONTAINER', 'false').lower() == 'true'
app.run(
host="0.0.0.0",
port=port,
debug=not is_container # Disable debug mode in container
)
except Exception as e:
logger.error(f"Application failed to start: {str(e)}")

0 comments on commit b9fa957

Please sign in to comment.