diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 7527deb..23568d9 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -26,6 +26,7 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 # Fetch all history for proper versioning + fetch-tags: true # Explicitly fetch all tags - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: @@ -59,6 +60,7 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 # Fetch all history for proper versioning + fetch-tags: true # Explicitly fetch all tags - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: @@ -84,7 +86,8 @@ jobs: - name: Checkout uses: actions/checkout@v4 with: - fetch-depth: 0 + fetch-depth: 0 # Fetch all history for proper versioning + fetch-tags: true # Explicitly fetch all tags - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -153,6 +156,7 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 # Fetch all history for proper versioning + fetch-tags: true # Explicitly fetch all tags # PyPI deployment (only for Python 3.x representative) - name: Set up Python @@ -213,6 +217,27 @@ jobs: flavor: | latest=${{ matrix.python-version == '3.11' }} + - name: Get Version for Docker Build + id: get_version + run: | + # Ensure we have tags + git fetch --tags --force + + # For tagged builds, use the exact tag + if [[ "$GITHUB_REF" == refs/tags/* ]]; then + TAG=${GITHUB_REF#refs/tags/} + # Keep the v prefix for git tags + VERSION="$TAG" + echo "Using tag version: $VERSION" + else + # Use git version with v prefix + VERSION=$(git describe --tags --always 2>/dev/null || echo "v0.1.0") + echo "Using git version: $VERSION" + fi + + # Output for GitHub Actions + echo "VERSION=$VERSION" >> $GITHUB_OUTPUT + - name: Build and Push Docker Image uses: docker/build-push-action@v5 with: @@ -222,5 +247,6 @@ jobs: labels: ${{ steps.meta.outputs.labels }} build-args: | PYTHON_VERSION=${{ matrix.python-version }} + VERSION=${{ steps.get_version.outputs.VERSION }} cache-from: type=gha cache-to: type=gha,mode=max \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d0dd1a0..486760b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,25 @@ ARG PYTHON_VERSION=3.11 FROM python:${PYTHON_VERSION}-slim +# Accept version as build arg +ARG VERSION="0.1.0" + WORKDIR /app -# Install git to properly detect version during build +# Install git for versioning RUN apt-get update && apt-get install -y git && apt-get clean && rm -rf /var/lib/apt/lists/* # Copy requirements and install dependencies first COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Copy everything needed for versioning and installation +# Copy source code for installation COPY . . -# Install the package with proper versioning support +# Use setuptools_scm with the version passed from the build +ENV SETUPTOOLS_SCM_PRETEND_VERSION=$VERSION + +# Install the package RUN pip install --no-cache-dir . # Create a wrapper script to allow more flexibility