Added docker #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Python and Docker CI/CD | |
| on: | |
| push: | |
| branches: | |
| - main | |
| tags: | |
| - '*' | |
| pull_request: | |
| branches: | |
| - main | |
| permissions: | |
| contents: read | |
| packages: write | |
| jobs: | |
| lint: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| python-version: ["3.9", "3.10", "3.11", "3.12"] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install ruff flake8 pylint isort setuptools | |
| pip install -r requirements.txt | |
| - name: Analysing the code with pylint | |
| run: | | |
| pylint $(git ls-files '*.py') | |
| - name: Check code style with flake8 | |
| run: | | |
| flake8 . | |
| - name: Check import order with isort | |
| run: | | |
| isort --check-only --diff . | |
| - name: Linting with Ruff | |
| run: | | |
| ruff check $(git ls-files '*.py') | |
| test: | |
| needs: lint | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| python-version: ["3.9", "3.10", "3.11", "3.12"] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python ${{ matrix.python-version }} | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install pytest | |
| pip install -r requirements.txt | |
| - name: Run tests | |
| run: | | |
| python -m pytest | |
| docker: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| python-version: ["3.9", "3.10", "3.11", "3.12"] | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build Docker Image - Python ${{ matrix.python-version }} | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| load: true | |
| tags: gpt-po-translator:py${{ matrix.python-version }} | |
| build-args: | | |
| PYTHON_VERSION=${{ matrix.python-version }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Test Docker Image - Help Text | |
| run: | | |
| # Run Docker image to verify it works | |
| docker run gpt-po-translator:py${{ matrix.python-version }} --version | |
| # Check help output (should exit with 0) | |
| docker run gpt-po-translator:py${{ matrix.python-version }} --help | |
| echo "✅ Basic Docker image tests passed for Python ${{ matrix.python-version }}" | |
| - name: Test Docker Image - CLI Options and Tests | |
| run: | | |
| # Test with --list-models (doesn't require API key) | |
| docker run -e OPENAI_API_KEY="dummy-key" gpt-po-translator:py${{ matrix.python-version }} --provider openai --list-models | |
| # Run the Python tests inside the container | |
| docker run gpt-po-translator:py${{ matrix.python-version }} python -m pytest | |
| echo "✅ CLI option test and pytest passed for Python ${{ matrix.python-version }}" | |
| - name: Test Docker Image with Sample PO file | |
| run: | | |
| # Create test directory with sample PO file | |
| mkdir -p test-po-files | |
| cp .github/workflows/test-sample.po test-po-files/ | |
| # Test Docker volume mounting and file access (no actual translation) | |
| docker run \ | |
| -v $(pwd)/test-po-files:/test \ | |
| -e OPENAI_API_KEY="dummy-key" \ | |
| gpt-po-translator:py${{ matrix.python-version }} \ | |
| --folder /test --lang de --provider openai --model gpt-3.5-turbo-instruct --list-models | |
| # Check if the tool can access the mounted files | |
| echo "Verifying test file is accessible..." | |
| docker run \ | |
| -v $(pwd)/test-po-files:/test \ | |
| gpt-po-translator:py${{ matrix.python-version }} \ | |
| --folder /test --help | |
| echo "✅ Docker volume mount test passed for Python ${{ matrix.python-version }}" | |
| - name: Login to GitHub Container Registry | |
| if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')) | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: | | |
| ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=ref,event=branch,suffix=-py${{ matrix.python-version }} | |
| type=semver,pattern={{version}}-py${{ matrix.python-version }} | |
| type=semver,pattern={{major}}.{{minor}}-py${{ matrix.python-version }} | |
| type=semver,pattern={{major}}-py${{ matrix.python-version }} | |
| type=sha,format=short,suffix=-py${{ matrix.python-version }} | |
| flavor: | | |
| latest=${{ matrix.python-version == '3.11' }} | |
| - name: Build and Push Docker Image | |
| if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')) | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| build-args: | | |
| PYTHON_VERSION=${{ matrix.python-version }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max |